[Rt-commit] rt branch, 5.0/rest2-collection-role-fields, created. rt-5.0.1-20-g3e86271c57

? sunnavy sunnavy at bestpractical.com
Tue Mar 2 17:37:29 EST 2021


The branch, 5.0/rest2-collection-role-fields has been created
        at  3e86271c57b0a0d75ec5ef64f66533058f46ccb2 (commit)

- Log -----------------------------------------------------------------
commit 3906e946db20bcddbad18fa987e8510f66115b77
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 04:55:38 2021 +0800

    Support core roles for ticket search result in REST2

diff --git a/lib/RT/REST2/Resource/Tickets.pm b/lib/RT/REST2/Resource/Tickets.pm
index 3bdccaf8cd..b8060935e5 100644
--- a/lib/RT/REST2/Resource/Tickets.pm
+++ b/lib/RT/REST2/Resource/Tickets.pm
@@ -64,7 +64,7 @@ sub dispatch_rules {
 }
 
 use Encode qw( decode_utf8 );
-use RT::REST2::Util qw( error_as_json );
+use RT::REST2::Util qw( error_as_json expand_uid );
 use RT::Search::Simple;
 
 has 'query' => (
@@ -114,6 +114,25 @@ sub limit_collection {
     return 1;
 }
 
+sub expand_field {
+    my $self         = shift;
+    my $item         = shift;
+    my $field        = shift;
+    my $param_prefix = shift;
+    if ( $field =~ /^(Requestor|AdminCc|Cc)/ ) {
+        my $role    = $1;
+        my $members = [];
+        if ( my $group = $item->RoleGroup($role) ) {
+            my $gms = $group->MembersObj;
+            while ( my $gm = $gms->Next ) {
+                push @$members, expand_uid( $gm->MemberObj->Object->UID );
+            }
+        }
+        return $members;
+    }
+    return $self->SUPER::expand_field( $item, $field, $param_prefix );
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;

commit 1877ce95e23ec1d4cde128a3ded60934bfc46c28
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 05:33:57 2021 +0800

    Test core role fields for ticket search result in REST2

diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t
index 1fdce910f2..d6676c21a6 100644
--- a/t/rest2/tickets.t
+++ b/t/rest2/tickets.t
@@ -712,4 +712,34 @@ my $json = JSON->new->utf8;
     is( $value->LargeContent, $img_content, 'image file content');
 }
 
+# Ticket Search - Role Fields
+{
+
+    my $payload = {
+        Subject   => 'Ticket creation using REST',
+        Queue     => 'General',
+        Content   => 'Testing ticket creation using REST API.',
+        Requestor => 'alice at example.com',
+        Cc        => 'alice at example.com, bob at example.com',
+        AdminCc   => 'root at example.com',
+    };
+
+    my $res = $mech->post_json( "$rest_base_path/ticket", $payload, 'Authorization' => $auth, );
+    is( $res->code, 201 );
+    ok( my $ticket_url = $res->header('location') );
+    ok( my ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
+
+    $res = $mech->get( "$rest_base_path/tickets?query=id=$ticket_id&fields=Requestor,Cc,AdminCc",
+        'Authorization' => $auth, );
+    is( $res->code, 200 );
+    my $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    my $ticket = $content->{items}->[0];
+    is( $ticket->{Requestor}[0]{id}, 'alice at example.com', 'Requestor id in search result' );
+    is( $ticket->{Cc}[0]{id},        'alice at example.com', 'Cc id in search result' );
+    is( $ticket->{Cc}[1]{id},        'bob at example.com',   'Cc id in search result' );
+    is( $ticket->{AdminCc}[0]{id},   'root at example.com',  'AdminCc id in search result' );
+}
+
 done_testing;

commit cc30db0aec5e66f35c7998273e370c5427756453
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 04:58:44 2021 +0800

    Support core roles for asset search result in REST2

diff --git a/lib/RT/REST2/Resource/Assets.pm b/lib/RT/REST2/Resource/Assets.pm
index 0b988d05ed..5c32dac76d 100644
--- a/lib/RT/REST2/Resource/Assets.pm
+++ b/lib/RT/REST2/Resource/Assets.pm
@@ -63,6 +63,28 @@ sub dispatch_rules {
     )
 }
 
+use RT::REST2::Util qw( expand_uid );
+
+sub expand_field {
+    my $self         = shift;
+    my $item         = shift;
+    my $field        = shift;
+    my $param_prefix = shift;
+    if ( $field =~ /^(Owner|HeldBy|Contact)/ ) {
+        my $role    = $1;
+        my $members = [];
+        if ( my $group = $item->RoleGroup($role) ) {
+            my $gms = $group->MembersObj;
+            while ( my $gm = $gms->Next ) {
+                push @$members, expand_uid( $gm->MemberObj->Object->UID );
+            }
+            $members = shift @$members if $group->SingleMemberRoleGroup;
+        }
+        return $members;
+    }
+    return $self->SUPER::expand_field( $item, $field, $param_prefix );
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;

commit e9cdc843efc267e029d7bf0a0f93575414e6499c
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 05:48:50 2021 +0800

    Test core role fields for asset search result in REST2

diff --git a/t/rest2/assets.t b/t/rest2/assets.t
index 1f1712bfed..0c47374aac 100644
--- a/t/rest2/assets.t
+++ b/t/rest2/assets.t
@@ -253,4 +253,38 @@ my ($asset_url, $asset_id);
     }
 }
 
+# Asset Search - Role Fields
+{
+
+    my $payload = {
+        Name        => 'Asset creation using REST',
+        Description => 'Asset description',
+        Catalog     => 'General assets',
+        Content     => 'Testing asset creation using REST API.',
+        Owner       => 'root at localhost',
+        HeldBy      => 'root at example.com',
+        Contact     => 'alice at example.com, bob at example.com',
+    };
+
+    my $res = $mech->post_json( "$rest_base_path/asset", $payload, 'Authorization' => $auth, );
+    is( $res->code, 201 );
+    ok( my $asset_url = $res->header('location') );
+    ok( my ($asset_id) = $asset_url =~ qr[/asset/(\d+)] );
+
+    $res = $mech->post_json(
+        "$rest_base_path/assets?fields=Owner,HeldBy,Contact",
+        [ { field => 'id', operator => '=', value => $asset_id } ],
+        'Authorization' => $auth,
+    );
+    is( $res->code, 200 );
+    my $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    my $asset = $content->{items}->[0];
+    is( $asset->{Owner}{id},      'root',              'Owner id in search result' );
+    is( $asset->{HeldBy}[0]{id},  'root at example.com',  'HeldBy id in search result' );
+    is( $asset->{Contact}[0]{id}, 'alice at example.com', 'Contact id in search result' );
+    is( $asset->{Contact}[1]{id}, 'bob at example.com',   'Contact id in search result' );
+}
+
 done_testing;

commit ae0291ede194f730161b1e148cc6179163ae2e5d
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 04:59:08 2021 +0800

    Support custom roles for search result in REST2

diff --git a/lib/RT/REST2/Resource.pm b/lib/RT/REST2/Resource.pm
index 1159ba7802..724669367a 100644
--- a/lib/RT/REST2/Resource.pm
+++ b/lib/RT/REST2/Resource.pm
@@ -110,6 +110,29 @@ sub expand_field {
 
             push @{ $result }, values %values if %values;
         }
+    } elsif ($field eq 'CustomRoles') {
+        if ( $item->DOES("RT::Record::Role::Roles") ) {
+            my %data;
+            for my $role ( $item->Roles( ACLOnly => 0 ) ) {
+                next unless $role =~ /^RT::CustomRole-/;
+                $data{$role} = [];
+
+                my $group = $item->RoleGroup($role);
+                if ( !$group->Id ) {
+                    $data{$role} = expand_uid( RT->Nobody->UserObj->UID ) if $item->_ROLES->{$role}{Single};
+                    next;
+                }
+
+                my $gms = $group->MembersObj;
+                while ( my $gm = $gms->Next ) {
+                    push @{ $data{$role} }, expand_uid( $gm->MemberObj->Object->UID );
+                }
+
+                # Avoid the extra array ref for single member roles
+                $data{$role} = shift @{$data{$role}} if $group->SingleMemberRoleGroup;
+            }
+            return \%data;
+        }
     } elsif ($field eq 'ContentLength' && $item->can('ContentLength')) {
         $result = $item->ContentLength;
     } elsif ($item->can('_Accessible') && $item->_Accessible($field => 'read')) {

commit 3e86271c57b0a0d75ec5ef64f66533058f46ccb2
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Wed Mar 3 06:00:59 2021 +0800

    Test custom role fields for ticket search result in REST2

diff --git a/t/rest2/ticket-customroles.t b/t/rest2/ticket-customroles.t
index 0433911ea6..834dd71df8 100644
--- a/t/rest2/ticket-customroles.t
+++ b/t/rest2/ticket-customroles.t
@@ -563,5 +563,32 @@ $user->PrincipalObj->GrantRight( Right => $_ )
     }, 'Later Single Member is Nobody');
 }
 
+# Ticket Search
+{
+
+    my $payload = {
+        Subject            => 'Ticket creation using REST',
+        Queue              => 'General',
+        Content            => 'Testing ticket creation using REST API.',
+        $single->GroupType => 'single2 at example.com',
+        $multi->GroupType  => 'multi at example.com, multi2 at example.com',
+    };
+
+    my $res = $mech->post_json( "$rest_base_path/ticket", $payload, 'Authorization' => $auth, );
+    is( $res->code, 201 );
+    ok( my $ticket_url = $res->header('location') );
+    ok( my ($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)] );
+
+    $res = $mech->get( "$rest_base_path/tickets?query=id=$ticket_id&fields=CustomRoles", 'Authorization' => $auth, );
+    is( $res->code, 200 );
+    my $content = $mech->json_response;
+    is( scalar @{ $content->{items} }, 1 );
+
+    my $ticket = $content->{items}->[0];
+    is( $ticket->{CustomRoles}{ $single->GroupType }{id}, 'single2 at example.com',  'Single Member id in search result' );
+    is( $ticket->{CustomRoles}{ $multi->GroupType }[0]{id}, 'multi at example.com',  'Multi Member id in search result' );
+    is( $ticket->{CustomRoles}{ $multi->GroupType }[1]{id}, 'multi2 at example.com', 'Multi Member id in search result' );
+}
+
 done_testing;
 

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


More information about the rt-commit mailing list