1 | #!/bin/env perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use LATMOS::Accounts::Utils; |
---|
9 | use LATMOS::Accounts::Log; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | la-group -- add users to a group |
---|
14 | |
---|
15 | =head1 SYNOPSIS |
---|
16 | |
---|
17 | la-group [options] [-s|-r] group user1 [user2 [...]] |
---|
18 | |
---|
19 | =cut |
---|
20 | |
---|
21 | GetOptions( |
---|
22 | 'c|config=s' => \my $config, |
---|
23 | 'b|base=s' => \my $base, |
---|
24 | 's' => \my $set, |
---|
25 | 'r' => \my $remove, |
---|
26 | 'no-unexp|wo-unexp' => \my $nounexp, |
---|
27 | 'with-unexp' => \my $unexp, |
---|
28 | 'help' => sub { pod2usage(0) }, |
---|
29 | ) or pod2usage(); |
---|
30 | |
---|
31 | $set && $remove and do { |
---|
32 | warn "-s and -r cannot be used together\n"; |
---|
33 | pod2usage(); |
---|
34 | exit 1; |
---|
35 | }; |
---|
36 | |
---|
37 | my $otype = 'group'; |
---|
38 | |
---|
39 | =head1 OPTIONS |
---|
40 | |
---|
41 | =over 4 |
---|
42 | |
---|
43 | =item -c|--config configdir |
---|
44 | |
---|
45 | Use this configuration directory instead of the default one. |
---|
46 | |
---|
47 | =item -b|--base basename |
---|
48 | |
---|
49 | Query this specific base instead of the default one. |
---|
50 | |
---|
51 | =item --with-unexp |
---|
52 | |
---|
53 | Take into account all objects (even non propagated ones, with attribute |
---|
54 | 'exported'=0) (default) |
---|
55 | |
---|
56 | =item --wo-unexp |
---|
57 | |
---|
58 | Take into account only propagated objects (attribute 'exported'=1) |
---|
59 | |
---|
60 | =item -r |
---|
61 | |
---|
62 | Remove the "group" in the user(s) list instead of adding (cannot be used with -s) |
---|
63 | |
---|
64 | =item -s |
---|
65 | |
---|
66 | Set the "group" to user(s) list instead of adding (cannot be used with -r) |
---|
67 | |
---|
68 | =back |
---|
69 | |
---|
70 | =cut |
---|
71 | |
---|
72 | if (@ARGV < 2) {warn "You must specify 'group' and 'user1', aborting\n"; pod2usage(); } |
---|
73 | |
---|
74 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
75 | my $labase = $LA->base($base); |
---|
76 | $labase && $labase->load or die "Cannot load base"; |
---|
77 | |
---|
78 | $labase->unexported($nounexp ? 0 : 1); |
---|
79 | |
---|
80 | $labase->is_supported_object($otype) or die "$otype object unsupported\n"; |
---|
81 | my ($objname, @users) = @ARGV; |
---|
82 | |
---|
83 | if (my $obj = $labase->get_object($otype, $objname)) { |
---|
84 | # getting current value |
---|
85 | { |
---|
86 | my @missing = grep { |
---|
87 | !$labase->get_object('user', $_) |
---|
88 | } @users; |
---|
89 | if (@missing) { |
---|
90 | la_log(LA_ERR, "cannot find user(s) %s", join(', ', sort @missing)); |
---|
91 | exit 1; |
---|
92 | } |
---|
93 | } |
---|
94 | my @current_users = $obj->get_attributes('member'); |
---|
95 | my %uniq_users; |
---|
96 | if ($set) { |
---|
97 | foreach (@users) { |
---|
98 | $uniq_users{$_} = 1; |
---|
99 | } |
---|
100 | } else { |
---|
101 | foreach (@current_users) { |
---|
102 | $uniq_users{$_} = 1; |
---|
103 | } |
---|
104 | if ($remove) { |
---|
105 | foreach (@users) { |
---|
106 | delete $uniq_users{$_}; |
---|
107 | } |
---|
108 | } else { |
---|
109 | foreach (@users) { |
---|
110 | $uniq_users{$_} = 1; |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | my @new_users = keys %uniq_users; |
---|
115 | la_log(LA_INFO, "member was: %s", join(', ', sort @current_users)); |
---|
116 | if ($obj->set_c_fields(member => [ @new_users ])) { |
---|
117 | la_log(LA_INFO, "member are now: %s", join(', ', sort @new_users)); |
---|
118 | $labase->commit; |
---|
119 | exit 0; |
---|
120 | } else { |
---|
121 | la_log(LA_INFO, "No change done"); |
---|
122 | exit 1; |
---|
123 | } |
---|
124 | } else { |
---|
125 | la_log(LA_ERR, "Cannot find group %s", $objname); |
---|
126 | exit 1; |
---|
127 | } |
---|
128 | |
---|