More rigorous session-cleanup for BecomeUser
[usit-rt.git] / sbin / rt-email-group-admin
CommitLineData
84fb5b46
MKG
1#!/usr/bin/perl
2# BEGIN BPS TAGGED BLOCK {{{
3#
4# COPYRIGHT:
5#
6# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
7# <sales@bestpractical.com>
8#
9# (Except where explicitly superseded by other copyright notices)
10#
11#
12# LICENSE:
13#
14# This work is made available to you under the terms of Version 2 of
15# the GNU General Public License. A copy of that license should have
16# been provided with this software, but in any event can be snarfed
17# from www.gnu.org.
18#
19# This work is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY; without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program; if not, write to the Free Software
26# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27# 02110-1301 or visit their web page on the internet at
28# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
29#
30#
31# CONTRIBUTION SUBMISSION POLICY:
32#
33# (The following paragraph is not intended to limit the rights granted
34# to you to modify and distribute this software under the terms of
35# the GNU General Public License and is only of importance to you if
36# you choose to contribute your changes and enhancements to the
37# community by submitting them to Best Practical Solutions, LLC.)
38#
39# By intentionally submitting any modifications, corrections or
40# derivatives to this work, or any other work intended for use with
41# Request Tracker, to Best Practical Solutions, LLC, you confirm that
42# you are the copyright holder for those contributions and you grant
43# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
44# royalty-free, perpetual, license to use, copy, create derivative
45# works based on those contributions, and sublicense and distribute
46# those contributions and any derivatives thereof.
47#
48# END BPS TAGGED BLOCK }}}
49=head1 NAME
50
51rt-email-group-admin - Command line tool for administrating NotifyGroup actions
52
53=head1 SYNOPSIS
54
55 rt-email-group-admin --list
56 rt-email-group-admin --create 'Notify foo team' --group Foo
57 rt-email-group-admin --create 'Notify foo team as comment' --comment --group Foo
58 rt-email-group-admin --create 'Notify group Foo and Bar' --group Foo --group Bar
59 rt-email-group-admin --create 'Notify user foo@bar.com' --user foo@bar.com
60 rt-email-group-admin --create 'Notify VIPs' --user vip1@bar.com
61 rt-email-group-admin --add 'Notify VIPs' --user vip2@bar.com --group vip1 --user vip3@foo.com
62 rt-email-group-admin --rename 'Notify VIPs' --newname 'Inform VIPs'
63 rt-email-group-admin --switch 'Notify VIPs'
64 rt-email-group-admin --delete 'Notify user foo@bar.com'
65
66=head1 DESCRIPTION
67
68This script list, create, modify or delete scrip actions in the RT DB. Once
69you've created an action you can use it in a scrip.
70
71For example you can create the following action using this script:
72
73 rt-email-group-admin --create 'Notify developers' --group 'Development Team'
74
75Then you can add the followoing scrip to your Bugs queue:
76
77 Condition: On Create
78 Action: Notify developers
79 Template: Transaction
80 Stage: TransactionCreate
81
82Your development team will be notified on every new ticket in the queue.
83
84=cut
85
86use warnings;
87use strict;
88
89# fix lib paths, some may be relative
90BEGIN {
91 require File::Spec;
92 my @libs = ("lib", "local/lib");
93 my $bin_path;
94
95 for my $lib (@libs) {
96 unless ( File::Spec->file_name_is_absolute($lib) ) {
97 unless ($bin_path) {
98 if ( File::Spec->file_name_is_absolute(__FILE__) ) {
99 $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
100 }
101 else {
102 require FindBin;
103 no warnings "once";
104 $bin_path = $FindBin::Bin;
105 }
106 }
107 $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
108 }
109 unshift @INC, $lib;
110 }
111
112}
113
114use Getopt::Long qw(GetOptions);
115Getopt::Long::Configure( "pass_through" );
116
117our $cmd = 'usage';
118our $opts = {};
119
120sub parse_args {
121 my $tmp;
122 if ( GetOptions( 'list' => \$tmp ) && $tmp ) {
123 $cmd = 'list';
124 }
125 elsif ( GetOptions( 'create=s' => \$tmp ) && $tmp ) {
126 $cmd = 'create';
127 $opts->{'name'} = $tmp;
128 $opts->{'groups'} = [];
129 $opts->{'users'} = [];
130 GetOptions( 'comment' => \$opts->{'comment'} );
131 GetOptions( 'group:s@' => $opts->{'groups'} );
132 GetOptions( 'user:s@' => $opts->{'users'} );
133 unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
134 usage();
135 exit(-1);
136 }
137 }
138 elsif ( GetOptions( 'add=s' => \$tmp ) && $tmp ) {
139 $cmd = 'add';
140 $opts->{'name'} = $tmp;
141 $opts->{'groups'} = [];
142 $opts->{'users'} = [];
143 GetOptions( 'group:s@' => $opts->{'groups'} );
144 GetOptions( 'user:s@' => $opts->{'users'} );
145 unless ( @{ $opts->{'users'} } + @{ $opts->{'groups'} } ) {
146 usage();
147 exit(-1);
148 }
149 }
150 elsif ( GetOptions( 'switch=s' => \$tmp ) && $tmp ) {
151 $cmd = 'switch';
152 $opts->{'name'} = $tmp;
153 }
154 elsif ( GetOptions( 'rename=s' => \$tmp ) && $tmp ) {
155 $cmd = 'rename';
156 $opts->{'name'} = $tmp;
157 GetOptions( 'newname=s' => \$opts->{'newname'} );
158 unless ( $opts->{'newname'} ) {
159 usage();
160 exit(-1);
161 }
162 }
163 elsif ( GetOptions( 'delete=s' => \$tmp ) && $tmp) {
164 $cmd = 'delete';
165 $opts->{'name'} = $tmp;
166 } else {
167 $cmd = 'usage';
168 }
169
170 return;
171}
172
173sub usage {
174 require Pod::Usage;
175 Pod::Usage::pod2usage({ verbose => 2 });
176}
177
178my $help;
179if ( GetOptions( 'help|h' => \$help ) && $help ) {
180 usage();
181 exit;
182}
183
184parse_args();
185
186require RT;
187RT->LoadConfig;
188RT->Init;
189
190require RT::Principal;
191require RT::User;
192require RT::Group;
193require RT::ScripActions;
194
195
196{
197 eval "main::$cmd()";
198 if ( $@ ) {
199 print STDERR $@ ."\n";
200 }
201}
202
203exit(0);
204
205=head1 USAGE
206
207rt-email-group-admin --COMMAND ARGS
208
209=head1 COMMANDS
210
211=head2 list
212
213Lists actions and its descriptions.
214
215=cut
216
217sub list {
218 my $actions = _get_our_actions();
219 while( my $a = $actions->Next ) {
220 _list( $a );
221 }
222 return;
223}
224
225sub _list {
226 my $action = shift;
227
228 print "Name: ". $action->Name() ."\n";
229 print "Module: ". $action->ExecModule() ."\n";
230
231 my @princ = argument_to_list( $action );
232
233 print "Members: \n";
234 foreach( @princ ) {
235 my $obj = RT::Principal->new( RT->SystemUser );
236 $obj->Load( $_ );
237 next unless $obj->id;
238
239 print "\t". $obj->PrincipalType;
240 print "\t=> ". $obj->Object->Name;
241 print "(Disabled!!!)" if $obj->Disabled;
242 print "\n";
243 }
244 print "\n";
245 return;
246}
247
248=head2 create NAME [--comment] [--group GNAME] [--user UNAME]
249
250Creates new action with NAME and adds users and/or groups to its
251recipient list. Would be notify as comment if --comment specified.
252
253=cut
254
255sub create {
256 my $actions = RT::ScripActions->new( RT->SystemUser );
257 $actions->Limit(
258 FIELD => 'Name',
259 VALUE => $opts->{'name'},
260 );
261 if ( $actions->Count ) {
262 print STDERR "ScripAction '". $opts->{'name'} ."' allready exists\n";
263 exit(-1);
264 }
265
266 my @groups = _check_groups( @{ $opts->{'groups'} } );
267 my @users = _check_users( @{ $opts->{'users'} } );
268 unless ( @users + @groups ) {
269 print STDERR "List of groups and users is empty\n";
270 exit(-1);
271 }
272
273 my $action = __create_empty( $opts->{'name'}, $opts->{'comment'} );
274
275 __add( $action, $_ ) foreach( @users );
276 __add( $action, $_ ) foreach( @groups );
277
278 return;
279}
280
281sub __create_empty {
282 my $name = shift;
283 my $as_comment = shift || 0;
284 require RT::ScripAction;
285 my $action = RT::ScripAction->new( RT->SystemUser );
286 $action->Create(
287 Name => $name,
288 Description => "Created with rt-email-group-admin script",
289 ExecModule => $as_comment? 'NotifyGroupAsComment': 'NotifyGroup',
290 Argument => '',
291 );
292
293 return $action;
294}
295
296sub _check_groups
297{
298 return grep { $_ ? 1: do { print STDERR "Group '$_' skipped, doesn't exist\n"; 0; } }
299 map { __check_group($_) } @_;
300}
301
302sub __check_group
303{
304 my $instance = shift;
305 require RT::Group;
306 my $obj = RT::Group->new( RT->SystemUser );
307 $obj->LoadUserDefinedGroup( $instance );
308 return $obj->id ? $obj : undef;
309}
310
311sub _check_users
312{
313 return grep { $_ ? 1: do { print STDERR "User '$_' skipped, doesn't exist\n"; 0; } }
314 map { __check_user($_) } @_;
315}
316
317sub __check_user
318{
319 my $instance = shift;
320 require RT::User;
321 my $obj = RT::User->new( RT->SystemUser );
322 $obj->Load( $instance );
323 return $obj->id ? $obj : undef;
324}
325
326=head2 add NAME [--group GNAME] [--user UNAME]
327
328Adds groups and/or users to recipients of the action NAME.
329
330=cut
331
332sub add {
333 my $action = _get_action_by_name( $opts->{'name'} );
334 unless ( $action ) {
335 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
336 exit(-1);
337 }
338
339 my @groups = _check_groups( @{ $opts->{'groups'} } );
340 my @users = _check_users( @{ $opts->{'users'} } );
341
342 unless ( @users + @groups ) {
343 print STDERR "List of groups and users is empty\n";
344 exit(-1);
345 }
346
347 __add( $action, $_ ) foreach @users;
348 __add( $action, $_ ) foreach @groups;
349
350 return;
351}
352
353sub __add
354{
355 my $action = shift;
356 my $obj = shift;
357
358 my @cur = argument_to_list( $action );
359
360 my $id = $obj->id;
361 return if grep $_ == $id, @cur;
362
363 push @cur, $id;
364
365 return $action->__Set( Field => 'Argument', Value => join(',', @cur) );
366}
367
368=head2 delete NAME
369
370Deletes action NAME if scrips doesn't use it.
371
372=cut
373
374sub delete {
375 my $action = _get_action_by_name( $opts->{'name'} );
376 unless ( $action ) {
377 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
378 exit(-1);
379 }
380
381 require RT::Scrips;
382 my $scrips = RT::Scrips->new( RT->SystemUser );
383 $scrips->Limit( FIELD => 'ScripAction', VALUE => $action->id );
384 if ( $scrips->Count ) {
385 my @sid;
386 while( my $s = $scrips->Next ) {
387 push @sid, $s->id;
388 }
389 print STDERR "ScripAction '". $opts->{'name'} ."'"
390 . " is in use by Scrip(s) ". join( ", ", map "#$_", @sid )
391 . "\n";
392 exit(-1);
393 }
394
395 return __delete( $action );
396}
397
398sub __delete {
399 require DBIx::SearchBuilder::Record;
400 return DBIx::SearchBuilder::Record::Delete( shift );
401}
402
403sub _get_action_by_name {
404 my $name = shift;
405 my $actions = _get_our_actions();
406 $actions->Limit(
407 FIELD => 'Name',
408 VALUE => $name
409 );
410
411 if ( $actions->Count > 1 ) {
412 print STDERR "More then one ScripAction with name '$name'\n";
413 }
414
415 return $actions->First;
416}
417
418=head2 switch NAME
419
420Switch action NAME from notify as correspondence to comment and back.
421
422=cut
423
424sub switch {
425 my $action = _get_action_by_name( $opts->{'name'} );
426 unless ( $action ) {
427 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
428 exit(-1);
429 }
430
431 my %h = (
432 NotifyGroup => 'NotifyGroupAsComment',
433 NotifyGroupAsComment => 'NotifyGroup'
434 );
435
436 return $action->__Set(
437 Field => 'ExecModule',
438 Value => $h{ $action->ExecModule }
439 );
440}
441
442=head2 rename NAME --newname NEWNAME
443
444Renames action NAME to NEWNAME.
445
446=cut
447
448sub rename {
449 my $action = _get_action_by_name( $opts->{'name'} );
450 unless ( $action ) {
451 print STDERR "ScripAction '". $opts->{'name'} ."' doesn't exist\n";
452 exit(-1);
453 }
454
455 my $actions = RT::ScripActions->new( RT->SystemUser );
456 $actions->Limit( FIELD => 'Name', VALUE => $opts->{'newname'} );
457 if ( $actions->Count ) {
458 print STDERR "ScripAction '". $opts->{'newname'} ."' allready exists\n";
459 exit(-1);
460 }
461
462 return $action->__Set(
463 Field => 'Name',
464 Value => $opts->{'newname'},
465 );
466}
467
468=head2 NOTES
469
470If command has option --group or --user then you can use it more then once,
471if other is not specified.
472
473=cut
474
475###############
476#### Utils ####
477###############
478
479sub argument_to_list {
480 my $action = shift;
481 require RT::Action::NotifyGroup;
482 return RT::Action::NotifyGroup->__SplitArg( $action->Argument );
483}
484
485sub _get_our_actions {
486 my $actions = RT::ScripActions->new( RT->SystemUser );
487 $actions->Limit(
488 FIELD => 'ExecModule',
489 VALUE => 'NotifyGroup',
490 ENTRYAGGREGATOR => 'OR',
491 );
492 $actions->Limit(
493 FIELD => 'ExecModule',
494 VALUE => 'NotifyGroupAsComment',
495 ENTRYAGGREGATOR => 'OR',
496 );
497
498 return $actions;
499}
500
501=head1 AUTHOR
502
503Ruslan U. Zakirov E<lt>ruz@bestpractical.comE<gt>
504
505=head1 SEE ALSO
506
507L<RT::Action::NotifyGroup>, L<RT::Action::NotifyGroupAsComment>
508
509=cut