[Rt-commit] r2681 - in rt/branches/PLATANO-EXPERIMENTAL: . html/Elements html/Search/Elements lib/RT lib/RT/URI lib/t/regression

jesse at bestpractical.com jesse at bestpractical.com
Sat Apr 16 02:48:31 EDT 2005


Author: jesse
Date: Sat Apr 16 02:48:29 2005
New Revision: 2681

Added:
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/SavedSearch.pm
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/SavedSearches.pm
   rt/branches/PLATANO-EXPERIMENTAL/lib/t/regression/20savedsearch.t
Modified:
   rt/branches/PLATANO-EXPERIMENTAL/   (props changed)
   rt/branches/PLATANO-EXPERIMENTAL/config.layout
   rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomField
   rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomFieldText
   rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditLinks
   rt/branches/PLATANO-EXPERIMENTAL/html/Search/Elements/SelectSearchesForObjects
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/CustomFields_Overlay.pm
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Record.pm
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/URI/t.pm
   rt/branches/PLATANO-EXPERIMENTAL/lib/RT/User_Overlay.pm
Log:
 r13019 at hualien:  jesse | 2005-04-16 02:27:57 -0400
  r12961 at hualien:  jesse | 2005-04-15 17:43:00 -0400
   r12947 at hualien:  jesse | 2005-04-15 15:31:30 -0400
    r10706 at hualien (orig r2541):  tla | 2005-03-30 19:20:44 -0500
    Fixes so that custom fields will pay attention to defaults, when the defaults
    are specified.
    
    r10782 at hualien (orig r2542):  robert | 2005-03-30 23:03:13 -0500
     r2579 at dog:  rspier | 2005-03-30 19:58:06 -0800
     RT-Ticket: 6572
     RT-Status: resolved
     RT-Update: correspond
     
     Remove second FHS layout
     
    
    r11405 at hualien (orig r2557):  tla | 2005-04-01 23:16:13 -0500
    A scheme which allows "t:123" URIs, for ticket specification.
    
    r11785 at hualien (orig r2582):  tla | 2005-04-03 15:35:23 -0400
    Get rid of LocalURIPrefix test.  Would make this DTRT if I knew what TRT was.
    
    r11854 at hualien (orig r2584):  tla | 2005-04-04 04:18:22 -0400
    Add a simple sub to return the object class with the RT::Lib:: prefix
    stripped off.  Useful for displaying, e.g., "Ticket #35" or "Article #3".
    
    r11856 at hualien (orig r2586):  tla | 2005-04-04 04:28:02 -0400
    Add a callback for the use of RTFM
    
    r12528 at hualien (orig r2601):  tla | 2005-04-10 19:49:31 -0400
    User_Overlay: added OwnGroups method to get a collection of groups of which
    the user is a member.
    Record: revamped ObjectTypeStr according to suggestions.
    
    
    r12529 at hualien (orig r2602):  tla | 2005-04-10 19:51:16 -0400
    Upped version dependency of DBIx::SearchBuilder to 1.24 for unique records
    feature in CustomFields.
    
    r12530 at hualien (orig r2603):  tla | 2005-04-10 19:51:56 -0400
    UNTESTED first cut at a saved search lib wrapper.
    
    r12549 at hualien (orig r2607):  tla | 2005-04-11 14:47:03 -0400
    Semi-tested, semi-working checkpoint.  Formal tests to follow shortly.
    
    r12760 at hualien (orig r2608):  tla | 2005-04-12 15:29:25 -0400
    SavedSearch now shinily tested.  API also changed somewhat.
    
    r12761 at hualien (orig r2609):  tla | 2005-04-12 15:59:07 -0400
    Made SavedSearches work, added error messages to SavedSearch, updated tests
    
    r12779 at hualien (orig r2617):  tla | 2005-04-12 21:52:54 -0400
    Added delete method & localized strings in SavedSearch
    Made SavedSearches::LimitByPrivacy safe to call multiple times
    
    r12781 at hualien (orig r2619):  autrijus | 2005-04-13 08:13:01 -0400
    * VALUE="1" does not work on checkboxes; this renders the
      default True setting on the "Set to Privliged" checkbox
      in the "New User" screen useless, so new users are defaulted
      to unprivileged even though the code shows that they are
      intended to be so.
    r12782 at hualien (orig r2620):  autrijus | 2005-04-13 11:19:54 -0400
    * Never mind my last change -- I saw "VALUE = 1" and trigger-happily
      inferred that it should default to CHECKED.  Which is, of course,
      not the case.  Reverted.
    r12784 at hualien (orig r2622):  tla | 2005-04-13 12:27:38 -0400
    Lost a curly brace somewhere.  Also noted location of tests for both files.
    
    r12785 at hualien (orig r2623):  tla | 2005-04-13 13:32:41 -0400
    Skeletal inline tests, to make autogen happy.
    
    r12786 at hualien (orig r2624):  tla | 2005-04-13 13:42:51 -0400
    Change to exclude saved searches meant for things other than tickets.
    
   
  
 


Modified: rt/branches/PLATANO-EXPERIMENTAL/config.layout
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/config.layout	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/config.layout	Sat Apr 16 02:48:29 2005
@@ -84,31 +84,6 @@
   customlibdir:		${customdir}/lib
 </Layout>
 
-<Layout FHS>
-  prefix:		/usr/local
-  exec_prefix:		${prefix}
-  bindir:		${prefix}/bin
-  sbindir:		${prefix}/sbin
-  sysconfdir:		/etc+
-  datadir:		${prefix}/share
-# FIXME: missing support for lib64
-  libdir:		${prefix}/lib
-  mandir:		${datadir}/man
-# FIXME: no such directory in FHS; shouldn't go to somewhere in "${datadir}/rt/"?
-  htmldir:		${datadir}/html
-  manualdir:		${datadir}/doc
-  localstatedir:	/var
-  logfiledir:		${localstatedir}/log
-# XXX: "/var/cache/mason/*"?
-  masonstatedir:	${localstatedir}/cache/mason_data
-  sessionstatedir:	${localstatedir}/cache/session_data
-  customdir:		${prefix}/local
-  custometcdir:		${customdir}/etc
-  customhtmldir:	${customdir}/html
-  customlexdir:		${customdir}/po
-  customlibdir:		${customdir}/lib
-</Layout>
-
 <Layout FreeBSD>
   prefix:		/usr/local
   exec_prefix:		${prefix}

Modified: rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomField
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomField	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomField	Sat Apr 16 02:48:29 2005
@@ -53,10 +53,11 @@
 my $Type = $CustomField->Type;
 my $MaxValues = $CustomField->MaxValues;
 if ($MaxValues == 1 and $Object and $Values) {
-    $Default = ($Values->First ? $Values->First->Content : '');
+    # what exactly is this doing?  Without the "unless" it breaks RTFM
+    # transaction extraction into articles.
+    $Default = ($Values->First ? $Values->First->Content : '') unless $Default;
     $Values->GotoFirstItem;
 }
-
 # The "Magic" hidden input causes RT to know that we were trying to edit the field, even if 
 # we don't see a value later, since browsers aren't compelled to submit empty form fields
 $m->out("\n".'<input type="hidden" name="'.$NamePrefix.$CustomField->Id.'-Values-Magic" value="1">'."\n");

Modified: rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomFieldText
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomFieldText	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditCustomFieldText	Sat Apr 16 02:48:29 2005
@@ -47,7 +47,7 @@
 <textarea cols=<%$Cols%> rows=<%$Rows%> name="<%$NamePrefix%><%$CustomField->Id%>-Values"><% $value->Content %></textarea><br>
 % }
 % if (!$MaxValues or !$Values or $Values->Count < $MaxValues) {
-<textarea cols=<%$Cols%> rows=<%$Rows%> name="<%$NamePrefix%><%$CustomField->Id%>-Values"></textarea>
+<textarea cols=<%$Cols%> rows=<%$Rows%> name="<%$NamePrefix%><%$CustomField->Id%>-Values"><% $Default %></textarea>
 % }
 <%INIT>
 # XXX - MultiValue textarea is for now outlawed.

Modified: rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditLinks
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditLinks	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/html/Elements/EditLinks	Sat Apr 16 02:48:29 2005
@@ -114,9 +114,12 @@
 <TD VALIGN=TOP>
 <h3><&|/l&>New Links</&></h3>
 % if (ref($Object) eq 'RT::Ticket') {
-<i><&|/l&>Enter tickets or URIs to link tickets to. Separate multiple entries with spaces.</&></i><br>
+<i><&|/l&>Enter tickets or URIs to link tickets to. Separate multiple entries with spaces.</&>
+<& /Elements/Callback, _CallbackName => 'ExtraLinkInstructions' &>
+</i><br>
 % } elsif (ref($Object) eq 'RT::Queue') {
-<i><&|/l&>Enter queues or URIs to link queues to. Separate multiple entries with spaces.</&></i><br>
+<i><&|/l&>Enter queues or URIs to link queues to. Separate multiple entries with spaces.</&>
+</i><br>
 % } else {
 <i><&|/l&>Enter objects or URIs to link objects to. Separate multiple entries with spaces.</&></i><br>
 % }

Modified: rt/branches/PLATANO-EXPERIMENTAL/html/Search/Elements/SelectSearchesForObjects
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/html/Search/Elements/SelectSearchesForObjects	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/html/Search/Elements/SelectSearchesForObjects	Sat Apr 16 02:48:29 2005
@@ -55,9 +55,11 @@
 <option value=""></option>
 <option value=""><&|/l, $object->Name&>[_1]'s saved searches</&></option>
 % }
-% foreach ($m->comp("SearchesForObject", Object => $object)) { 
-% my ($desc, $search) = @$_;
-<option value="<%ref($object).'-'.$object->Id.'-SavedSearch-'.$search->Id%>"> -<%$desc||loc('Unnamed search')%></option>
+% foreach my $search (@searches) { 
+%     # Skip it if it is not a ticket search.
+%     next if ($search->SubValue('SearchType')
+%              && $search->SubValue('SearchType') ne 'Ticket');
+<option value="<%ref($object)%>-<%$object->id%>-SavedSearch-<%$search->Id%>"> -<%$search->Description||loc('Unnamed search')%></option>
 % }
 % }
 </select>

Modified: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/CustomFields_Overlay.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/lib/RT/CustomFields_Overlay.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/CustomFields_Overlay.pm	Sat Apr 16 02:48:29 2005
@@ -69,6 +69,7 @@
 
 use strict;
 no warnings qw(redefine);
+use DBIx::SearchBuilder::Unique;
 
 
 sub _OCFAlias {

Modified: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Record.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Record.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/Record.pm	Sat Apr 16 02:48:29 2005
@@ -124,6 +124,31 @@
     } 
 }
 
+=head2 ObjectTypeStr
+
+Returns a string which is this object's type.  The type is the class,
+without the "RT::" prefix.
+
+=begin testing
+
+my $ticket = RT::Ticket->new($RT::SystemUser);
+my $group = RT::Group->new($RT::SystemUser);
+is($ticket->ObjectTypeStr, 'Ticket', "Ticket returns correct typestring");
+is($group->ObjectTypeStr, 'Group', "Group returns correct typestring");
+
+=end testing
+
+=cut
+
+sub ObjectTypeStr {
+    my $self = shift;
+    if (ref($self) =~ /^.*::(\w+)$/) {
+	return $self->loc($1);
+    } else {
+	return $self->loc(ref($self));
+    }
+}
+
 =head2 Attributes
 
 Return this object's attributes as an RT::Attributes object

Added: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/SavedSearch.pm
==============================================================================
--- (empty file)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/SavedSearch.pm	Sat Apr 16 02:48:29 2005
@@ -0,0 +1,322 @@
+# BEGIN BPS TAGGED BLOCK {{{
+# 
+# COPYRIGHT:
+#  
+# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
+#                                          <jesse at bestpractical.com>
+# 
+# (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.
+# 
+# 
+# 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
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# 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.
+
+=head1 SYNOPSIS
+
+  use RT::SavedSearch
+
+=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
+
+=cut
+
+package RT::SavedSearch;
+
+use RT::Base;
+use RT::Attribute;
+
+use strict;
+use vars qw/@ISA/;
+ at 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.
+
+=cut
+
+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));
+    }
+
+}
+
+=head2 Save
+
+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)
+
+=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 a hashref with
+the new parameters.
+
+=cut
+
+sub Update {
+    my $self = shift;
+    my $params = shift;
+    
+    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(%{$params});
+    if ($status) {
+	# Update all the accessor variables.
+	return ($status, $self->loc("Search update: [_1]", $msg));
+    }
+}
+
+=head2 Delete
+    
+Deletes the existing search.  Returns a tuple of status and message,
+where status is true upon success.
+
+=cut
+
+sub Delete {
+    my $self = shift;
+
+    my ($status, $msg) = $self->{'Attribute'}->Delete;
+    if ($status) {
+	return (1, $self->loc("Deleted search"));
+    } else {
+	return (0, $self->loc("Delete failed: [_1]", $msg));
+    }
+}
+	
+
+### Accessor methods
+
+=head2 Name
+
+Returns the name of the search.
+
+=cut
+
+sub Name {
+    my $self = shift;
+    return unless ref($self->{'Attribute'}) eq 'RT::Attribute';
+    return $self->{'Attribute'}->Description();
+}
+
+=head2 GetParameter
+
+Returns the given named parameter of the search, e.g. 'Query', 'Format'.
+
+=cut
+
+sub GetParameter {
+    my $self = shift;
+    my $param = shift;
+    return unless ref($self->{'Attribute'}) eq 'RT::Attribute';
+    return $self->{'Attribute'}->SubValue($param);
+}
+
+=head2 Id
+
+Returns the numerical id of this search.
+
+=cut
+
+sub Id {
+     my $self = shift;
+     return $self->{'Id'};
+}
+
+=head2 Privacy
+
+Returns the principal object to whom this search belongs, in a string
+"<class>-<id>", e.g. "RT::Group-16".
+
+=cut
+
+sub Privacy {
+    my $self = shift;
+    return $self->{'Privacy'};
+}
+
+=head2 Type
+
+Returns the type of this search, e.g. 'Ticket'.  Useful for denoting the
+saved searches that are relevant to a particular search page.
+
+=cut
+
+sub Type {
+    my $self = shift;
+    return $self->{'Type'};
+}
+
+### Internal methods
+
+# _GetObject: helper routine to load the correct object whose parameters
+#  have been passed.
+
+sub _GetObject {
+    my $self = shift;
+    my $privacy = shift;
+
+    my ($obj_type, $obj_id) = split(/\-/, $privacy);
+    unless ($obj_type eq 'RT::User' || $obj_type eq 'RT::Group') {
+	$RT::Logger->error("Tried to load a search belonging to an $obj_type, which is neither a user nor a group");
+	return undef;
+    }
+
+    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->error("Permission denied for user other than self");
+	return undef;
+    }
+    if ($obj_type eq 'RT::Group' &&
+	!$object->HasMemberRecursively($self->CurrentUser->PrincipalObj)) {
+	$RT::Logger->error("Permission denied, ".$self->CurrentUser->Name.
+			   " is not a member of group");
+	return undef;
+    }
+
+    return $object;
+}
+
+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});
+
+1;

Added: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/SavedSearches.pm
==============================================================================
--- (empty file)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/SavedSearches.pm	Sat Apr 16 02:48:29 2005
@@ -0,0 +1,205 @@
+# BEGIN BPS TAGGED BLOCK {{{
+# 
+# COPYRIGHT:
+#  
+# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
+#                                          <jesse at bestpractical.com>
+# 
+# (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.
+# 
+# 
+# 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
+# you are the copyright holder for those contributions and you grant
+# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+# 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::SavedSearches - a pseudo-collection for SavedSearch objects.
+
+=head1 SYNOPSIS
+
+  use RT::SavedSearch
+
+=head1 DESCRIPTION
+
+  SavedSearches is an object consisting of a number of SavedSearch objects.
+  It works more or less like a DBIx::SearchBuilder collection, although it
+  is not.
+
+=head1 METHODS
+
+=begin testing
+
+use_ok(RT::SavedSearches);
+
+# The real tests are in lib/t/20savedsearch.t
+
+=end testing
+
+=cut
+
+package RT::SavedSearches;
+
+use RT::Base;
+use RT::SavedSearch;
+
+use strict;
+use vars qw/@ISA/;
+ at ISA = qw/RT::Base/;
+
+sub new  {
+    my $proto = shift;
+    my $class = ref($proto) || $proto;
+    my $self  = {};
+    bless ($self, $class);
+    $self->CurrentUser(@_);
+    $self->{'idx'} = 0;
+    $self->{'objects'} = [];
+    return $self;
+}
+
+=head2 LimitToPrivacy
+
+Takes two argumets: a privacy string, of the format "<class>-<id>", as
+produced by RT::SavedSearch::Privacy(); and a type string, as produced
+by RT::SavedSearch::Type().  The SavedSearches object will load the
+searches belonging to that user or group that are of the type
+specified.  If no type is specified, all the searches belonging to the
+user/group will be loaded.  Repeated calls to the same object should DTRT.
+
+=cut
+
+sub LimitToPrivacy {
+    my $self = shift;
+    my $privacy = shift;
+    my $type = shift;
+
+    my $object = $self->_GetObject($privacy);
+
+    if ($object) {
+	$self->{'objects'} = [];
+	my @search_atts = $object->Attributes->Named('SavedSearch');
+	foreach my $att (@search_atts) {
+	    my $search = RT::SavedSearch->new($self->CurrentUser);
+	    $search->Load($privacy, $att->Id);
+	    next if $type && $search->Type ne $type;
+	    push(@{$self->{'objects'}}, $search);
+	}
+    } else {
+	$RT::Logger->error("Could not load object $privacy");
+    }
+}
+
+### Accessor methods
+
+=head2 Next
+
+Returns the next object in the collection.
+
+=cut
+
+sub Next {
+    my $self = shift;
+    my $search = $self->{'objects'}->[$self->{'idx'}];
+    if ($search) {
+	$self->{'idx'}++;
+    } else {
+	# We have run out of objects; reset the counter.
+	$self->{'idx'} = 0;
+    }
+    return $search;
+}
+
+=head2 Count
+
+Returns the number of search objects found.
+
+=cut
+
+sub Count {
+    my $self = shift;
+    return scalar @{$self->{'objects'}};
+}
+
+### Internal methods
+
+# _GetObject: helper routine to load the correct object whose parameters
+#  have been passed.
+
+sub _GetObject {
+    my $self = shift;
+    my $privacy = shift;
+
+    my ($obj_type, $obj_id) = split(/\-/, $privacy);
+    unless ($obj_type eq 'RT::User' || $obj_type eq 'RT::Group') {
+	$RT::Logger->error("Tried to load a search belonging to an $obj_type, which is neither a user nor a group");
+	return undef;
+    }
+
+    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->error('Requested user ' . $object->Id 
+			   . 'is not current user');
+	return undef;
+    }
+    if ($obj_type eq 'RT::Group'
+	&& !$object->HasMemberRecursively($self->CurrentUser->PrincipalObj)) {
+	$RT::Logger->error('Current user does not belong to requested group ' 
+			   . $object->Id);
+	return undef;
+    }
+
+    return $object;
+}
+
+eval "require RT::SavedSearches_Vendor";
+die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearches_Vendor.pm});
+eval "require RT::SavedSearches_Local";
+die $@ if ($@ && $@ !~ qr{^Can't locate RT/SavedSearches_Local.pm});
+
+1;

Modified: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/URI/t.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/lib/RT/URI/t.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/URI/t.pm	Sat Apr 16 02:48:29 2005
@@ -42,8 +42,6 @@
 use_ok("RT::URI::t");
 my $uri = RT::URI::t->new($RT::SystemUser);
 ok(ref($uri), "URI object exists");
-is($uri->LocalURIPrefix, 't://example.com', 
-   "URI has correct local prefix");
 
 my $uristr = "t:1";
 $uri->ParseURI($uristr);

Modified: rt/branches/PLATANO-EXPERIMENTAL/lib/RT/User_Overlay.pm
==============================================================================
--- rt/branches/PLATANO-EXPERIMENTAL/lib/RT/User_Overlay.pm	(original)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/RT/User_Overlay.pm	Sat Apr 16 02:48:29 2005
@@ -1265,6 +1265,26 @@
 
 # }}}
 
+# {{{ sub OwnGroups 
+
+=head2 OwnGroups
+
+Returns a group collection object containing the groups of which this
+user is a member.
+
+=cut
+
+sub OwnGroups {
+    my $self = shift;
+    my $groups = RT::Groups->new($self->CurrentUser);
+    $groups->LimitToUserDefinedGroups;
+    $groups->WithMember(PrincipalId => $self->Id, 
+			Recursively => 1);
+    return $groups;
+}
+
+# }}}
+
 # {{{ sub Rights testing
 
 =head2 Rights testing

Added: rt/branches/PLATANO-EXPERIMENTAL/lib/t/regression/20savedsearch.t
==============================================================================
--- (empty file)
+++ rt/branches/PLATANO-EXPERIMENTAL/lib/t/regression/20savedsearch.t	Sat Apr 16 02:48:29 2005
@@ -0,0 +1,180 @@
+use RT;
+use Test::More qw/no_plan/;
+use RT::User;
+use RT::Group;
+use RT::Ticket;
+use RT::Queue;
+
+use_ok(RT::SavedSearch);
+use_ok(RT::SavedSearches);
+
+RT::LoadConfig();
+RT::Init();
+
+# Set up some infrastructure.  These calls are tested elsewhere.
+
+my $searchuser = RT::User->new($RT::SystemUser);
+my ($ret, $msg) = $searchuser->Create(Name => 'searchuser'.$$,
+		    Privileged => 1,
+		    EmailAddress => "searchuser\@p$$.example.com",
+		    RealName => 'Search user');
+ok($ret, "created searchuser: $msg");
+$searchuser->PrincipalObj->GrantRight(Right => 'LoadSavedSearch');
+$searchuser->PrincipalObj->GrantRight(Right => 'CreateSavedSearch');
+$searchuser->PrincipalObj->GrantRight(Right => 'ModifySelf');
+
+# This is the group whose searches searchuser should be able to see.
+my $ingroup = RT::Group->new($RT::SystemUser);
+$ingroup->CreateUserDefinedGroup(Name => 'searchgroup1'.$$);
+$ingroup->AddMember($searchuser->Id);
+$searchuser->PrincipalObj->GrantRight(Right => 'EditSavedSearches',
+				      Object => $ingroup);
+$searchuser->PrincipalObj->GrantRight(Right => 'ShowSavedSearches',
+				      Object => $ingroup);
+
+# This is the group whose searches searchuser should not be able to see.
+my $outgroup = RT::Group->new($RT::SystemUser);
+$outgroup->CreateUserDefinedGroup(Name => 'searchgroup2'.$$);
+$outgroup->AddMember($RT::SystemUser->Id);
+
+my $queue = RT::Queue->new($RT::SystemUser);
+$queue->Create(Name => 'SearchQueue'.$$);
+$searchuser->PrincipalObj->GrantRight(Right => 'SeeQueue', Object => $queue);
+$searchuser->PrincipalObj->GrantRight(Right => 'ShowTicket', Object => $queue);
+$searchuser->PrincipalObj->GrantRight(Right => 'OwnTicket', Object => $queue);
+
+
+my $ticket = RT::Ticket->new($RT::SystemUser);
+$ticket->Create(Queue => $queue->Id,
+		Requestor => [ $searchuser->Name ],
+		Owner => $searchuser,
+		Subject => 'saved search test');
+
+
+# Now start the search madness.
+my $curruser = RT::CurrentUser->new($searchuser);
+my $format = '\'   <b><a href="/Ticket/Display.html?id=__id__">__id__</a></b>/TITLE:#\',
+\'<b><a href="/Ticket/Display.html?id=__id__">__Subject__</a></b>/TITLE:Subject\',
+\'__Status__\',
+\'__QueueName__\',
+\'__OwnerName__\',
+\'__Priority__\',
+\'__NEWLINE__\',
+\'\',
+\'<small>__Requestors__</small>\',
+\'<small>__CreatedRelative__</small>\',
+\'<small>__ToldRelative__</small>\',
+\'<small>__LastUpdatedRelative__</small>\',
+\'<small>__TimeLeft__</small>\'';
+
+my ($ret, $msg);
+my $mysearch = RT::SavedSearch->new($curruser);
+($ret, $msg) = $mysearch->Save(Privacy => 'RT::User-' . $searchuser->Id,
+			       Type => 'Ticket',
+			       Name => 'owned by me',
+			       SearchParams => {'Format' => $format,
+						'Query' => "Owner = '" 
+						    . $searchuser->Name 
+						    . "'"});
+ok($ret, "mysearch was created");
+
+
+my $groupsearch = RT::SavedSearch->new($curruser);
+($ret, $msg) = $groupsearch->Save(Privacy => 'RT::Group-' . $ingroup->Id,
+				  Type => 'Ticket',
+				  Name => 'search queue',
+				  SearchParams => {'Format' => $format,
+						   'Query' => "Queue = '"
+						       . $queue->Name . "'"});
+ok($ret, "groupsearch was created");
+
+my $othersearch = RT::SavedSearch->new($curruser);
+($ret, $msg) = $othersearch->Save(Privacy => 'RT::Group-' . $outgroup->Id,
+				  Type => 'Ticket',
+				  Name => 'searchuser requested',
+				  SearchParams => {'Format' => $format,
+						   'Query' => 
+						       "Requestor.Name LIKE 'search'"});
+ok(!$ret, "othersearch NOT created");
+like($msg, qr/Failed to load object for/, "...for the right reason");
+
+$othersearch = RT::SavedSearch->new($RT::SystemUser);
+($ret, $msg) = $othersearch->Save(Privacy => 'RT::Group-' . $outgroup->Id,
+				  Type => 'Ticket',
+				  Name => 'searchuser requested',
+				  SearchParams => {'Format' => $format,
+						   'Query' => 
+						       "Requestor.Name LIKE 'search'"});
+ok($ret, "othersearch created by systemuser");
+
+# Now try to load some searches.
+
+# This should work.
+my $loadedsearch1 = RT::SavedSearch->new($curruser);
+$loadedsearch1->Load('RT::User-'.$curruser->Id, $mysearch->Id);
+is($loadedsearch1->Id, $mysearch->Id, "Loaded mysearch");
+like($loadedsearch1->GetParameter('Query'), qr/Owner/, 
+     "Retrieved query of mysearch");
+# Check through the other accessor methods.
+is($loadedsearch1->Privacy, 'RT::User-' . $curruser->Id,
+   "Privacy of mysearch correct");
+is($loadedsearch1->Name, 'owned by me', "Name of mysearch correct");
+is($loadedsearch1->Type, 'Ticket', "Type of mysearch correct");
+
+# See if it can be used to search for tickets.
+my $tickets = RT::Tickets->new($curruser);
+$tickets->FromSQL($loadedsearch1->GetParameter('Query'));
+is($tickets->Count, 1, "Found a ticket");
+
+# This should fail -- wrong object.
+# my $loadedsearch2 = RT::SavedSearch->new($curruser);
+# $loadedsearch2->Load('RT::User-'.$curruser->Id, $groupsearch->Id);
+# isnt($loadedsearch2->Id, $othersearch->Id, "Didn't load groupsearch as mine");
+# ...but this should succeed.
+my $loadedsearch3 = RT::SavedSearch->new($curruser);
+$loadedsearch3->Load('RT::Group-'.$ingroup->Id, $groupsearch->Id);
+is($loadedsearch3->Id, $groupsearch->Id, "Loaded groupsearch");
+like($loadedsearch3->GetParameter('Query'), qr/Queue/,
+     "Retrieved query of groupsearch");
+# Can it get tickets?
+$tickets = RT::Tickets->new($curruser);
+$tickets->FromSQL($loadedsearch3->GetParameter('Query'));
+is($tickets->Count, 1, "Found a ticket");
+
+# This should fail -- no permission.
+my $loadedsearch4 = RT::SavedSearch->new($curruser);
+$loadedsearch4->Load($othersearch->Privacy, $othersearch->Id);
+isnt($loadedsearch4->Id, $othersearch->Id, "Did not load othersearch");
+
+# Try to update an existing search.
+$loadedsearch1->Update({'Format' => $format,
+			'Query' => "Queue = '" . $queue->Name . "'" });
+like($loadedsearch1->GetParameter('Query'), qr/Queue/,
+     "Updated mysearch parameter");
+is($loadedsearch1->Type, 'Ticket', "mysearch is still for tickets");
+is($loadedsearch1->Privacy, 'RT::User-'.$curruser->Id,
+   "mysearch still belongs to searchuser");
+like($mysearch->GetParameter('Query'), qr/Queue/, "other mysearch object updated");
+
+
+## Right ho.  Test the pseudo-collection object.
+
+my $genericsearch = RT::SavedSearch->new($curruser);
+$genericsearch->Save(Name => 'generic search',
+		     Type => 'all',
+		     SearchParams => {'Query' => "Queue = 'General'"});
+
+my $ticketsearches = RT::SavedSearches->new($curruser);
+$ticketsearches->LimitToPrivacy('RT::User-'.$curruser->Id, 'Ticket');
+is($ticketsearches->Count, 1, "Found searchuser's ticket searches");
+
+my $allsearches = RT::SavedSearches->new($curruser);
+$allsearches->LimitToPrivacy('RT::User-'.$curruser->Id);
+is($allsearches->Count, 2, "Found all searchuser's searches");
+
+# Delete a search.
+($ret, $msg) = $genericsearch->Delete;
+ok($ret, "Deleted genericsearch");
+$allsearches->LimitToPrivacy('RT::User-'.$curruser->Id);
+is($allsearches->Count, 1, "Found all searchuser's searches after deletion");
+


More information about the Rt-commit mailing list