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 | 'help' => sub { pod2usage(0) }, |
---|
27 | ) or pod2usage(); |
---|
28 | |
---|
29 | $set && $remove and do { |
---|
30 | warn "-s and -r cannot be used together\n"; |
---|
31 | pod2usage(); |
---|
32 | exit 1; |
---|
33 | }; |
---|
34 | |
---|
35 | my $otype = 'group'; |
---|
36 | |
---|
37 | =head1 OPTIONS |
---|
38 | |
---|
39 | =over 4 |
---|
40 | |
---|
41 | =item -c|--config configfile |
---|
42 | |
---|
43 | Use this configuration file instead the default one |
---|
44 | |
---|
45 | =item -b|--base basename |
---|
46 | |
---|
47 | Query this specific base instead default. |
---|
48 | |
---|
49 | =item -r |
---|
50 | |
---|
51 | Remove users list instead adding (cannot be used with -s) |
---|
52 | |
---|
53 | =item -s |
---|
54 | |
---|
55 | Set member list to user list instead of adding (cannot be used with -r) |
---|
56 | |
---|
57 | =back |
---|
58 | |
---|
59 | =cut |
---|
60 | |
---|
61 | my $LA = LATMOS::Accounts->new($config); |
---|
62 | my $labase = $base ? $LA->base($base) : $LA->default_base; |
---|
63 | $labase && $labase->load or die "Cannot load base"; |
---|
64 | |
---|
65 | $labase->is_supported_object($otype) or die "$otype object unsupported\n"; |
---|
66 | my ($objname, @users) = @ARGV; |
---|
67 | |
---|
68 | if (my $obj = $labase->get_object($otype, $objname)) { |
---|
69 | # getting current value |
---|
70 | { |
---|
71 | my @missing = grep { |
---|
72 | !$labase->get_object('user', $_) |
---|
73 | } @users; |
---|
74 | if (@missing) { |
---|
75 | la_log(LA_ERR, "cannot find user(s) %s", join(', ', sort @missing)); |
---|
76 | exit 1; |
---|
77 | } |
---|
78 | } |
---|
79 | my @current_users = $obj->get_attributes('member'); |
---|
80 | my %uniq_users; |
---|
81 | if ($set) { |
---|
82 | foreach (@users) { |
---|
83 | $uniq_users{$_} = 1; |
---|
84 | } |
---|
85 | } else { |
---|
86 | foreach (@current_users) { |
---|
87 | $uniq_users{$_} = 1; |
---|
88 | } |
---|
89 | if ($remove) { |
---|
90 | foreach (@users) { |
---|
91 | delete $uniq_users{$_}; |
---|
92 | } |
---|
93 | } else { |
---|
94 | foreach (@users) { |
---|
95 | $uniq_users{$_} = 1; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | my @new_users = keys %uniq_users; |
---|
100 | la_log(LA_INFO, "member was: %s", join(', ', sort @current_users)); |
---|
101 | if ($obj->set_c_fields(member => [ @new_users ])) { |
---|
102 | la_log(LA_INFO, "member are now: %s", join(', ', sort @new_users)); |
---|
103 | $labase->commit; |
---|
104 | exit 0; |
---|
105 | } else { |
---|
106 | la_log(LA_INFO, "No change done"); |
---|
107 | exit 1; |
---|
108 | } |
---|
109 | } else { |
---|
110 | la_log(LA_ERR, "Cannot find group %s", $objname); |
---|
111 | exit 1; |
---|
112 | } |
---|
113 | |
---|