[Rt-commit] rt branch, 4.4/search-watcher-by-group-name, created. rt-4.4.4-507-g0d8c8b5306
? sunnavy
sunnavy at bestpractical.com
Thu Jun 3 18:21:53 EDT 2021
The branch, 4.4/search-watcher-by-group-name has been created
at 0d8c8b5306edf657edc9c17ff3e0a7c079985f14 (commit)
- Log -----------------------------------------------------------------
commit f420227f0d1099056f64cdd9c64823cf104fc39b
Author: sunnavy <sunnavy at bestpractical.com>
Date: Tue Nov 3 05:00:16 2020 +0800
Support to search user defined group names in watcher limit
To search tickets that have a specified group in a role, previously we
had to specify the group id like "AdminCc.id = 123", which is not
convenient. Here we support group names to improve this, e.g.
"AdminCc.Name = 'team foo'".
In case the name in question is shared by a user and a group(which is
weird and should be quite rare in real world), both will be respected.
I.e. we return tickets that have either the user or the group in the
role.
diff --git a/lib/RT/SearchBuilder/Role/Roles.pm b/lib/RT/SearchBuilder/Role/Roles.pm
index 98fb469f3c..3506d61165 100644
--- a/lib/RT/SearchBuilder/Role/Roles.pm
+++ b/lib/RT/SearchBuilder/Role/Roles.pm
@@ -255,7 +255,7 @@ sub RoleLimit {
my $column = $type ? $class->Role($type)->{Column} : undef;
- # if it's equality op and search by Email or Name then we can preload user
+ # if it's equality op and search by Email or Name then we can preload user/group
# we do it to help some DBs better estimate number of rows and get better plans
if ( $args{QUOTEVALUE} && $args{OPERATOR} =~ /^!?=$/
&& (!$args{FIELD} || $args{FIELD} eq 'Name' || $args{FIELD} eq 'EmailAddress') ) {
@@ -265,8 +265,27 @@ sub RoleLimit {
? ($column ? 'Load' : 'LoadByEmail')
: $args{FIELD} eq 'EmailAddress' ? 'LoadByEmail': 'Load';
$o->$method( $args{VALUE} );
+ my @values;
+ @values = $o->Id if $o->Id;
+
+ if ( !$args{FIELD} || $args{FIELD} eq 'Name' ) {
+ my $group = RT::Group->new( $self->CurrentUser );
+ $group->LoadUserDefinedGroup( $args{VALUE} );
+ push @values, $group->Id if $group->Id;
+ }
+
$args{FIELD} = 'id';
- $args{VALUE} = $o->id || 0;
+ if ( @values == 1 ) {
+ $args{VALUE} = $values[0];
+ }
+ elsif ( @values > 1 ) {
+ RT->Logger->debug("Name $args{VALUE} is used in both user and group");
+ $args{VALUE} = \@values;
+ $args{OPERATOR} = $args{OPERATOR} =~ /!/ ? 'NOT IN' : 'IN';
+ }
+ else {
+ $args{VALUE} = 0;
+ }
}
if ( $column and $args{FIELD} and $args{FIELD} eq 'id' ) {
@@ -396,15 +415,16 @@ sub RoleLimit {
GroupsAlias => $groups, New => 1, Left => 0
);
if ($args{FIELD} eq "id") {
- my @ids;
- if ( $is_shallow ) {
- @ids = $args{VALUE};
- }
- else {
- my $groups = RT::Groups->new( RT->SystemUser );
- $groups->LimitToUserDefinedGroups;
- $groups->WithMember( PrincipalId => $args{VALUE}, Recursively => 1 );
- @ids = ( $args{VALUE}, map { $_->id } @{ $groups->ItemsArrayRef } );
+ my @ids = ref $args{VALUE} eq 'ARRAY' ? @{ $args{VALUE} } : $args{VALUE};
+ if ( !$is_shallow ) {
+ my @group_ids;
+ for my $id (@ids) {
+ my $groups = RT::Groups->new( RT->SystemUser );
+ $groups->LimitToUserDefinedGroups;
+ $groups->WithMember( PrincipalId => $id, Recursively => 1 );
+ push @group_ids, map { $_->id } @{ $groups->ItemsArrayRef };
+ }
+ push @ids, @group_ids;
}
# Save a left join to Users, if possible
commit 0d8c8b5306edf657edc9c17ff3e0a7c079985f14
Author: sunnavy <sunnavy at bestpractical.com>
Date: Tue Nov 3 14:38:48 2020 +0800
Test ticket search by id/name of the group added in watchers
diff --git a/t/ticket/search_by_watcher_group.t b/t/ticket/search_by_watcher_group.t
index 9c522d5d0b..b02e2cc74a 100644
--- a/t/ticket/search_by_watcher_group.t
+++ b/t/ticket/search_by_watcher_group.t
@@ -20,6 +20,9 @@ my $group;
my $root = RT::Test->load_or_create_user( Name => 'root', MemberOf => $group->id );
ok $root && $root->id;
+my $test_user = RT::Test->load_or_create_user( Name => 'Test' );
+ok $test_user && $test_user->id;
+
RT::Test->create_tickets(
{ Queue => $q, },
{ Subject => '-', },
@@ -27,14 +30,24 @@ RT::Test->create_tickets(
{ Subject => 'r', Requestor => $root->id },
{ Subject => 'c', Cc => $root->id },
{ Subject => 'a', AdminCc => $root->id },
+ { Subject => 'ag', AdminCc => $group->id },
+ { Subject => 'ru', Requestor => $test_user->id },
);
run_tests(
'OwnerGroup = "Test"' => { '-' => 0, o => 1, r => 0, c => 0, a => 0 },
'RequestorGroup = "Test"' => { '-' => 0, o => 0, r => 1, c => 0, a => 0 },
'CCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 1, a => 0 },
- 'AdminCCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 0, a => 1 },
- 'WatcherGroup = "Test"' => { '-' => 0, o => 1, r => 1, c => 1, a => 1 },
+ 'AdminCCGroup = "Test"' => { '-' => 0, o => 0, r => 0, c => 0, a => 1, ag => 1 },
+ 'WatcherGroup = "Test"' => { '-' => 0, o => 1, r => 1, c => 1, a => 1, ag => 1 },
+ 'Requestor.id = ' . $test_user->Id => { '-' => 0, ru => 1 },
+ 'Requestor.Name = "Test"' => { '-' => 0, ru => 1 },
+ 'AdminCc.id = ' . $group->Id => { '-' => 0, ag => 1 },
+ 'AdminCc = "Test"' => { '-' => 0, ag => 1 },
+ 'AdminCc.Name = "Test"' => { '-' => 0, ag => 1 },
+ 'Watcher.id = ' . $test_user->Id => { '-' => 0, ru => 1 },
+ 'Watcher.id = ' . $group->Id => { '-' => 0, ag => 1 },
+ 'Watcher.Name = "Test"' => { '-' => 0, ru => 1, ag => 1 },
);
warning_like {
-----------------------------------------------------------------------
More information about the rt-commit
mailing list