X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FSavedSearch.pm;h=4dd869bf2492ae48ae5e98545d3aded0431d10c2;hp=65411a7b2f08800c18cac6bf2007ab77c907c480;hb=919e930aa9279b3c5cd12b593889cd6de79d67bf;hpb=d4d0590bef31071e8809ec046717444b95b3f30a diff --git a/rt/lib/RT/SavedSearch.pm b/rt/lib/RT/SavedSearch.pm index 65411a7b2..4dd869bf2 100644 --- a/rt/lib/RT/SavedSearch.pm +++ b/rt/lib/RT/SavedSearch.pm @@ -1,38 +1,40 @@ # BEGIN BPS TAGGED BLOCK {{{ -# +# # COPYRIGHT: -# -# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC -# -# +# +# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC +# +# # (Except where explicitly superseded by other copyright notices) -# -# +# +# # LICENSE: -# +# # This work is made available to you under the terms of Version 2 of # the GNU General Public License. A copy of that license should have # been provided with this software, but in any event can be snarfed # from www.gnu.org. -# +# # This work is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. -# +# # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software -# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -# -# +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 or visit their web page on the internet at +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. +# +# # CONTRIBUTION SUBMISSION POLICY: -# +# # (The following paragraph is not intended to limit the rights granted # to you to modify and distribute this software under the terms of # the GNU General Public License and is only of importance to you if # you choose to contribute your changes and enhancements to the # community by submitting them to Best Practical Solutions, LLC.) -# +# # By intentionally submitting any modifications, corrections or # derivatives to this work, or any other work intended for use with # Request Tracker, to Best Practical Solutions, LLC, you confirm that @@ -41,12 +43,12 @@ # royalty-free, perpetual, license to use, copy, create derivative # works based on those contributions, and sublicense and distribute # those contributions and any derivatives thereof. -# +# # END BPS TAGGED BLOCK }}} =head1 NAME - RT::SavedSearch - an API for saving and retrieving search form values. +RT::SavedSearch - an API for saving and retrieving search form values. =head1 SYNOPSIS @@ -54,213 +56,85 @@ =head1 DESCRIPTION - SavedSearch is an object that can belong to either an RT::User or an - RT::Group. It consists of an ID, a description, and a number of - search parameters. - -=head1 METHODS - -=begin testing - -use_ok(RT::SavedSearch); - -# Real tests are in lib/t/20savedsearch.t - -=end testing +SavedSearch is an object based on L that can belong +to either an L or an L. It consists of an ID, +a description, and a number of search parameters. =cut package RT::SavedSearch; -use RT::Base; -use RT::Attribute; - use strict; -use vars qw/@ISA/; -@ISA = qw/RT::Base/; - -sub new { - my $proto = shift; - my $class = ref($proto) || $proto; - my $self = {}; - $self->{'Id'} = 0; - bless ($self, $class); - $self->CurrentUser(@_); - return $self; -} - -=head2 Load - -Takes a privacy specification, an object ID, and a search ID. Loads -the given search ID if it belongs to the stated user or group. -Returns a tuple of status and message, where status is true on -success. +use warnings; -=cut +use base qw/RT::SharedSetting/; -sub Load { - my $self = shift; - my ($privacy, $id) = @_; - my $object = $self->_GetObject($privacy); - - if ($object) { - $self->{'Attribute'} = $object->Attributes->WithId($id); - if ($self->{'Attribute'}->Id) { - $self->{'Id'} = $self->{'Attribute'}->Id; - $self->{'Privacy'} = $privacy; - $self->{'Type'} = $self->{'Attribute'}->SubValue('SearchType'); - return (1, $self->loc("Loaded search [_1]", $self->Name)); - } else { - $RT::Logger->error("Could not load attribute " . $id - . " for object " . $privacy); - return (0, $self->loc("Search attribute load failure")); - } - } else { - $RT::Logger->error("Could not load object $privacy when loading search"); - return (0, $self->loc("Could not load object for [_1]", $privacy)); - } - -} +=head1 METHODS -=head2 Save +=head2 ObjectName -Takes a privacy, an optional type, a name, and a hashref containing the -search parameters. Saves the given parameters to the appropriate user/ -group object, and loads the resulting search. Returns a tuple of status -and message, where status is true on success. Defaults are: - Privacy: undef - Type: Ticket - Name: "new search" - SearchParams: (empty hash) +An object of this class is called "search" =cut -sub Save { - my $self = shift; - my %args = ('Privacy' => 'RT::User-' . $self->CurrentUser->Id, - 'Type' => 'Ticket', - 'Name' => 'new search', - 'SearchParams' => {}, - @_); - my $privacy = $args{'Privacy'}; - my $type = $args{'Type'}; - my $name = $args{'Name'}; - my %params = %{$args{'SearchParams'}}; - - $params{'SearchType'} = $type; - my $object = $self->_GetObject($privacy); - if ($object) { - my ($att_id, $att_msg) = $object->AddAttribute( - 'Name' => 'SavedSearch', - 'Description' => $name, - 'Content' => \%params); - if ($att_id) { - $self->{'Attribute'} = $object->Attributes->WithId($att_id); - $self->{'Id'} = $att_id; - $self->{'Privacy'} = $privacy; - $self->{'Type'} = $type; - return (1, $self->loc("Saved search [_1]", $name)); - } else { - $RT::Logger->error("SavedSearch save failure: $att_msg"); - return (0, $self->loc("Failed to create search attribute")); - } - } else { - return (0, $self->loc("Failed to load object for [_1]", $privacy)); - } -} - -=head2 Update - -Updates the parameters of an existing search. Takes the arguments -"Name" and "SearchParams"; SearchParams should be a hashref containing -the new parameters of the search. If Name is not specified, the name -will not be changed. - -=cut +sub ObjectName { "search" } # loc -sub Update { +sub PostLoad { my $self = shift; - my %args = ('Name' => '', - 'SearchParams' => {}, - @_); - - return(0, $self->loc("No search loaded")) unless $self->Id; - return(0, $self->loc("Could not load search attribute")) - unless $self->{'Attribute'}->Id; - my ($status, $msg) = $self->{'Attribute'}->SetSubValues(%{$args{'SearchParams'}}); - if ($status && $args{'Name'}) { - ($status, $msg) = $self->{'Attribute'}->SetDescription($args{'Name'}); - } - return ($status, $self->loc("Search update: [_1]", $msg)); + $self->{'Type'} = $self->{'Attribute'}->SubValue('SearchType'); } -=head2 Delete - -Deletes the existing search. Returns a tuple of status and message, -where status is true upon success. +sub SaveAttribute { + my $self = shift; + my $object = shift; + my $args = shift; -=cut + my $params = $args->{'SearchParams'}; -sub Delete { - my $self = shift; + $params->{'SearchType'} = $args->{'Type'} || 'Ticket'; - my ($status, $msg) = $self->{'Attribute'}->Delete; - if ($status) { - return (1, $self->loc("Deleted search")); - } else { - return (0, $self->loc("Delete failed: [_1]", $msg)); - } + return $object->AddAttribute( + 'Name' => 'SavedSearch', + 'Description' => $args->{'Name'}, + 'Content' => $params, + ); } - - -### Accessor methods - -=head2 Name -Returns the name of the search. -=cut - -sub Name { +sub UpdateAttribute { my $self = shift; - return unless ref($self->{'Attribute'}) eq 'RT::Attribute'; - return $self->{'Attribute'}->Description(); -} + my $args = shift; + my $params = $args->{'SearchParams'} || {}; -=head2 GetParameter + my ($status, $msg) = $self->{'Attribute'}->SetSubValues(%$params); -Returns the given named parameter of the search, e.g. 'Query', 'Format'. - -=cut + if ($status && $args->{'Name'}) { + ($status, $msg) = $self->{'Attribute'}->SetDescription($args->{'Name'}); + } -sub GetParameter { - my $self = shift; - my $param = shift; - return unless ref($self->{'Attribute'}) eq 'RT::Attribute'; - return $self->{'Attribute'}->SubValue($param); + return ($status, $msg); } -=head2 Id +=head2 RT::SavedSearch->EscapeDescription STRING -Returns the numerical id of this search. +This is a class method because system-level saved searches aren't true +C objects but direct C objects. -=cut - -sub Id { - my $self = shift; - return $self->{'Id'}; -} - -=head2 Privacy - -Returns the principal object to whom this search belongs, in a string -"-", e.g. "RT::Group-16". +Returns C with all square brackets except those in C<[_1]> escaped, +ready for passing as the first argument to C. =cut -sub Privacy { +sub EscapeDescription { my $self = shift; - return $self->{'Privacy'}; + my $desc = shift; + if ($desc) { + # We only use [_1] in saved search descriptions, so let's escape other "[" + # and "]" unless they are escaped already. + $desc =~ s/(?error("Tried to load a search belonging to an $obj_type, which is neither a user nor a group"); - return undef; +# _PrivacyObjects: returns a list of objects that can be used to load, create, +# etc. saved searches from. You probably want to use the wrapper methods like +# ObjectsForLoading, ObjectsForCreating, etc. + +sub _PrivacyObjects { + my $self = shift; + my ($has_attr) = @_; + my $CurrentUser = $self->CurrentUser; + + my $groups = RT::Groups->new($CurrentUser); + $groups->LimitToUserDefinedGroups; + $groups->WithCurrentUser; + if ($has_attr) { + my $attrs = $groups->Join( + ALIAS1 => 'main', + FIELD1 => 'id', + TABLE2 => 'Attributes', + FIELD2 => 'ObjectId', + ); + $groups->Limit( + ALIAS => $attrs, + FIELD => 'ObjectType', + VALUE => 'RT::Group', + ); + $groups->Limit( + ALIAS => $attrs, + FIELD => 'Name', + VALUE => $has_attr, + ); } - my $object; - eval " - require $obj_type; - \$object = $obj_type->new(\$self->CurrentUser); - \$object->Load(\$obj_id); - "; - unless (ref($object) eq $obj_type) { - $RT::Logger->error("Could not load object of type $obj_type with ID $obj_id"); - return undef; - } - - # Do not allow the loading of a user object other than the current - # user, or of a group object of which the current user is not a member. - - if ($obj_type eq 'RT::User' - && $object->Id != $self->CurrentUser->UserObj->Id()) { - $RT::Logger->debug("Permission denied for user other than self"); - return undef; - } - if ($obj_type eq 'RT::Group' && - !$object->HasMemberRecursively($self->CurrentUser->PrincipalObj)) { - $RT::Logger->debug("Permission denied, ".$self->CurrentUser->Name. - " is not a member of group"); - return undef; - } + return ( $CurrentUser->UserObj, @{ $groups->ItemsArrayRef() } ); +} - return $object; +sub ObjectsForLoading { + my $self = shift; + return grep { $self->CurrentUserCanSee($_) } $self->_PrivacyObjects( "SavedSearch" ); } -eval "require RT::SavedSearch_Vendor"; -die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearch_Vendor.pm}); -eval "require RT::SavedSearch_Local"; -die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearch_Local.pm}); +RT::Base->_ImportOverlays(); 1;