[Rt-commit] rt branch 5.0/search-user-cf-in-sql created. rt-5.0.5-165-gfe05866ada
BPS Git Server
git at git.bestpractical.com
Mon Feb 26 18:52:10 UTC 2024
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 fe05866ada7a337a39a7071c70bab9b9fcd7ca44 (commit)
- Log -----------------------------------------------------------------
commit fe05866ada7a337a39a7071c70bab9b9fcd7ca44
Author: sunnavy <sunnavy at bestpractical.com>
Date: Sat May 21 02:37:26 2022 +0800
Add user cfs to query builder criteria
diff --git a/share/html/Search/Elements/SelectPersonType b/share/html/Search/Elements/SelectPersonType
index a167c9fec2..3ba8b9439d 100644
--- a/share/html/Search/Elements/SelectPersonType
+++ b/share/html/Search/Elements/SelectPersonType
@@ -134,6 +134,12 @@ $m->callback(
);
push @types, map { [ "CustomRole.{" . $_->Name . "}", $_->Name ] } @{ $CustomRoles->ItemsArrayRef };
+my $cfs = RT::CustomFields->new( $session{CurrentUser} );
+$cfs->LimitToLookupType( RT::User->CustomFieldLookupType );
+while ( my $cf = $cfs->Next ) {
+ push @subtypes, 'CustomField.{' . $cf->Name . '}';
+}
+
$m->callback(Types => \@types, Subtypes => \@subtypes);
</%INIT>
commit c8b0e2364b2f10fc6b9b12bbc87e292c511be535
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 6c463cee12d36144ebb4d25c920aedba9d30f5eb
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 30ab53629c..f16cc38cff 100644
--- a/lib/RT/Assets.pm
+++ b/lib/RT/Assets.pm
@@ -1315,6 +1315,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'";
diff --git a/lib/RT/SearchBuilder/Role/Roles.pm b/lib/RT/SearchBuilder/Role/Roles.pm
index 24d7f1daea..35393692c1 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 275fe6653a..b17ff8f378 100644
--- a/lib/RT/Tickets.pm
+++ b/lib/RT/Tickets.pm
@@ -1164,6 +1164,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