X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FScrip.pm;h=48547ccfd2316e2bc3590037fd93e90dfc11d206;hp=aef011ca3cd750cb7bd872de15ffa5a828234e21;hb=7322f2afedcc2f427e997d1535a503613a83f088;hpb=0ebeec96313dd7edfca340f01f8fbbbac1f4aa1d diff --git a/rt/lib/RT/Scrip.pm b/rt/lib/RT/Scrip.pm index aef011ca3..48547ccfd 100755 --- a/rt/lib/RT/Scrip.pm +++ b/rt/lib/RT/Scrip.pm @@ -1,4 +1,50 @@ -#$Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Scrip.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $ +# BEGIN BPS TAGGED BLOCK {{{ +# +# COPYRIGHT: +# +# This software is Copyright (c) 1996-2016 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., 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 +# 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 @@ -13,121 +59,157 @@ =head1 METHODS -=begin testing - -ok (require RT::TestHarness); -ok (require RT::Scrip); - -=end testing =cut + package RT::Scrip; -use RT::Record; -@ISA= qw(RT::Record); -# {{{ sub _Init -sub _Init { - my $self = shift; - $self->{'table'} = "Scrips"; - return ($self->SUPER::_Init(@_)); -} -# }}} +use strict; +use warnings; +use base 'RT::Record'; -# {{{ sub _Accessible -sub _Accessible { - my $self = shift; - my %Cols = ( ScripAction => 'read/write', - ScripCondition => 'read/write', - Stage => 'read/write', - Queue => 'read/write', - Template => 'read/write', - ); - return($self->SUPER::_Accessible(@_, %Cols)); -} -# }}} +use RT::Queue; +use RT::Template; +use RT::ScripCondition; +use RT::ScripAction; +use RT::Scrips; +use RT::ObjectScrip; + +sub Table {'Scrips'} -# {{{ sub Create +# {{{ sub Create =head2 Create -Creates a new entry in the Scrips table. Takes a paramhash with the attributes: +Creates a new entry in the Scrips table. Takes a paramhash with: + + Queue => 0, + Description => undef, + Template => undef, + ScripAction => undef, + ScripCondition => undef, + CustomPrepareCode => undef, + CustomCommitCode => undef, + CustomIsApplicableCode => undef, + + - Queue A queue id or 0 for a global scrip - Template A template ID or name. - Behavior is undefined if you have multiple items with - the same name - ScripAction A ScripAction id or name - Behavior is undefined if you have multiple items with - the same name - ScripCondition A ScripCondition id or name - Behavior is undefined if you have multiple items with - the same name Returns (retval, msg); retval is 0 for failure or scrip id. msg is a textual description of what happened. =cut -sub Create { +sub Create { my $self = shift; - my %args = ( Queue => undef, - Template => undef, - ScripAction => undef, - ScripCondition => undef, - Stage => 'TransactionCreate', - @_ - ); - - - if ($args{'Queue'} == 0 ) { - unless ($self->CurrentUser->HasSystemRight('ModifyScrips')) { - return (0, 'Permission Denied'); - } + my %args = ( + Queue => 0, + Template => undef, # name or id + ScripAction => 0, # name or id + ScripCondition => 0, # name or id + Stage => 'TransactionCreate', + Description => undef, + CustomPrepareCode => undef, + CustomCommitCode => undef, + CustomIsApplicableCode => undef, + @_ + ); + + if ($args{CustomPrepareCode} || $args{CustomCommitCode} || $args{CustomIsApplicableCode}) { + unless ( $self->CurrentUser->HasRight( Object => $RT::System, + Right => 'ExecuteCode' ) ) + { + return ( 0, $self->loc('Permission Denied') ); + } + } + + unless ( $args{'Queue'} ) { + unless ( $self->CurrentUser->HasRight( Object => $RT::System, + Right => 'ModifyScrips' ) ) + { + return ( 0, $self->loc('Permission Denied') ); + } + $args{'Queue'} = 0; # avoid undef sneaking in } else { - my $QueueObj = new RT::Queue($self->CurrentUser); - $QueueObj->Load($args{'Queue'}); - unless ($QueueObj->id()) { - return (0,'Invalid queue'); - } - unless ($QueueObj->CurrentUserHasRight('ModifyScrips')) { - return (0, 'Permssion Denied'); - } + my $QueueObj = RT::Queue->new( $self->CurrentUser ); + $QueueObj->Load( $args{'Queue'} ); + unless ( $QueueObj->id ) { + return ( 0, $self->loc('Invalid queue') ); + } + unless ( $QueueObj->CurrentUserHasRight('ModifyScrips') ) { + return ( 0, $self->loc('Permission Denied') ); + } + $args{'Queue'} = $QueueObj->id; } - #TODO +++ validate input - - require RT::ScripAction; - my $action = new RT::ScripAction($self->CurrentUser); - $action->Load($args{'ScripAction'}); - return (0, "Action ".$args{'ScripAction'}." not found") unless $action->Id; - - require RT::Template; - my $template = new RT::Template($self->CurrentUser); - $template->Load($args{'Template'}); - return (0, 'Template not found') unless $template->Id; + #TODO +++ validate input + + return ( 0, $self->loc("Action is mandatory argument") ) + unless $args{'ScripAction'}; + my $action = RT::ScripAction->new( $self->CurrentUser ); + $action->Load( $args{'ScripAction'} ); + return ( 0, $self->loc( "Action '[_1]' not found", $args{'ScripAction'} ) ) + unless $action->Id; + + return ( 0, $self->loc("Template is mandatory argument") ) + unless $args{'Template'}; + my $template = RT::Template->new( $self->CurrentUser ); + if ( $args{'Template'} =~ /\D/ ) { + $template->LoadByName( Name => $args{'Template'}, Queue => $args{'Queue'} ); + return ( 0, $self->loc( "Global template '[_1]' not found", $args{'Template'} ) ) + if !$template->Id && !$args{'Queue'}; + return ( 0, $self->loc( "Global or queue specific template '[_1]' not found", $args{'Template'} ) ) + if !$template->Id; + } else { + $template->Load( $args{'Template'} ); + return ( 0, $self->loc( "Template '[_1]' not found", $args{'Template'} ) ) + unless $template->Id; + + return (0, $self->loc( "Template '[_1]' is not global" )) + if !$args{'Queue'} && $template->Queue; + return (0, $self->loc( "Template '[_1]' is not global nor queue specific" )) + if $args{'Queue'} && $template->Queue && $template->Queue != $args{'Queue'}; + } - require RT::ScripCondition; - my $condition = new RT::ScripCondition($self->CurrentUser); - $condition->Load($args{'ScripCondition'}); - - unless ($condition->Id) { - return (0, 'Condition not found'); - } - - my $id = $self->SUPER::Create(Queue => $args{'Queue'}, - Template => $template->Id, - ScripCondition => $condition->id, - Stage => $args{'Stage'}, - ScripAction => $action->Id - ); - return ($id, 'Scrip Created'); + return ( 0, $self->loc("Condition is mandatory argument") ) + unless $args{'ScripCondition'}; + my $condition = RT::ScripCondition->new( $self->CurrentUser ); + $condition->Load( $args{'ScripCondition'} ); + return ( 0, $self->loc( "Condition '[_1]' not found", $args{'ScripCondition'} ) ) + unless $condition->Id; + + if ( $args{'Stage'} eq 'Disabled' ) { + $RT::Logger->warning("Disabled Stage is deprecated"); + $args{'Stage'} = 'TransactionCreate'; + $args{'Disabled'} = 1; + } + $args{'Disabled'} ||= 0; + + my ( $id, $msg ) = $self->SUPER::Create( + Template => $template->Name, + ScripCondition => $condition->id, + ScripAction => $action->Id, + Disabled => $args{'Disabled'}, + Description => $args{'Description'}, + CustomPrepareCode => $args{'CustomPrepareCode'}, + CustomCommitCode => $args{'CustomCommitCode'}, + CustomIsApplicableCode => $args{'CustomIsApplicableCode'}, + ); + return ( $id, $msg ) unless $id; + + (my $status, $msg) = RT::ObjectScrip->new( $self->CurrentUser )->Add( + Scrip => $self, + Stage => $args{'Stage'}, + ObjectId => $args{'Queue'}, + ); + $RT::Logger->error( "Couldn't add scrip: $msg" ) unless $status; + + return ( $id, $self->loc('Scrip Created') ); } -# }}} -# {{{ sub Delete =head2 Delete @@ -137,236 +219,942 @@ Delete this object sub Delete { my $self = shift; - - unless ($self->CurrentUserHasRight('ModifyScrips')) { - return (0, 'Permission Denied'); + + unless ( $self->CurrentUserHasRight('ModifyScrips') ) { + return ( 0, $self->loc('Permission Denied') ); } - - return ($self->SUPER::Delete(@_)); + + RT::ObjectScrip->new( $self->CurrentUser )->DeleteAll( Scrip => $self ); + + return ( $self->SUPER::Delete(@_) ); +} + +sub IsGlobal { return shift->IsAdded(0) } + +sub IsAdded { + my $self = shift; + my $record = RT::ObjectScrip->new( $self->CurrentUser ); + $record->LoadByCols( Scrip => $self->id, ObjectId => shift || 0 ); + return undef unless $record->id; + return $record; +} + +sub IsAddedToAny { + my $self = shift; + my $record = RT::ObjectScrip->new( $self->CurrentUser ); + $record->LoadByCols( Scrip => $self->id ); + return $record->id ? 1 : 0; +} + +sub AddedTo { + my $self = shift; + return RT::ObjectScrip->new( $self->CurrentUser ) + ->AddedTo( Scrip => $self ); } -# }}} -# {{{ sub QueueObj +sub NotAddedTo { + my $self = shift; + return RT::ObjectScrip->new( $self->CurrentUser ) + ->NotAddedTo( Scrip => $self ); +} + +=head2 AddToObject + +Adds (applies) the current scrip to the provided queue (ObjectId). + +Accepts a param hash of: + +=over + +=item C + +Queue name or id. 0 makes the scrip global. + +=item C + +Stage to run in. Valid stages are TransactionCreate or +TransactionBatch. Defaults to TransactionCreate. As of RT 4.2, Disabled +is no longer a stage. + +=item C