Skip to content

Commit eb47c4e

Browse files
committed
Make sure TicketType and ObjectType limits in transaction searches are AND-ed
To add TicketType='ticket' and ObjectType="RT::Ticket" preconditions, previously we wrapped user's TxnSQL with only if it doesn't contain outer parens, of which the regex check was not quite accurate. E.g. ( Type = 'Set' ) OR ( Type = 'Correspond' ) It matches the regex(qr/^\s*\(.*\)$/) but actually we still need to wrap it so the final TxnSQL could be: TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND ( ( Type = 'Set' ) OR ( Type = 'Correspond' ) ) Otherwise all Set/Correspond transactions would be included in search result. To fix the issue above, this commit parses user's TxnSQL to wrap it as long as the root condition is "OR".
1 parent 5afd223 commit eb47c4e

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

lib/RT/Interface/Web.pm

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5870,10 +5870,25 @@ sub PreprocessTransactionSearchQuery {
58705870
my @limits;
58715871
if ( $args{ObjectType} eq 'RT::Ticket' ) {
58725872
if ( $args{Query} !~ /^TicketType = 'ticket' AND ObjectType = '$args{ObjectType}' AND (.+)/ ) {
5873+
require RT::Interface::Web::QueryBuilder::Tree;
5874+
my $tree = RT::Interface::Web::QueryBuilder::Tree->new;
5875+
my @results = $tree->ParseSQL(
5876+
Query => $args{Query},
5877+
CurrentUser => $session{CurrentUser},
5878+
Class => 'RT::Transactions',
5879+
);
5880+
5881+
# Errors will be handled in FromSQL later, so it's safe to simply return here
5882+
return $args{Query} if @results;
5883+
5884+
if ( lc( $tree->getNodeValue // '' ) eq 'or' ) {
5885+
$args{Query} = "( $args{Query} )";
5886+
}
5887+
58735888
@limits = (
58745889
q{TicketType = 'ticket'},
58755890
qq{ObjectType = '$args{ObjectType}'},
5876-
$args{Query} =~ /^\s*\(.*\)$/ ? $args{Query} : "($args{Query})"
5891+
$args{Query},
58775892
);
58785893
}
58795894
else {

t/transaction/search.t

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,24 @@ is( $txns->Count, 1, 'Found the txn with id limit' );
136136
$txns->FromSQL("id > 10000");
137137
is( $txns->Count, 0, 'No txns with big ids yet' );
138138

139+
diag 'Test HTML::Mason::Commands::PreprocessTransactionSearchQuery';
140+
141+
my %processed = (
142+
q{Type = 'Set'} => q{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND Type = 'Set'},
143+
q{Type = 'Set' OR Type = 'Correspond'} =>
144+
q{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND ( Type = 'Set' OR Type = 'Correspond' )},
145+
q{( Type = 'Set' ) OR ( Type = 'Correspond' )} =>
146+
q{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND ( ( Type = 'Set' ) OR ( Type = 'Correspond' ) )},
147+
q{Type = 'Set' AND Field = 'Status'} =>
148+
q{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND Type = 'Set' AND Field = 'Status'},
149+
q{( Type = 'Set' AND Field = 'Status' ) OR ( Type = 'Correspond' )} =>
150+
q{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND ( ( Type = 'Set' AND Field = 'Status' ) OR ( Type = 'Correspond' ) )},
151+
);
152+
153+
local $HTML::Mason::Commands::session{CurrentUser} = RT->SystemUser;
154+
for my $query ( sort keys %processed ) {
155+
is( HTML::Mason::Commands::PreprocessTransactionSearchQuery( Query => $query ),
156+
$processed{$query}, "Processed query: $query" );
157+
}
158+
139159
done_testing;

t/web/search_txns.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ diag "Query builder";
3535

3636
$m->follow_link_ok( { text => 'Edit Search' }, 'Build Query' );
3737
my $form = $m->form_name('BuildQuery');
38-
is($form->find_input('Query')->value, qq{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND ( TicketId = 1 )});
38+
is($form->find_input('Query')->value, qq{TicketType = 'ticket' AND ObjectType = 'RT::Ticket' AND TicketId = 1});
3939

4040
$m->field( TypeOp => '=' );
4141
$m->field( ValueOfType => 'Create' );

0 commit comments

Comments
 (0)