[Rt-commit] rt branch 5.0/search-user-cf-in-sql created. rt-5.0.2-10-g06f83df55c
BPS Git Server
git at git.bestpractical.com
Wed Sep 15 22:11:52 UTC 2021
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "rt".
The branch, 5.0/search-user-cf-in-sql has been created
at 06f83df55cb8d8cf9069f45d238e6bf3443f0f59 (commit)
- Log -----------------------------------------------------------------
commit 06f83df55cb8d8cf9069f45d238e6bf3443f0f59
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Sep 16 05:32:03 2021 +0800
Test user custom fields searches in TicketSQL
diff --git a/t/api/tickets.t b/t/api/tickets.t
index ce71e3b6d9..e4d8a38754 100644
--- a/t/api/tickets.t
+++ b/t/api/tickets.t
@@ -379,4 +379,90 @@ diag "Columns as values in searches";
is( $count, 1, 'Found 1 ticket' );
}
+diag "Ticket role group member custom fields";
+{
+ my $cr = RT::CustomRole->new( RT->SystemUser );
+ my ( $ret, $msg ) = $cr->Create(
+ Name => 'Engineer',
+ MaxValues => 0,
+ );
+ ok( $ret, "Created custom role: $msg" );
+
+ ( $ret, $msg ) = $cr->AddToObject( ObjectId => 'General' );
+ ok( $ret, "Added CR to queue: $msg" );
+
+ my $cf = RT::CustomField->new( RT->SystemUser );
+ ( $ret, $msg ) = $cf->Create(
+ Name => 'manager',
+ Type => 'FreeformSingle',
+ LookupType => RT::User->CustomFieldLookupType,
+ );
+ ok( $ret, "Created user cf: $msg" );
+ ok( $cf->AddToObject( RT::User->new( RT->SystemUser ) ), 'Applied user CF globally' );
+
+ my $ticket = RT::Test->create_ticket( Queue => 'General', Subject => 'test role member cfs' );
+ my $admincc = $ticket->RoleGroup('AdminCc');
+
+ my $alice = RT::Test->load_or_create_user( Name => 'alice' );
+ ok( $alice->AddCustomFieldValue( Field => 'manager', Value => 'bob' ) );
+
+ my $bob = RT::Test->load_or_create_user( Name => 'bob' );
+ ok( $bob->AddCustomFieldValue( Field => 'manager', Value => 'root' ) );
+
+ my $richard = RT::Test->load_or_create_user( Name => 'richard' );
+ ok( $richard->AddCustomFieldValue( Field => 'manager', Value => 'alice' ) );
+
+ my $tickets = RT::Tickets->new( RT->SystemUser );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND Owner.CustomField.{manager} = 'bob'");
+ ok( !$tickets->Count, 'No tickets found' );
+
+ $alice->PrincipalObj->GrantRight( Right => 'OwnTicket' );
+ ( $ret, $msg ) = $ticket->SetOwner('alice');
+ ok( $ret, $msg );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND Owner.CustomField.{manager} = 'bob'");
+ is( $tickets->Count, 1, 'Found 1 ticket' );
+ is( $tickets->First->id, $ticket->id, 'Found the ticket' );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND Requestor.CustomField.manager = 'alice'");
+ ok( !$tickets->Count, 'No tickets found' );
+
+ ( $ret, $msg ) = $ticket->RoleGroup('Requestor')->AddMember( $richard->Id );
+ ok( $ret, $msg );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND Requestor.CustomField.manager = 'alice'");
+ is( $tickets->Count, 1, 'Found 1 ticket' );
+ is( $tickets->First->id, $ticket->id, 'Found the ticket' );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND CustomRole.{Engineer}.CustomField.{manager} = 'root'");
+ ok( !$tickets->Count, 'No tickets found' );
+
+ ( $ret, $msg ) = $ticket->RoleGroup( $cr->GroupType )->AddMember( $bob->Id );
+ ok( $ret, $msg );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND CustomRole.{Engineer}.CustomField.{manager} = 'root'");
+ is( $tickets->Count, 1, 'Found 1 ticket' );
+ is( $tickets->First->id, $ticket->id, 'Found the ticket' );
+
+ ok( $bob->AddCustomFieldValue( Field => 'manager', Value => 'nobody' ) );
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND CustomRole.{Engineer}.CustomField.{manager} = 'root'");
+ ok( !$tickets->Count, 'No tickets found' );
+
+ $alice->PrincipalObj->GrantRight( Right => 'ShowTicket' );
+ my $alice_current_user = RT::CurrentUser->new( RT->SystemUser );
+ $alice_current_user->Load( $alice->Id );
+
+ $tickets = RT::Tickets->new($alice_current_user);
+
+ $tickets->FromSQL("Subject = 'test role member cfs' AND Owner.CustomField.{manager} = 'bob'");
+ ok( !$tickets->Count, 'No tickets found' );
+
+ $alice->PrincipalObj->GrantRight( Right => 'SeeCustomField', Object => $cf );
+ $tickets->FromSQL("Subject = 'test role member cfs' AND Owner.CustomField.{manager} = 'bob'");
+ is( $tickets->Count, 1, 'Found 1 ticket' );
+ is( $tickets->First->id, $ticket->id, 'Found the ticket' );
+}
+
done_testing;
commit e072d6ba314b81d5fee2ebdcaa2539001e1b19dc
Author: sunnavy <sunnavy at bestpractical.com>
Date: Thu Sep 16 05:25:46 2021 +0800
Support to search user custom fields in TicketSQL/AssetSQL
diff --git a/lib/RT/Assets.pm b/lib/RT/Assets.pm
index 8cd4d4c2f8..af21e03aa9 100644
--- a/lib/RT/Assets.pm
+++ b/lib/RT/Assets.pm
@@ -1238,6 +1238,7 @@ sub _WatcherLimit {
# Bail if the subfield is not allowed
if ( $rest{SUBKEY}
+ and $rest{SUBKEY} !~ /^CustomField\.(?:\w+|\{.+\})$/i
and not grep { $_ eq $rest{SUBKEY} } @{$SEARCHABLE_SUBFIELDS{'User'}})
{
die "Invalid watcher subfield: '$rest{SUBKEY}'";
diff --git a/lib/RT/SearchBuilder/Role/Roles.pm b/lib/RT/SearchBuilder/Role/Roles.pm
index e3eeb39af7..d70f5a0b12 100644
--- a/lib/RT/SearchBuilder/Role/Roles.pm
+++ b/lib/RT/SearchBuilder/Role/Roles.pm
@@ -338,11 +338,48 @@ sub RoleLimit {
# do the right thing when there is only one exist and semi-working solution
# otherwise.
my $users_obj = RT::Users->new( $self->CurrentUser );
- $users_obj->Limit(
- FIELD => $args{FIELD},
- OPERATOR => $args{OPERATOR},
- VALUE => $args{VALUE},
- );
+ if ( $args{FIELD} =~ /^CustomField\.(?:(\w+)|\{(.+)\})$/i ) {
+ my $cf_name = $1 || $2;
+ my $cf = RT::CustomField->new( $self->CurrentUser );
+ $cf->LoadByCols( LookupType => RT::User->CustomFieldLookupType, Name => $cf_name );
+ if ( $cf->id && $cf->CurrentUserHasRight('SeeCustomField') ) {
+ my $ocfvs = $users_obj->Join(
+ ALIAS1 => 'main',
+ FIELD1 => 'id',
+ TABLE2 => 'ObjectCustomFieldValues',
+ FIELD2 => 'ObjectId',
+ );
+
+ $users_obj->Limit(
+ LEFTJOIN => $ocfvs,
+ FIELD => 'CustomField',
+ VALUE => $cf->id,
+ ENTRYAGGREGATOR => 'AND',
+ );
+
+ $users_obj->Limit(
+ LEFTJOIN => $ocfvs,
+ FIELD => 'Disabled',
+ VALUE => 0,
+ ENTRYAGGREGATOR => 'AND',
+ );
+
+ $users_obj->Limit(
+ ALIAS => $ocfvs,
+ FIELD => 'Content',
+ OPERATOR => $args{OPERATOR},
+ VALUE => $args{VALUE},
+ CASESENSITIVE => 0,
+ );
+ }
+ }
+ else {
+ $users_obj->Limit(
+ FIELD => $args{FIELD},
+ OPERATOR => $args{OPERATOR},
+ VALUE => $args{VALUE},
+ );
+ }
$users_obj->OrderBy;
$users_obj->RowsPerPage(2);
my @users = @{ $users_obj->ItemsArrayRef };
@@ -392,21 +429,75 @@ sub RoleLimit {
TABLE2 => 'Users',
FIELD2 => 'id',
);
- $self->Limit(
- LEFTJOIN => $users,
- ALIAS => $users,
- FIELD => $args{FIELD},
- OPERATOR => $args{OPERATOR},
- VALUE => $args{VALUE},
- CASESENSITIVE => 0,
- );
- $self->Limit(
- %args,
- ALIAS => $users,
- FIELD => 'id',
- OPERATOR => 'IS',
- VALUE => 'NULL',
- );
+ if ( $args{FIELD} =~ /^CustomField\.(?:(\w+)|\{(.+)\})$/i ) {
+ my $cf_name = $1 || $2;
+ my $cf = RT::CustomField->new( $self->CurrentUser );
+ $cf->LoadByCols( LookupType => RT::User->CustomFieldLookupType, Name => $cf_name );
+ if ( $cf->id && $cf->CurrentUserHasRight('SeeCustomField') ) {
+ my $ocfvs = $self->NewAlias('ObjectCustomFieldValues');
+ $self->Join(
+ TYPE => 'LEFT',
+ ALIAS1 => $users,
+ FIELD1 => 'id',
+ ALIAS2 => $ocfvs,
+ FIELD2 => 'ObjectId',
+ );
+
+ $self->Limit(
+ LEFTJOIN => $ocfvs,
+ FIELD => 'CustomField',
+ VALUE => $cf->id,
+ ENTRYAGGREGATOR => 'AND',
+ );
+
+ $self->Limit(
+ LEFTJOIN => $ocfvs,
+ FIELD => 'Disabled',
+ VALUE => 0,
+ ENTRYAGGREGATOR => 'AND',
+ );
+
+ $self->Limit(
+ LEFTJOIN => $ocfvs,
+ ALIAS => $ocfvs,
+ OPERATOR => $args{OPERATOR},
+ FIELD => 'Content',
+ VALUE => $args{VALUE},
+ CASESENSITIVE => 0,
+ );
+ $self->Limit(
+ %args,
+ ALIAS => $ocfvs,
+ FIELD => 'id',
+ OPERATOR => 'IS',
+ VALUE => 'NULL',
+ );
+ }
+ else {
+ $self->Limit(
+ %args,
+ FIELD => 'id',
+ VALUE => 0,
+ );
+ }
+ }
+ else {
+ $self->Limit(
+ LEFTJOIN => $users,
+ ALIAS => $users,
+ FIELD => $args{FIELD},
+ OPERATOR => $args{OPERATOR},
+ VALUE => $args{VALUE},
+ CASESENSITIVE => 0,
+ );
+ $self->Limit(
+ %args,
+ ALIAS => $users,
+ FIELD => 'id',
+ OPERATOR => 'IS',
+ VALUE => 'NULL',
+ );
+ }
}
} else {
# positive condition case
@@ -472,14 +563,61 @@ sub RoleLimit {
FIELD2 => 'id',
);
}
- $self->Limit(
- %args,
- ALIAS => $users,
- FIELD => $args{FIELD},
- OPERATOR => $args{OPERATOR},
- VALUE => $args{VALUE},
- CASESENSITIVE => 0,
- );
+
+ if ( $args{FIELD} =~ /^CustomField\.(?:(\w+)|\{(.+)\})$/i ) {
+ my $cf_name = $1 || $2;
+ my $cf = RT::CustomField->new( $self->CurrentUser );
+ $cf->LoadByCols( LookupType => RT::User->CustomFieldLookupType, Name => $cf_name );
+ if ( $cf->id && $cf->CurrentUserHasRight('SeeCustomField') ) {
+ my $ocfvs = $self->NewAlias('ObjectCustomFieldValues');
+ $self->Join(
+ ALIAS1 => $users,
+ FIELD1 => 'id',
+ ALIAS2 => $ocfvs,
+ FIELD2 => 'ObjectId',
+ );
+
+ $self->Limit(
+ LEFTJOIN => $ocfvs,
+ FIELD => 'CustomField',
+ VALUE => $cf->id,
+ ENTRYAGGREGATOR => 'AND',
+ );
+
+ $self->Limit(
+ LEFTJOIN => $ocfvs,
+ FIELD => 'Disabled',
+ VALUE => 0,
+ ENTRYAGGREGATOR => 'AND',
+ );
+
+ $self->Limit(
+ %args,
+ ALIAS => $ocfvs,
+ OPERATOR => $args{OPERATOR},
+ FIELD => 'Content',
+ VALUE => $args{VALUE},
+ CASESENSITIVE => 0,
+ );
+ }
+ else {
+ $self->Limit(
+ %args,
+ FIELD => 'id',
+ VALUE => 0,
+ );
+ }
+ }
+ else {
+ $self->Limit(
+ %args,
+ ALIAS => $users,
+ FIELD => $args{FIELD},
+ OPERATOR => $args{OPERATOR},
+ VALUE => $args{VALUE},
+ CASESENSITIVE => 0,
+ );
+ }
}
}
$self->_CloseParen( $args{SUBCLAUSE} ) if $args{SUBCLAUSE};
diff --git a/lib/RT/Tickets.pm b/lib/RT/Tickets.pm
index 4ffbd6ab61..7b7b7c8404 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1160,6 +1160,7 @@ sub _WatcherLimit {
# Bail if the subfield is not allowed
if ( $column
+ and $column !~ /^CustomField\.(?:\w+|\{.+\})$/i
and not grep { $_ eq $column } @{$SEARCHABLE_SUBFIELDS{'User'}})
{
die "Invalid watcher subfield: '$column'";
-----------------------------------------------------------------------
hooks/post-receive
--
rt
More information about the rt-commit
mailing list