[Rt-commit] rt branch, 5.0/rest2-query-by-json-support-custom-fields, created. rt-5.0.1-16-ge6016ac1e9
Craig Kaiser
craig at bestpractical.com
Tue Mar 2 10:06:59 EST 2021
The branch, 5.0/rest2-query-by-json-support-custom-fields has been created
at e6016ac1e96936f08943ccefe8ad91ee919000b4 (commit)
- Log -----------------------------------------------------------------
commit d7ecd6fce26e6ee1bc7062cbc86ad5342ef71645
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 00e03e2e32..4c9251c317 100644
--- a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
@@ -96,10 +96,31 @@ sub limit_collection {
$collection->{'find_disabled_rows'} = 1
if $self->request->param('find_disabled_rows');
+
+ my $customFieldObject = 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};
+ next unless $limit->{field} and defined $limit->{value};
+
+ # Check if we have any custom field searches
+ if ( $limit->{'field'} =~ /CF\.\{.*\}/ ) {
+ my $cfName = $limit->{'field'};
+ $cfName =~ s/CF\.\{(.*)\}/$1/;
+
+ my ($ret, $msg) = $customFieldObject->Load( $cfName );
+ unless ( $ret && $customFieldObject->Id ) {
+ RT::Logger->error( "Could not load custom field: $cfName: $msg" );
+ next;
+ }
+
+ $collection->LimitCustomField(
+ OPERATOR => '=',
+ VALUE => $limit->{'value'},
+ CUSTOMFIELD => $customFieldObject->Id
+ );
+ }
+
+ next unless $searchable{$limit->{field}};
$collection->Limit(
FIELD => $limit->{field},
commit e6016ac1e96936f08943ccefe8ad91ee919000b4
Author: craig kaiser <craig at bestpractical.com>
Date: Tue Mar 2 10:06:10 2021 -0500
Add test for searching users custom fields with REST2
diff --git a/t/rest2/users.t b/t/rest2/users.t
index 0b686df69d..cb10e2ceeb 100644
--- a/t/rest2/users.t
+++ b/t/rest2/users.t
@@ -161,7 +161,44 @@ $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 => $_ ) for qw/SeeCustomField ModifyCustomField/;
+
+ (my $ret, $msg) = $user_foo->AddCustomFieldValue( Field => 'Department', Value => 'HR' );
+ ok ($ret, "Added Dapartment custom field value to user_foo");
+
+ my $payload = [
+ {
+ "field" => "CF.{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;
+ ok( $content->{'count'} eq 1, "Found one user" );
+ ok( $content->{'items'}[0]->{'id'} eq 'foo', "Found foo user" );
+}
+
+$test_user->PrincipalObj->RevokeRight( Right => $_ ) for qw/SeeCustomField ModifyCustomField ShowUserHistory AdminUsers/;
done_testing;
-----------------------------------------------------------------------
More information about the rt-commit
mailing list