[Bps-public-commit] rt-extension-rightsdebugger branch, master, updated. 6c72af87d6cab5a4c2df3e97c41acf3f2d267ff0

Shawn Moore shawn at bestpractical.com
Tue Mar 7 14:12:08 EST 2017


The branch, master has been updated
       via  6c72af87d6cab5a4c2df3e97c41acf3f2d267ff0 (commit)
      from  61f93dc28e46b888c0f0d209d142c954f848051e (commit)

Summary of changes:
 lib/RT/Extension/RightsDebugger.pm | 59 ++++++++++++++++++--------------------
 1 file changed, 28 insertions(+), 31 deletions(-)

- Log -----------------------------------------------------------------
commit 6c72af87d6cab5a4c2df3e97c41acf3f2d267ff0
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Tue Mar 7 19:10:31 2017 +0000

    Highlight "user:root" as just "root"
    
    This is imperfect because it may produce false positives, such as a group named
    "Groots" would match. It would be better to flag just the specific principal
    instead, but the implementation of that is (currently) left as an exercise to
    the reader.

diff --git a/lib/RT/Extension/RightsDebugger.pm b/lib/RT/Extension/RightsDebugger.pm
index 89b36b5..4238d6d 100644
--- a/lib/RT/Extension/RightsDebugger.pm
+++ b/lib/RT/Extension/RightsDebugger.pm
@@ -22,7 +22,24 @@ sub _EscapeHTML {
 }
 
 sub _HighlightTerm {
-    my ($text, $re) = @_;
+    my ($text, $term) = @_;
+
+    $term ||= '';
+
+    # strip user:root leaving just root
+    $term =~ s{
+        ^
+            \s*
+            (u|user|g|group)
+            \s*
+            [:#]
+            \s*
+            (.+?)
+            \s*
+        $
+    }{$2}xi;
+
+    my $re = qr/\Q$term\E/i;
 
     $text =~ s{
         \G         # where we left off the previous iteration thanks to /g
@@ -39,25 +56,17 @@ sub _HighlightTerm {
 
 sub _HighlightSerializedForSearch {
     my $serialized = shift;
-    my $search     = shift;
+    my $args       = shift;
 
     # highlight matching terms
-    $serialized->{right_highlighted} = _HighlightTerm($serialized->{right}, join '|', @{ $search->{right} || [] });
+    $serialized->{right_highlighted} = _HighlightTerm($serialized->{right}, $args->{search});
 
     for my $key (qw/principal object/) {
         for my $record ($serialized->{$key}, $serialized->{$key}->{primary_record}) {
             next if !$record;
 
-            if (my $matchers = $search->{$key}) {
-                my $re = join '|', @$matchers;
-                for my $column (qw/label detail/) {
-                    $record->{$column . '_highlighted'} = _HighlightTerm($record->{$column}, $re);
-                }
-            }
-
             for my $column (qw/label detail/) {
-                # make sure we escape html if there was no search
-                $record->{$column . '_highlighted'} //= _EscapeHTML($record->{$column});
+                $record->{$column . '_highlighted'} = _HighlightTerm($record->{$column}, $args->{$key});
             }
         }
     }
@@ -104,7 +113,6 @@ sub Search {
     );
 
     my @results;
-    my %search;
 
     my $ACL = RT::ACL->new($self->CurrentUser);
 
@@ -204,16 +212,6 @@ sub Search {
 
     $ACL->RowsPerPage(100);
 
-    for my $key (qw/principal object right/) {
-        if (my $search = $args{$key}) {
-            my @matchers;
-            for my $term ($key eq 'right' ? (split ' ', $search) : $search) {
-                push @matchers, qr/\Q$term\E/i;
-            }
-            $search{$key} = \@matchers;
-        }
-    }
-
     my $continueAfter;
 
     ACE: while (my $ACE = $ACL->Next) {
@@ -225,21 +223,20 @@ sub Search {
 	    # searching in SQL is absolutely a nonstarter
             next KEY unless $use_regex_search_for{$key};
 
-            if (my $matchers = $search{$key}) {
+            if (my $term = $args{$key}) {
                 my $record = $serialized->{$key};
-                for my $re (@$matchers) {
-                    next KEY if $record->{class}  =~ $re
-                             || $record->{id}     =~ $re
-                             || $record->{label}  =~ $re
-                             || $record->{detail} =~ $re;
-                }
+                my $re = qr/\Q$term\E/i;
+                next KEY if $record->{class}  =~ $re
+                         || $record->{id}     =~ $re
+                         || $record->{label}  =~ $re
+                         || $record->{detail} =~ $re;
 
                 # no matches
                 next ACE;
             }
         }
 
-        _HighlightSerializedForSearch($serialized, \%search);
+        _HighlightSerializedForSearch($serialized, \%args);
 
         push @results, $serialized;
     }

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


More information about the Bps-public-commit mailing list