X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FScrip.pm;h=48547ccfd2316e2bc3590037fd93e90dfc11d206;hp=b430a77e90e1d7c9c38e8534eb3699d94fe86a2d;hb=7322f2afedcc2f427e997d1535a503613a83f088;hpb=5a5e999c1995dc74e90a5bec660e28692b81611e diff --git a/rt/lib/RT/Scrip.pm b/rt/lib/RT/Scrip.pm index b430a77e9..48547ccfd 100755 --- a/rt/lib/RT/Scrip.pm +++ b/rt/lib/RT/Scrip.pm @@ -2,7 +2,7 @@ # # COPYRIGHT: # -# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC +# This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -46,371 +46,998 @@ # # 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; - - =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, + + + + +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 + +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) } -use vars qw( @ISA ); -@ISA= qw( RT::Record ); +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 _Init { - my $self = shift; +sub AddedTo { + my $self = shift; + return RT::ObjectScrip->new( $self->CurrentUser ) + ->AddedTo( Scrip => $self ); +} - $self->Table('Scrips'); - $self->SUPER::_Init(@_); +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