# BEGIN BPS TAGGED BLOCK {{{ # # COPYRIGHT: # # 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., 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 RT::Scrip - an RT Scrip object =head1 SYNOPSIS use RT::Scrip; =head1 DESCRIPTION =head1 METHODS =cut package RT::Scrip; 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, 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, ConditionRules => undef, ActionRules => 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'}, ConditionRules => $args{'ConditionRules'}, ActionRules => $args{'ActionRules'}, ); 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 sub Delete { my $self = shift; unless ( $self->CurrentUserHasRight('ModifyScrips') ) { return ( 0, $self->loc('Permission Denied') ); } 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