[Rt-commit] rt branch, 5.0-trunk, updated. rt-5.0.1-26-g0ae4eae458
? sunnavy
sunnavy at bestpractical.com
Fri Mar 5 12:57:59 EST 2021
The branch, 5.0-trunk has been updated
via 0ae4eae458640bc0cd64e8d64467adfbcdf882ce (commit)
via 5563716d6743649fd75472d54e8c1a849acd7f39 (commit)
via 99ef673776d418f1009ee87fce1e92893934a027 (commit)
via 4ad39ac8221967b0cb1ea1c9960c96c305afed13 (commit)
via f7365e39fc464fe689f5ea9d5c7d7a9741d3ff14 (commit)
from 94c9c1d2d457b0e8e8b3c7dbca1a8b28017b74e8 (commit)
Summary of changes:
lib/RT/REST2.pm | 11 ++++-
lib/RT/REST2/Resource/Collection/QueryByJSON.pm | 53 +++++++++++++++++++------
t/rest2/groups.t | 53 +++++++++++++++++++++++++
t/rest2/users.t | 37 ++++++++++++++++-
4 files changed, 138 insertions(+), 16 deletions(-)
create mode 100644 t/rest2/groups.t
- Log -----------------------------------------------------------------
commit f7365e39fc464fe689f5ea9d5c7d7a9741d3ff14
Author: craig kaiser <craig at bestpractical.com>
Date: Tue Mar 2 09:42:01 2021 -0500
Add support for custom fields in REST2 JSON query
diff --git a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm b/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
index 0caa28d7d1..0314906ad4 100644
--- a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
@@ -97,22 +97,49 @@ sub limit_collection_from_json {
my @fields = $self->searchable_fields;
my %searchable = map {; $_ => 1 } @fields;
+ my $custom_field_object = RT::CustomField->new( $self->request->env->{"rt.current_user"} );
+
for my $limit (@$query) {
- next unless $limit->{field}
- and $searchable{$limit->{field}}
- and defined $limit->{value};
-
- $collection->Limit(
- FIELD => $limit->{field},
- VALUE => $limit->{value},
- ( $limit->{operator}
+ next unless $limit->{field} && defined $limit->{value};
+
+ if ( $limit->{field} =~ /(?:CF|CustomField)\.\{(.*)\}/i ) {
+ my $cf_name = $1;
+ next unless $cf_name;
+
+ my ($ret, $msg) = $custom_field_object->LoadByName(
+ Name => $cf_name,
+ LookupType => $collection->RecordClass->CustomFieldLookupType,
+ IncludeGlobal => 1
+ );
+
+ unless ( $ret && $custom_field_object->Id ) {
+ RT::Logger->error( "Could not load custom field: $limit->{'field'}: $msg" );
+ next;
+ }
+
+ $collection->LimitCustomField(
+ VALUE => $limit->{'value'},
+ CUSTOMFIELD => $custom_field_object->Id,
+ ( $limit->{operator}
? (OPERATOR => $limit->{operator})
: () ),
- CASESENSITIVE => ($limit->{case_sensitive} || 0),
- ( $limit->{entry_aggregator}
- ? (ENTRYAGGREGATOR => $limit->{entry_aggregator})
- : () ),
- );
+ );
+ }
+ else {
+ next unless $searchable{$limit->{field}};
+
+ $collection->Limit(
+ FIELD => $limit->{field},
+ VALUE => $limit->{value},
+ ( $limit->{operator}
+ ? (OPERATOR => $limit->{operator})
+ : () ),
+ CASESENSITIVE => ($limit->{case_sensitive} || 0),
+ ( $limit->{entry_aggregator}
+ ? (ENTRYAGGREGATOR => $limit->{entry_aggregator})
+ : () ),
+ );
+ }
}
return 1;
commit 4ad39ac8221967b0cb1ea1c9960c96c305afed13
Author: craig kaiser <craig at bestpractical.com>
Date: Tue Mar 2 10:06:10 2021 -0500
Add test for searching users based on custom fields with REST2
diff --git a/t/rest2/users.t b/t/rest2/users.t
index 0b686df69d..2d6728bf75 100644
--- a/t/rest2/users.t
+++ b/t/rest2/users.t
@@ -161,7 +161,40 @@ $test_user->PrincipalObj->GrantRight(Right => 'AdminGroupMembership');
);
}
-$test_user->PrincipalObj->RevokeRight(Right => 'ShowUserHistory');
-$test_user->PrincipalObj->RevokeRight(Right => 'AdminUsers');
+diag "Test searching users based on custom field value";
+{
+ my $cf = RT::CustomField->new( RT->SystemUser );
+ ok( $cf, "Have a CustomField object" );
+
+ my ( $id, $msg ) = $cf->Create(
+ Name => 'Department',
+ Description => 'A Testing custom field',
+ Type => 'Freeform',
+ MaxValues => 1,
+ LookupType => RT::User->CustomFieldLookupType,
+ );
+ ok( $id, 'User custom field correctly created' );
+ ok( $cf->AddToObject( RT::User->new( RT->SystemUser ) ), 'applied Testing CF globally' );
+
+ $test_user->PrincipalObj->GrantRight( Right => 'SeeCustomField' );
+
+ ( my $ret, $msg ) = $user_foo->AddCustomFieldValue( Field => 'Department', Value => 'HR' );
+ ok( $ret, "Added Dapartment custom field value to user_foo" );
+
+ my $payload = [
+ { "field" => "CustomField.{Department}",
+ "value" => "HR",
+ "operator" => "="
+ }
+ ];
+
+ my $res = $mech->post_json( "$rest_base_path/users/", $payload, 'Authorization' => $auth, );
+ is( $res->code, 200 );
+ my $content = $mech->json_response;
+ is( $content->{'count'}, 1, "Found one user" );
+ is( $content->{'items'}[0]->{'id'}, 'foo', "Found foo user" );
+}
+
+$test_user->PrincipalObj->RevokeRight( Right => $_ ) for qw/SeeCustomField ShowUserHistory AdminUsers/;
done_testing;
commit 99ef673776d418f1009ee87fce1e92893934a027
Author: craig kaiser <craig at bestpractical.com>
Date: Tue Mar 2 17:43:14 2021 -0500
Add test for searching groups based on custom fields with REST2
diff --git a/t/rest2/groups.t b/t/rest2/groups.t
new file mode 100644
index 0000000000..043b11e179
--- /dev/null
+++ b/t/rest2/groups.t
@@ -0,0 +1,53 @@
+use strict;
+use warnings;
+use RT::Test::REST2 tests => undef;
+use Test::Deep;
+
+my $mech = RT::Test::REST2->mech;
+
+my $auth = RT::Test::REST2->authorization_header;
+
+my $rest_base_path = '/REST/2.0';
+
+my $test_user = RT::Test::REST2->user;
+$test_user->PrincipalObj->GrantRight( Right => 'SuperUser' );
+
+diag "Test searching groups based on custom field value";
+{
+ my $group1 = RT::Group->new( RT->SystemUser );
+ $group1->CreateUserDefinedGroup( Name => 'Group 1' );
+
+ my $group2 = RT::Group->new( RT->SystemUser );
+ $group2->CreateUserDefinedGroup( Name => 'Group 2' );
+
+ my $cf = RT::CustomField->new( RT->SystemUser );
+ ok( $cf, "Have a CustomField object" );
+
+ my ( $id, $msg ) = $cf->Create(
+ Name => 'Group Type',
+ Description => 'A Testing custom field',
+ Type => 'Freeform',
+ MaxValues => 1,
+ LookupType => RT::Group->CustomFieldLookupType,
+ );
+ ok( $id, 'Group custom field correctly created' );
+ ok( $cf->AddToObject( RT::Group->new( RT->SystemUser ) ), 'applied Testing CF globally' );
+
+ ( my $ret, $msg ) = $group1->AddCustomFieldValue( Field => 'Group Type', Value => 'Test' );
+ ok( $ret, "Added Group Type custom field value 'Test' to group1" );
+
+ my $payload = [
+ { "field" => "CustomField.{Group Type}",
+ "value" => "Test",
+ }
+ ];
+
+ my $res = $mech->post_json( "$rest_base_path/groups", $payload, 'Authorization' => $auth, );
+ is( $res->code, 200 );
+ my $content = $mech->json_response;
+ is( $content->{'count'}, 1, "Found one group" );
+ is( $content->{'items'}[0]->{'id'}, $group1->Id, "Found group1 group" );
+}
+$test_user->PrincipalObj->RevokeRight( Right => 'SuperUser' );
+
+done_testing();
commit 5563716d6743649fd75472d54e8c1a849acd7f39
Author: craig kaiser <craig at bestpractical.com>
Date: Fri Mar 5 08:33:02 2021 -0500
Add documentation for REST2 custom field JSON queries
diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 957c778dc9..f8ed84ea5e 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -689,6 +689,11 @@ Below are some examples using the endpoints above.
-d '[{ "field" : "id", "operator" : ">=", "value" : 0 }]'
'https://myrt.com/REST/2.0/assets'
+ # Search Assets Based On Custom Field Values using L</JSON searches>
+ curl -X POST -H "Content-Type: application/json" -u 'root:password'
+ -d '[{ "field" : "CustomField.{Department}", "value" : "Engineering" }]'
+ 'https://myrt.com/REST/2.0/assets'
+
# Search assets using AssetSQL
curl -X POST -u 'root:password' -d "query=Catalog='General assets' AND 'CF.{Asset Type}' LIKE 'Computer'"
'https://myrt.com/REST/2.0/assets'
@@ -936,7 +941,11 @@ values). An example:
"value": "Engineering" },
{ "field": "Lifecycle",
- "value": "helpdesk" }
+ "value": "helpdesk" },
+
+ { "field" : "CustomField.{Department}",
+ "operator" : "=",
+ "value" : "Human Resources" }
]
'
commit 0ae4eae458640bc0cd64e8d64467adfbcdf882ce
Merge: 94c9c1d2d4 5563716d67
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sat Mar 6 01:55:40 2021 +0800
Merge branch '5.0/rest2-query-by-json-support-custom-fields' into 5.0-trunk
-----------------------------------------------------------------------
More information about the rt-commit
mailing list