This commit was generated by cvs2svn to compensate for changes in r2523,
[freeside.git] / rt / lib / RT / Scrip.pm
1 #$Header: /home/cvs/cvsroot/freeside/rt/lib/RT/Scrip.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2
3 =head1 NAME
4
5   RT::Scrip - an RT Scrip object
6
7 =head1 SYNOPSIS
8
9   use RT::Scrip;
10
11 =head1 DESCRIPTION
12
13
14 =head1 METHODS
15
16 =begin testing
17
18 ok (require RT::TestHarness);
19 ok (require RT::Scrip);
20
21 =end testing
22
23 =cut
24
25 package RT::Scrip;
26 use RT::Record;
27 @ISA= qw(RT::Record);
28
29 # {{{ sub _Init
30 sub _Init  {
31     my $self = shift;
32     $self->{'table'} = "Scrips";
33     return ($self->SUPER::_Init(@_));
34 }
35 # }}}
36
37 # {{{ sub _Accessible 
38 sub _Accessible  {
39     my $self = shift;
40     my %Cols = ( ScripAction  => 'read/write',
41                  ScripCondition => 'read/write',
42                  Stage => 'read/write',
43                  Queue => 'read/write', 
44                  Template => 'read/write',
45                );
46     return($self->SUPER::_Accessible(@_, %Cols));
47 }
48 # }}}
49
50 # {{{ sub Create 
51
52 =head2 Create
53
54 Creates a new entry in the Scrips table. Takes a paramhash with the attributes:
55
56     Queue           A queue id or 0 for a global scrip
57     Template        A template ID or name.  
58                     Behavior is undefined if you have multiple items with 
59                     the same name
60     ScripAction     A ScripAction id or name
61                     Behavior is undefined if you have multiple items with 
62                     the same name
63     ScripCondition  A ScripCondition id or name
64                     Behavior is undefined if you have multiple items with 
65                     the same name
66
67 Returns (retval, msg);
68 retval is 0 for failure or scrip id.  msg is a textual description of what happened.
69
70 =cut
71
72 sub Create  {
73     my $self = shift;
74     my %args = ( Queue => undef,
75                  Template => undef,
76                  ScripAction => undef,
77                  ScripCondition => undef,
78                  Stage => 'TransactionCreate',
79                  @_
80                );
81     
82       
83     if ($args{'Queue'} == 0 ) { 
84         unless ($self->CurrentUser->HasSystemRight('ModifyScrips')) {
85             return (0, 'Permission Denied');
86         }       
87     }
88     else {
89         my $QueueObj = new RT::Queue($self->CurrentUser);
90         $QueueObj->Load($args{'Queue'});
91         unless ($QueueObj->id()) {
92             return (0,'Invalid queue');
93         }
94         unless ($QueueObj->CurrentUserHasRight('ModifyScrips')) {
95             return (0, 'Permssion Denied');
96         }       
97     }
98
99     #TODO +++ validate input 
100
101     require RT::ScripAction;
102     my $action = new RT::ScripAction($self->CurrentUser);
103     $action->Load($args{'ScripAction'});
104     return (0, "Action ".$args{'ScripAction'}." not found") unless $action->Id;
105
106     require RT::Template;
107     my $template = new RT::Template($self->CurrentUser);
108     $template->Load($args{'Template'});
109     return (0, 'Template not found') unless $template->Id;
110
111     require RT::ScripCondition;
112     my $condition = new RT::ScripCondition($self->CurrentUser);
113     $condition->Load($args{'ScripCondition'});
114
115     unless ($condition->Id) {
116         return (0, 'Condition not found');
117     }   
118     
119     my $id = $self->SUPER::Create(Queue => $args{'Queue'},
120                                   Template => $template->Id,
121                                   ScripCondition => $condition->id,
122                                   Stage => $args{'Stage'},
123                                   ScripAction => $action->Id
124                                  );
125     return ($id, 'Scrip Created'); 
126 }
127
128 # }}}
129
130 # {{{ sub Delete
131
132 =head2 Delete
133
134 Delete this object
135
136 =cut
137
138 sub Delete {
139     my $self = shift;
140     
141     unless ($self->CurrentUserHasRight('ModifyScrips')) {
142         return (0, 'Permission Denied');
143     }
144     
145     return ($self->SUPER::Delete(@_));
146 }
147 # }}}
148
149 # {{{ sub QueueObj
150
151 =head2 QueueObj
152
153 Retuns an RT::Queue object with this Scrip\'s queue
154
155 =cut
156
157 sub QueueObj {
158     my $self = shift;
159     
160     if (!$self->{'QueueObj'})  {
161         require RT::Queue;
162         $self->{'QueueObj'} = RT::Queue->new($self->CurrentUser);
163         $self->{'QueueObj'}->Load($self->Queue);
164     }
165     return ($self->{'QueueObj'});
166 }
167
168 # }}}
169
170 # {{{ sub ActionObj
171
172
173 =head2 ActionObj
174
175 Retuns an RT::Action object with this Scrip\'s Action
176
177 =cut
178
179 sub ActionObj {
180     my $self = shift;
181     
182     unless (defined $self->{'ScripActionObj'})  {
183         require RT::ScripAction;
184         
185         $self->{'ScripActionObj'} = RT::ScripAction->new($self->CurrentUser);
186         #TODO: why are we loading Actions with templates like this. 
187         # two seperate methods might make more sense
188         $self->{'ScripActionObj'}->Load($self->ScripAction, $self->Template);
189     }
190     return ($self->{'ScripActionObj'});
191 }
192
193 # }}}
194
195
196 # {{{ sub TemplateObj
197 =head2 TemplateObj
198
199 Retuns an RT::Template object with this Scrip\'s Template
200
201 =cut
202
203 sub TemplateObj {
204     my $self = shift;
205     
206     unless (defined $self->{'TemplateObj'})  {
207         require RT::Template;
208         $self->{'TemplateObj'} = RT::Template->new($self->CurrentUser);
209         $self->{'TemplateObj'}->Load($self->Template);
210     }
211     return ($self->{'TemplateObj'});
212 }
213
214 # }}}
215
216 # {{{ sub Prepare
217 =head2 Prepare
218
219 Calls the action object's prepare method
220
221 =cut
222
223 sub Prepare {
224     my $self = shift;
225     $self->ActionObj->Prepare(@_);
226 }
227
228 # }}}
229
230 # {{{ sub Commit
231 =head2 Commit
232
233 Calls the action object's commit method
234
235 =cut
236
237 sub Commit {
238     my $self = shift;
239     $self->ActionObj->Commit(@_);
240 }
241
242 # }}}
243
244 # {{{ sub ConditionObj
245
246 =head2 ConditionObj
247
248 Retuns an RT::ScripCondition object with this Scrip's IsApplicable
249
250 =cut
251
252 sub ConditionObj {
253     my $self = shift;
254     
255     unless (defined $self->{'ScripConditionObj'})  {
256         require RT::ScripCondition;
257         $self->{'ScripConditionObj'} = RT::ScripCondition->new($self->CurrentUser);
258         $self->{'ScripConditionObj'}->Load($self->ScripCondition);
259     }
260     return ($self->{'ScripConditionObj'});
261 }
262
263 # }}}
264
265 # {{{ sub IsApplicable
266
267 =head2 IsApplicable
268
269 Calls the  Condition object\'s IsApplicable method
270
271 =cut
272
273 sub IsApplicable {
274     my $self = shift;
275     return ($self->ConditionObj->IsApplicable(@_));
276 }
277
278 # }}}
279
280 # {{{ sub DESTROY
281 sub DESTROY {
282     my $self = shift;
283     $self->{'ActionObj'} = undef;
284 }
285 # }}}
286
287 # {{{ ACL related methods
288
289 # {{{ sub _Set
290
291 # does an acl check and then passes off the call
292 sub _Set {
293     my $self = shift;
294     
295     unless ($self->CurrentUserHasRight('ModifyScrips')) {
296         $RT::Logger->debug("CurrentUser can't modify Scrips for ".$self->Queue."\n");
297         return (0, 'Permission Denied');
298     }
299     return $self->__Set(@_);
300 }
301
302 # }}}
303
304 # {{{ sub _Value
305 # does an acl check and then passes off the call
306 sub _Value {
307     my $self = shift;
308     
309     unless ($self->CurrentUserHasRight('ShowScrips')) {
310         $RT::Logger->debug("CurrentUser can't modify Scrips for ".$self->__Value('Queue')."\n");
311         return (undef);
312     }
313     
314     return $self->__Value(@_);
315 }
316 # }}}
317
318 # {{{ sub CurrentUserHasRight
319
320 =head2 CurrentUserHasRight
321
322 Helper menthod for HasRight. Presets Principal to CurrentUser then 
323 calls HasRight.
324
325 =cut
326
327 sub CurrentUserHasRight {
328     my $self = shift;
329     my $right = shift;
330     return ($self->HasRight( Principal => $self->CurrentUser->UserObj,
331                              Right => $right ));
332     
333 }
334
335 # }}}
336
337 # {{{ sub HasRight
338
339 =head2 HasRight
340
341 Takes a param-hash consisting of "Right" and "Principal"  Principal is 
342 an RT::User object or an RT::CurrentUser object. "Right" is a textual
343 Right string that applies to Scrips.
344
345 =cut
346
347 sub HasRight {
348     my $self = shift;
349     my %args = ( Right => undef,
350                  Principal => undef,
351                  @_ );
352     
353     if ((defined $self->SUPER::_Value('Queue')) and ($self->SUPER::_Value('Queue') != 0)) {
354         return ( $args{'Principal'}->HasQueueRight(
355                                                    Right => $args{'Right'},
356                                                    Queue => $self->SUPER::_Value('Queue'),
357                                                    Principal => $args{'Principal'}
358                                                   ) 
359                );
360         
361     }
362     else {
363         return( $args{'Principal'}->HasSystemRight( $args{'Right'}) );
364     }
365 }
366 # }}}
367
368 # }}}
369
370 1;
371
372