[Rt-commit] rt branch, 5.0/rest2-query-by-json-support-custom-fields, created. rt-5.0.1-17-g0d62088a90

Craig Kaiser craig at bestpractical.com
Tue Mar 2 17:43:42 EST 2021


The branch, 5.0/rest2-query-by-json-support-custom-fields has been created
        at  0d62088a908d8135c518c6c78d64f32507030642 (commit)

- Log -----------------------------------------------------------------
commit ee70d6b23ab692fb4afd226334beedb2629a7357
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..66ca0117fb 100644
--- a/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
+++ b/lib/RT/REST2/Resource/Collection/QueryByJSON.pm
@@ -96,36 +96,58 @@ 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};
-
-        $collection->Limit(
-            FIELD       => $limit->{field},
-            VALUE       => $limit->{value},
-            ( $limit->{operator}
+        next unless ( $limit->{field} || $limit->{'CustomField'} ) && defined $limit->{value};
+
+        if ( $limit->{field} ) {
+            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})
+                    : () ),
+            );
+        }
+        elsif ( $limit->{'CustomField'} ) {
+            my ($ret, $msg) = $customFieldObject->Load( $limit->{'CustomField'} );
+            unless ( $ret && $customFieldObject->Id ) {
+                RT::Logger->error( "Could not load custom field: $limit->{'CustomField'}: $msg" );
+                next;
+            }
+
+            $collection->LimitCustomField(
+              VALUE       => $limit->{'value'},
+              CUSTOMFIELD => $customFieldObject->Id,
+              ( $limit->{operator}
                 ? (OPERATOR => $limit->{operator})
                 : () ),
             CASESENSITIVE => ($limit->{case_sensitive} || 0),
             ( $limit->{entry_aggregator}
                 ? (ENTRYAGGREGATOR => $limit->{entry_aggregator})
                 : () ),
-        );
+            );
+        }
     }
-
     my @orderby_cols;
-    my @orders = $self->request->param('order');
-    foreach my $orderby ($self->request->param('orderby')) {
-        my $order = shift @orders || 'ASC';
-        $order = uc($order);
-        $order = 'ASC' unless $order eq 'DESC';
-        push @orderby_cols, {FIELD => $orderby, ORDER => $order};
-    }
-    $self->collection->OrderByCols(@orderby_cols)
-        if @orderby_cols;
-
-    return 1;
+      my @orders = $self->request->param('order');
+      foreach my $orderby ($self->request->param('orderby')) {
+          my $order = shift @orders || 'ASC';
+          $order = uc($order);
+          $order = 'ASC' unless $order eq 'DESC';
+          push @orderby_cols, {FIELD => $orderby, ORDER => $order};
+      }
+      $self->collection->OrderByCols(@orderby_cols)
+          if @orderby_cols;
+
+      return 1;
 }
 
 1;

commit df6e0ea95c849a180c8174d1a1e26c9422f1fae4
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..ce750ab8d1 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 = [
+    {
+        "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;
+  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;

commit 0d62088a908d8135c518c6c78d64f32507030642
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

diff --git a/t/rest2/groups.t b/t/rest2/groups.t
new file mode 100644
index 0000000000..6af77120e3
--- /dev/null
+++ b/t/rest2/groups.t
@@ -0,0 +1,58 @@
+use strict;
+use warnings;
+use RT::Test::REST2 tests => undef;
+use Test::Deep;
+
+use Data::Dumper;
+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 = [
+        {
+            "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;
+    ok( $content->{'count'} eq 1, "Found one group" );
+    ok( $content->{'items'}[0]->{'id'} eq $group1->Id, "Found group1 group" );
+}
+$test_user->PrincipalObj->RevokeRight( Right => 'SuperUser' );
+
+done_testing();

-----------------------------------------------------------------------


More information about the rt-commit mailing list