1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
30 # CONTRIBUTION SUBMISSION POLICY:
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
47 # END BPS TAGGED BLOCK }}}
51 RT::SearchBuilder - a baseclass for RT collection objects
65 package RT::SearchBuilder;
68 use DBIx::SearchBuilder "1.50";
74 use base qw(DBIx::SearchBuilder RT::Base);
79 $self->{'user'} = shift;
80 unless(defined($self->CurrentUser)) {
82 Carp::confess("$self was created without a CurrentUser");
83 $RT::Logger->err("$self was created without a CurrentUser");
86 $self->SUPER::_Init( 'Handle' => $RT::Handle);
91 $self->{'_sql_aliases'} = {};
92 return $self->SUPER::CleanSlate(@_);
95 sub JoinTransactions {
97 my %args = ( New => 0, @_ );
99 return $self->{'_sql_aliases'}{'transactions'}
100 if !$args{'New'} && $self->{'_sql_aliases'}{'transactions'};
102 my $alias = $self->Join(
105 TABLE2 => 'Transactions',
106 FIELD2 => 'ObjectId',
109 my $item = $self->NewItem;
110 my $object_type = $item->can('ObjectType') ? $item->ObjectType : ref $item;
112 $self->RT::SearchBuilder::Limit(
114 FIELD => 'ObjectType',
115 VALUE => $object_type,
117 $self->{'_sql_aliases'}{'transactions'} = $alias
127 next if defined $s->{FIELD} and $s->{FIELD} =~ /\W/;
128 $s->{FIELD} = $s->{FUNCTION} if $s->{FUNCTION};
131 return $self->SUPER::OrderByCols( @sort );
134 # If we're setting RowsPerPage or FirstRow, ensure we get a natural number or undef.
137 return if @_ and defined $_[0] and $_[0] =~ /\D/;
138 return $self->SUPER::RowsPerPage(@_);
143 return if @_ and defined $_[0] and $_[0] =~ /\D/;
144 return $self->SUPER::FirstRow(@_);
147 =head2 LimitToEnabled
149 Only find items that haven't been disabled
156 $self->{'handled_disabled_column'} = 1;
157 $self->Limit( FIELD => 'Disabled', VALUE => '0' );
160 =head2 LimitToDeleted
162 Only find items that have been deleted.
169 $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
170 $self->Limit( FIELD => 'Disabled', VALUE => '1' );
175 Find all matching rows, regardless of whether they are disabled or not
180 shift->{'find_disabled_rows'} = 1;
183 =head2 LimitCustomField
185 Takes a paramhash of key/value pairs with the following keys:
189 =item CUSTOMFIELD - CustomField id. Optional
191 =item OPERATOR - The usual Limit operators
193 =item VALUE - The value to compare against
201 my $class = ref($self);
202 $class =~ s/s$// or die "Cannot deduce SingularClass for $class";
206 sub LimitCustomField {
208 my %args = ( VALUE => undef,
209 CUSTOMFIELD => undef,
213 my $alias = $self->Join(
217 TABLE2 => 'ObjectCustomFieldValues',
222 FIELD => 'CustomField',
224 VALUE => $args{'CUSTOMFIELD'},
225 ) if ($args{'CUSTOMFIELD'});
228 FIELD => 'ObjectType',
230 VALUE => $self->_SingularClass,
235 OPERATOR => $args{'OPERATOR'},
236 VALUE => $args{'VALUE'},
240 =head2 Limit PARAMHASH
242 This Limit sub calls SUPER::Limit, but defaults "CASESENSITIVE" to 1, thus
243 making sure that by default lots of things don't do extra work trying to
244 match lower(colname) agaist lc($val);
246 We also force VALUE to C<NULL> when the OPERATOR is C<IS> or C<IS NOT>.
247 This ensures that we don't pass invalid SQL to the database or allow SQL
248 injection attacks when we pass through user specified values.
260 # We use the same regex here that DBIx::SearchBuilder uses to exclude
261 # values from quoting
262 if ( $ARGS{'OPERATOR'} =~ /IS/i ) {
263 # Don't pass anything but NULL for IS and IS NOT
264 $ARGS{'VALUE'} = 'NULL';
267 if ($ARGS{FUNCTION}) {
268 ($ARGS{ALIAS}, $ARGS{FIELD}) = split /\./, delete $ARGS{FUNCTION}, 2;
269 $self->SUPER::Limit(%ARGS);
270 } elsif ($ARGS{FIELD} =~ /\W/
271 or $ARGS{OPERATOR} !~ /^(=|<|>|!=|<>|<=|>=
273 |(NOT\s*)?(STARTS|ENDS)WITH
278 $RT::Logger->crit("Possible SQL injection attack: $ARGS{FIELD} $ARGS{OPERATOR}");
286 $self->SUPER::Limit(%ARGS);
292 If it has a SortOrder attribute, sort the array by SortOrder.
293 Otherwise, if it has a "Name" attribute, sort alphabetically by Name
294 Otherwise, just give up and return it in the order it came from the
303 if ($self->NewItem()->_Accessible('SortOrder','read')) {
304 $items = [ sort { $a->SortOrder <=> $b->SortOrder } @{$items} ];
306 elsif ($self->NewItem()->_Accessible('Name','read')) {
307 $items = [ sort { lc($a->Name) cmp lc($b->Name) } @{$items} ];
315 Return this object's ItemsArray, in the order that ItemsOrderBy sorts
322 return $self->ItemsOrderBy($self->SUPER::ItemsArrayRef());
325 # make sure that Disabled rows never get seen unless
326 # we're explicitly trying to see them.
331 if ( $self->{'with_disabled_column'}
332 && !$self->{'handled_disabled_column'}
333 && !$self->{'find_disabled_rows'}
335 $self->LimitToEnabled;
337 return $self->SUPER::_DoSearch(@_);
342 if ( $self->{'with_disabled_column'}
343 && !$self->{'handled_disabled_column'}
344 && !$self->{'find_disabled_rows'}
346 $self->LimitToEnabled;
348 return $self->SUPER::_DoCount(@_);
351 =head2 ColumnMapClassName
353 ColumnMap needs a Collection name to load the correct list display.
354 Depluralization is hard, so provide an easy way to correct the naive
355 algorithm that this code uses.
359 sub ColumnMapClassName {
361 my $Class = ref $self;
367 RT::Base->_ImportOverlays();