X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FScrip.pm;h=48547ccfd2316e2bc3590037fd93e90dfc11d206;hp=8b24a64004dbadd9991a2fc4c505ee7960e98aa6;hb=7322f2afedcc2f427e997d1535a503613a83f088;hpb=8103c1fc1b2c27a6855feadf26f91b980a54bc52 diff --git a/rt/lib/RT/Scrip.pm b/rt/lib/RT/Scrip.pm index 8b24a6400..48547ccfd 100755 --- a/rt/lib/RT/Scrip.pm +++ b/rt/lib/RT/Scrip.pm @@ -1,40 +1,40 @@ # BEGIN BPS TAGGED BLOCK {{{ -# +# # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC -# -# +# +# 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/copyleft/gpl.html. -# -# +# 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 @@ -43,373 +43,1001 @@ # 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 }}} -# Autogenerated by DBIx::SearchBuilder factory (by ) -# WARNING: THIS FILE IS AUTOGENERATED. ALL CHANGES TO THIS FILE WILL BE LOST. -# -# !! DO NOT EDIT THIS FILE !! # - -use strict; - +# END BPS TAGGED BLOCK }}} =head1 NAME -RT::Scrip - + RT::Scrip - an RT Scrip object =head1 SYNOPSIS + use RT::Scrip; + =head1 DESCRIPTION + =head1 METHODS + =cut + package RT::Scrip; -use RT::Record; + +use strict; +use warnings; +use base 'RT::Record'; + use RT::Queue; use RT::Template; use RT::ScripCondition; use RT::ScripAction; +use RT::Scrips; +use RT::ObjectScrip; + +sub Table {'Scrips'} + +# {{{ sub Create + +=head2 Create + +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, -use vars qw( @ISA ); -@ISA= qw( RT::Record ); -sub _Init { - my $self = shift; - $self->Table('Scrips'); - $self->SUPER::_Init(@_); + +Returns (retval, msg); +retval is 0 for failure or scrip id. msg is a textual description of what happened. + +=cut + +sub Create { + my $self = shift; + 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 = 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 + + 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'}; + } + + 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') ); } +=head2 Delete + +Delete this object +=cut -=head2 Create PARAMHASH +sub Delete { + my $self = shift; -Create takes a hash of values and creates a row in the database: + unless ( $self->CurrentUserHasRight('ModifyScrips') ) { + return ( 0, $self->loc('Permission Denied') ); + } - varchar(255) 'Description'. - int(11) 'ScripCondition'. - int(11) 'ScripAction'. - text 'ConditionRules'. - text 'ActionRules'. - text 'CustomIsApplicableCode'. - text 'CustomPrepareCode'. - text 'CustomCommitCode'. - varchar(32) 'Stage'. - int(11) 'Queue'. - int(11) 'Template'. + 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 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