[Rt-commit] rt branch, 4.4/cf-inspection-in-searches, created. rt-4.4.4-24-gba0c3309e

? sunnavy sunnavy at bestpractical.com
Fri Apr 12 14:51:34 EDT 2019


The branch, 4.4/cf-inspection-in-searches has been created
        at  ba0c3309ec023f8411703887b69a0ca4beabb84a (commit)

- Log -----------------------------------------------------------------
commit 7e04d335c195d4a96244caf85b1adfce2d5ba637
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Apr 13 02:14:09 2019 +0800

    Try harder to get custom field objects to inspect in searches
    
    Objects here are only used to inspect custom field properties, to
    generate more accurate queries. So it's fine we load them use system
    user to get around rights check.
    
    We actually already have this functionality, but it's only for custom
    field ids.  This commit enhances it to support other cases including
    objects and also names.
    
    See also a4c8bfa4cb

diff --git a/lib/RT/SearchBuilder.pm b/lib/RT/SearchBuilder.pm
index 09a775ec3..6ff0a4cc7 100644
--- a/lib/RT/SearchBuilder.pm
+++ b/lib/RT/SearchBuilder.pm
@@ -485,6 +485,13 @@ sub _LimitCustomField {
     my $cfkey  = delete $args{KEY};
     if (blessed($cf) and $cf->id) {
         $cfkey ||= $cf->id;
+
+        # Make sure we can really see $cf
+        unless ( $cf->CurrentUserHasRight('SeeCustomField') ) {
+            my $obj = RT::CustomField->new( RT->SystemUser );
+            $obj->Load( $cf->id );
+            $cf = $obj;
+        }
     } elsif ($cf =~ /^\d+$/) {
         # Intentionally load as the system user, so we can build better
         # queries; this is necessary as we don't have a context object
@@ -499,7 +506,21 @@ sub _LimitCustomField {
             $cfkey ||= "$ltype-$cf";
         }
     } else {
-        $cfkey ||= "$ltype-$cf";
+        # Resolve CF by name for better queries, like the above block.
+        my $cfs = RT::CustomFields->new( RT->SystemUser );
+        $cfs->LimitToLookupType($ltype);
+        $cfs->Limit(
+            FIELD         => 'Name',
+            VALUE         => $cf,
+            CASESENSITIVE => 0,
+        );
+        if ( $cfs->Count == 1 ) {
+            $cf = $cfs->Next;
+            $cfkey ||= $cf->id;
+        }
+        else {
+            $cfkey ||= "$ltype-$cf";
+        }
     }
 
     $args{SUBCLAUSE} ||= "cf-$cfkey";

commit ba0c3309ec023f8411703887b69a0ca4beabb84a
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Sat Apr 13 02:45:44 2019 +0800

    Test custom field inspection in searches

diff --git a/t/ticket/search_by_cf_date.t b/t/ticket/search_by_cf_date.t
new file mode 100644
index 000000000..84a8e21ec
--- /dev/null
+++ b/t/ticket/search_by_cf_date.t
@@ -0,0 +1,61 @@
+
+use strict;
+use warnings;
+
+use RT::Test nodata => 1, tests => undef;
+use Test::MockTime 'set_fixed_time';
+
+my $queue = RT::Test->load_or_create_queue( Name => 'General' );
+my $cf = RT::Test->load_or_create_custom_field( Name => 'test_cf', Queue => $queue->id, Type => 'Date' );
+my $cfid = $cf->id;
+
+
+set_fixed_time("2019-04-12T00:00:00Z");
+
+my @tickets = RT::Test->create_tickets(
+    { Queue   => $queue->Name },
+    { Subject => 'Past date ticket', "CustomField-$cfid" => '2019-04-01' },
+    { Subject => 'Future date ticket', "CustomField-$cfid" => '2020-01-01' },
+);
+
+my $tickets = RT::Tickets->new( RT->SystemUser );
+$tickets->FromSQL(q{Queue = 'General' AND CF.test_cf < 'today'});
+is( $tickets->Count, 1, 'Found 1 ticket' );
+is( $tickets->First->id, $tickets[0]->id, 'Found the past ticket' );
+
+$tickets->FromSQL(q{Queue = 'General' AND CF.test_cf > 'today'});
+is( $tickets->Count, 1, 'Found 1 ticket' );
+is( $tickets->First->id, $tickets[1]->id, 'Found the future ticket' );
+
+my $alice = RT::Test->load_or_create_user( Name => 'alice' );
+$alice->PrincipalObj->GrantRight( Object => $queue, Right => 'ShowTicket' );
+
+my $current_alice = RT::CurrentUser->new( RT->SystemUser );
+$current_alice->Load('alice');
+
+$tickets = RT::Tickets->new($current_alice);
+$tickets->FromSQL(q{Queue = 'General' AND CF.test_cf < 'today'});
+TODO: {
+    local $TODO = 'Do not filter by cfs user lacks SeeCustomField';
+    is( $tickets->Count,                     2, 'Found 2 tickets' );
+    is( scalar @{ $tickets->ItemsArrayRef }, 2, 'Found 2 tickets' );
+}
+
+$alice->PrincipalObj->GrantRight( Object => $queue, Right => 'SeeCustomField' );
+$tickets->FromSQL(q{Queue = 'General' AND CF.test_cf < 'today'});
+is( $tickets->Count, 1, 'Found 1 ticket' );
+is( $tickets->First->id, $tickets[0]->id, 'Found the past ticket' );
+
+$tickets->FromSQL(q{Queue = 'General' AND CF.test_cf > 'today'});
+is( $tickets->Count, 1, 'Found 1 ticket' );
+is( $tickets->First->id, $tickets[1]->id, 'Found the future ticket' );
+
+$tickets->FromSQL(qq{Queue = 'General' AND CF.$cfid < 'today'});
+is( $tickets->Count, 1, 'Found 1 ticket' );
+is( $tickets->First->id, $tickets[0]->id, 'Found the past ticket' );
+
+$tickets->FromSQL(qq{Queue = 'General' AND CF.$cfid > 'today'});
+is( $tickets->Count, 1, 'Found 1 ticket' );
+is( $tickets->First->id, $tickets[1]->id, 'Found the future ticket' );
+
+done_testing;

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


More information about the rt-commit mailing list