This commit was generated by cvs2svn to compensate for changes in r4407,
[freeside.git] / rt / lib / RT / Queue_Overlay.pm
index 4eb265f..2f180fc 100644 (file)
@@ -1,8 +1,14 @@
-# BEGIN LICENSE BLOCK
+# BEGIN BPS TAGGED BLOCK {{{
 # 
-# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
+# COPYRIGHT:
+#  
+# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC 
+#                                          <jesse@bestpractical.com>
 # 
-# (Except where explictly superceded by other copyright notices)
+# (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
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 # General Public License for more details.
 # 
-# Unless otherwise specified, all modifications, corrections or
-# extensions to this work which alter its source code become the
-# property of Best Practical Solutions, LLC when submitted for
-# inclusion in the work.
+# 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 LICENSE BLOCK
+# END BPS TAGGED BLOCK }}}
+
 =head1 NAME
 
   RT::Queue - an RT Queue object
@@ -42,17 +65,20 @@ use RT::Queue;
 
 =cut
 
+
+package RT::Queue;
+
 use strict;
 no warnings qw(redefine);
 
-use vars qw(@STATUS @ACTIVE_STATUS @INACTIVE_STATUS $RIGHTS);
+use vars qw(@DEFAULT_ACTIVE_STATUS @DEFAULT_INACTIVE_STATUS $RIGHTS);
+
 use RT::Groups;
 use RT::ACL;
+use RT::Interface::Email;
 
-
-@ACTIVE_STATUS = qw(new open stalled);
-@INACTIVE_STATUS = qw(resolved rejected deleted);
-@STATUS = (@ACTIVE_STATUS, @INACTIVE_STATUS);
+@DEFAULT_ACTIVE_STATUS = qw(new open stalled);
+@DEFAULT_INACTIVE_STATUS = qw(resolved rejected deleted);  
 
 # $self->loc('new'); # For the string extractor to get a string to localize
 # $self->loc('open'); # For the string extractor to get a string to localize
@@ -68,15 +94,16 @@ $RIGHTS = {
     ShowACL             => 'Display Access Control List',             # loc_pair
     ModifyACL           => 'Modify Access Control List',              # loc_pair
     ModifyQueueWatchers => 'Modify the queue watchers',               # loc_pair
-    AdminCustomFields   => 'Create, delete and modify custom fields', # loc_pair
+    AssignCustomFields  => 'Assign and remove custom fields',         # loc_pair
     ModifyTemplate      => 'Modify Scrip templates for this queue',   # loc_pair
     ShowTemplate        => 'Display Scrip templates for this queue',  # loc_pair
 
     ModifyScrips => 'Modify Scrips for this queue',                   # loc_pair
     ShowScrips   => 'Display Scrips for this queue',                  # loc_pair
 
-    ShowTicket         => 'Show ticket summaries',                    # loc_pair
-    ShowTicketComments => 'Show ticket private commentary',           # loc_pair
+    ShowTicket         => 'See ticket summaries',                    # loc_pair
+    ShowTicketComments => 'See ticket private commentary',           # loc_pair
+    ShowOutgoingEmail => 'See exact outgoing email messages and their recipeients',           # loc_pair
 
     Watch => 'Sign up as a ticket Requestor or ticket or queue Cc',   # loc_pair
     WatchAsAdminCc  => 'Sign up as a ticket or queue AdminCc',        # loc_pair
@@ -102,6 +129,39 @@ foreach my $right ( keys %{$RIGHTS} ) {
 }
     
 
+sub AddLink {
+    my $self = shift;
+    my %args = ( Target => '',
+                 Base   => '',
+                 Type   => '',
+                 Silent => undef,
+                 @_ );
+
+    unless ( $self->CurrentUserHasRight('ModifyQueue') ) {
+        return ( 0, $self->loc("Permission Denied") );
+    }
+
+    return $self->SUPER::_AddLink(%args);
+}
+
+sub DeleteLink {
+    my $self = shift;
+    my %args = (
+        Base   => undef,
+        Target => undef,
+        Type   => undef,
+        @_
+    );
+
+    #check acls
+    unless ( $self->CurrentUserHasRight('ModifyQueue') ) {
+        $RT::Logger->debug("No permission to delete links\n");
+        return ( 0, $self->loc('Permission Denied'))
+    }
+
+    return $self->SUPER::_DeleteLink(%args);
+}
+
 =head2 AvailableRights
 
 Returns a hash of available rights for this object. The keys are the right names and the values are a description of what the rights do
@@ -123,7 +183,12 @@ Returns an array of all ActiveStatuses for this queue
 
 sub ActiveStatusArray {
     my $self = shift;
-    return (@ACTIVE_STATUS);
+    if (@RT::ActiveStatus) {
+       return (@RT::ActiveStatus)
+    } else {
+        $RT::Logger->warning("RT::ActiveStatus undefined, falling back to deprecated defaults");
+        return (@DEFAULT_ACTIVE_STATUS);
+    }
 }
 
 # }}}
@@ -138,7 +203,12 @@ Returns an array of all InactiveStatuses for this queue
 
 sub InactiveStatusArray {
     my $self = shift;
-    return (@INACTIVE_STATUS);
+    if (@RT::InactiveStatus) {
+       return (@RT::InactiveStatus)
+    } else {
+        $RT::Logger->warning("RT::InactiveStatus undefined, falling back to deprecated defaults");
+        return (@DEFAULT_INACTIVE_STATUS);
+    }
 }
 
 # }}}
@@ -153,7 +223,7 @@ Returns an array of all statuses for this queue
 
 sub StatusArray {
     my $self = shift;
-    return (@STATUS);
+    return ($self->ActiveStatusArray(), $self->InactiveStatusArray());
 }
 
 # }}}
@@ -162,20 +232,23 @@ sub StatusArray {
 
 =head2 IsValidStatus VALUE
 
-Returns true if VALUE is a valid status.  Otherwise, returns 0
+Returns true if VALUE is a valid status.  Otherwise, returns 0.
+
+=begin testing
 
-=for testing
 my $q = RT::Queue->new($RT::SystemUser);
 ok($q->IsValidStatus('new')== 1, 'New is a valid status');
 ok($q->IsValidStatus('f00')== 0, 'f00 is not a valid status');
 
+=end testing
+
 =cut
 
 sub IsValidStatus {
     my $self  = shift;
     my $value = shift;
 
-    my $retval = grep ( /^$value$/, $self->StatusArray );
+    my $retval = grep ( $_ eq $value, $self->StatusArray );
     return ($retval);
 
 }
@@ -188,19 +261,22 @@ sub IsValidStatus {
 
 Returns true if VALUE is a Active status.  Otherwise, returns 0
 
-=for testing
+=begin testing
+
 my $q = RT::Queue->new($RT::SystemUser);
 ok($q->IsActiveStatus('new')== 1, 'New is a Active status');
 ok($q->IsActiveStatus('rejected')== 0, 'Rejected is an inactive status');
 ok($q->IsActiveStatus('f00')== 0, 'f00 is not a Active status');
 
+=end testing
+
 =cut
 
 sub IsActiveStatus {
     my $self  = shift;
     my $value = shift;
 
-    my $retval = grep ( /^$value$/, $self->ActiveStatusArray );
+    my $retval = grep ( $_ eq $value, $self->ActiveStatusArray );
     return ($retval);
 
 }
@@ -213,19 +289,22 @@ sub IsActiveStatus {
 
 Returns true if VALUE is a Inactive status.  Otherwise, returns 0
 
-=for testing
+=begin testing
+
 my $q = RT::Queue->new($RT::SystemUser);
 ok($q->IsInactiveStatus('new')== 0, 'New is a Active status');
 ok($q->IsInactiveStatus('rejected')== 1, 'rejeected is an Inactive status');
 ok($q->IsInactiveStatus('f00')== 0, 'f00 is not a Active status');
 
+=end testing
+
 =cut
 
 sub IsInactiveStatus {
     my $self  = shift;
     my $value = shift;
 
-    my $retval = grep ( /^$value$/, $self->InactiveStatusArray );
+    my $retval = grep ( $_ eq $value, $self->InactiveStatusArray );
     return ($retval);
 
 }
@@ -235,11 +314,34 @@ sub IsInactiveStatus {
 
 # {{{ sub Create
 
-=head2 Create
 
-Create takes the name of the new queue 
+
+
+=head2 Create(ARGS)
+
+Arguments: ARGS is a hash of named parameters.  Valid parameters are:
+
+  Name (required)
+  Description
+  CorrespondAddress
+  CommentAddress
+  InitialPriority
+  FinalPriority
+  DefaultDueIn
 If you pass the ACL check, it creates the queue and returns its queue id.
 
+=begin testing
+
+my $queue = RT::Queue->new($RT::SystemUser);
+my ($id, $val) = $queue->Create( Name => 'Test1');
+ok($id, $val);
+
+($id, $val) = $queue->Create( Name => '66');
+ok(!$id, $val);
+
+=end testing
+
 =cut
 
 sub Create {
@@ -300,8 +402,8 @@ sub Delete {
 =head2 SetDisabled
 
 Takes a boolean.
-1 will cause this queue to no longer be avaialble for tickets.
-0 will re-enable this queue
+1 will cause this queue to no longer be available for tickets.
+0 will re-enable this queue.
 
 =cut
 
@@ -327,7 +429,7 @@ sub Load {
         $self->SUPER::LoadById($identifier);
     }
     else {
-        $self->LoadByCol( "Name", $identifier );
+        $self->LoadByCols( Name => $identifier );
     }
 
     return ( $self->Id );
@@ -352,20 +454,14 @@ sub ValidateName {
     my $tempqueue = new RT::Queue($RT::SystemUser);
     $tempqueue->Load($name);
 
-    #If we couldn't load it :)
-    unless ( $tempqueue->id() ) {
-        return (1);
-    }
-
     #If this queue exists, return undef
-    #Avoid the ACL check.
-    if ( $tempqueue->Name() ) {
+    if ( $tempqueue->Name() && $tempqueue->id != $self->id)  {
         return (undef);
     }
 
     #If the queue doesn't exist, return 1
     else {
-        return (1);
+        return ($self->SUPER::ValidateName($name));
     }
 
 }
@@ -398,7 +494,7 @@ sub Templates {
 
 # {{{  CustomField
 
-=item CustomField NAME
+=head2 CustomField NAME
 
 Load the queue-specific custom field named NAME
 
@@ -415,18 +511,38 @@ sub CustomField {
 
 # {{{ CustomFields
 
-=item CustomFields
+=head2 CustomFields
 
 Returns an RT::CustomFields object containing all global custom fields, as well as those tied to this queue
 
 =cut
 
+# XXX TODO - this should become TicketCustomFields
+
 sub CustomFields {
     my $self = shift;
+    warn "Queue->CustomFields is deprecated, use Queue->TicketCustomFields instead";
+    return $self->TicketCustomFields(@_);
+}
+
+sub TicketCustomFields {
+    my $self = shift;
 
     my $cfs = RT::CustomFields->new( $self->CurrentUser );
     if ( $self->CurrentUserHasRight('SeeQueue') ) {
-        $cfs->LimitToGlobalOrQueue( $self->Id );
+       $cfs->LimitToGlobalOrObjectId( $self->Id );
+       $cfs->LimitToLookupType( 'RT::Queue-RT::Ticket' );
+    }
+    return ($cfs);
+}
+
+sub TicketTransactionCustomFields {
+    my $self = shift;
+
+    my $cfs = RT::CustomFields->new( $self->CurrentUser );
+    if ( $self->CurrentUserHasRight('SeeQueue') ) {
+       $cfs->LimitToGlobalOrObjectId( $self->Id );
+       $cfs->LimitToLookupType( 'RT::Queue-RT::Ticket-RT::Transaction' );
     }
     return ($cfs);
 }
@@ -442,7 +558,7 @@ sub CustomFields {
 
 =head2 _CreateQueueGroups
 
-Create the ticket groups and relationships for this ticket. 
+Create the ticket groups and links for this ticket. 
 This routine expects to be called from Ticket->Create _inside of a transaction_
 
 It will create four groups for this ticket: Requestor, Cc, AdminCc and Owner.
@@ -518,6 +634,8 @@ Email       The email address of the new watcher. If a user with this
 
 If the watcher you\'re trying to set has an RT account, set the Owner paremeter to their User Id. Otherwise, set the Email parameter to their Email address.
 
+Returns a tuple of (status/id, message).
+
 =cut
 
 sub AddWatcher {
@@ -551,7 +669,7 @@ sub AddWatcher {
             }
         }
      else {
-            $RT::Logger->warn( "$self -> AddWatcher got passed a bogus type");
+            $RT::Logger->warning( "$self -> AddWatcher got passed a bogus type");
             return ( 0, $self->loc('Error in parameters to Queue->AddWatcher') );
         }
     }
@@ -602,10 +720,13 @@ sub _AddWatcher {
         # if the user doesn't exist, we need to create a new user
              my $new_user = RT::User->new($RT::SystemUser);
 
+            my ( $Address, $Name ) =  
+               RT::Interface::Email::ParseAddressFromHeader($args{'Email'});
+
             my ( $Val, $Message ) = $new_user->Create(
-                Name => $args{'Email'},
-                EmailAddress => $args{'Email'},
-                RealName     => $args{'Email'},
+                Name => $Address,
+                EmailAddress => $Address,
+                RealName     => $Name,
                 Privileged   => 0,
                 Comments     => 'Autocreated when added as a watcher');
             unless ($Val) {
@@ -709,8 +830,8 @@ sub DeleteWatcher {
             }
         }
         else {
-            $RT::Logger->warn( "$self -> DelWatcher got passed a bogus type");
-            return ( 0, $self->loc('Error in parameters to Queue->DelWatcher') );
+            $RT::Logger->warning( "$self -> DeleteWatcher got passed a bogus type");
+            return ( 0, $self->loc('Error in parameters to Queue->DeleteWatcher') );
         }
     }
 
@@ -866,8 +987,11 @@ sub IsWatcher {
 
     my $principal = RT::Principal->new($self->CurrentUser);
     $principal->Load($args{'PrincipalId'});
+    unless ($principal->Id) {
+        return (undef);
+    }
 
-    return ($group->HasMember($principal));
+    return ($group->HasMemberRecursively($principal));
 }
 
 # }}}
@@ -877,8 +1001,8 @@ sub IsWatcher {
 
 =head2 IsCc PRINCIPAL_ID
 
-  Takes an RT::Principal id.
-  Returns true if the principal is a requestor of the current queue.
+Takes an RT::Principal id.
+Returns true if the principal is a requestor of the current queue.
 
 
 =cut
@@ -897,8 +1021,8 @@ sub IsCc {
 
 =head2 IsAdminCc PRINCIPAL_ID
 
-  Takes an RT::Principal id.
-  Returns true if the principal is a requestor of the current queue.
+Takes an RT::Principal id.
+Returns true if the principal is a requestor of the current queue.
 
 =cut
 
@@ -999,7 +1123,7 @@ sub HasRight {
     }
     return (
         $args{'Principal'}->HasRight(
-            Object => $self,
+            Object => $self->Id ? $self : $RT::System,
             Right    => $args{'Right'}
           )
     );