[397] | 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 | |
---|
[403] | 13 | la-group -- add users to a group |
---|
[397] | 14 | |
---|
| 15 | =head1 SYNOPSIS |
---|
| 16 | |
---|
[403] | 17 | la-group [options] [-s|-r] group user1 [user2 [...]] |
---|
[397] | 18 | |
---|
| 19 | =cut |
---|
| 20 | |
---|
| 21 | GetOptions( |
---|
[849] | 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) }, |
---|
[397] | 29 | ) or pod2usage(); |
---|
| 30 | |
---|
[398] | 31 | $set && $remove and do { |
---|
| 32 | warn "-s and -r cannot be used together\n"; |
---|
| 33 | pod2usage(); |
---|
| 34 | exit 1; |
---|
| 35 | }; |
---|
| 36 | |
---|
[397] | 37 | my $otype = 'group'; |
---|
| 38 | |
---|
| 39 | =head1 OPTIONS |
---|
| 40 | |
---|
| 41 | =over 4 |
---|
| 42 | |
---|
[861] | 43 | =item -c|--config configdir |
---|
[397] | 44 | |
---|
[861] | 45 | Use this configuration directory instead of the default one. |
---|
[397] | 46 | |
---|
| 47 | =item -b|--base basename |
---|
| 48 | |
---|
[594] | 49 | Query this specific base instead of the default one. |
---|
[397] | 50 | |
---|
[849] | 51 | =item --with-unexp |
---|
[664] | 52 | |
---|
[849] | 53 | Take into account all objects (even non propagated ones, with attribute |
---|
| 54 | 'exported'=0) (default) |
---|
[664] | 55 | |
---|
[849] | 56 | =item --wo-unexp |
---|
[664] | 57 | |
---|
[849] | 58 | Take into account only propagated objects (attribute 'exported'=1) |
---|
[664] | 59 | |
---|
[398] | 60 | =item -r |
---|
| 61 | |
---|
[594] | 62 | Remove the "group" in the user(s) list instead of adding (cannot be used with -s) |
---|
[398] | 63 | |
---|
[397] | 64 | =item -s |
---|
| 65 | |
---|
[594] | 66 | Set the "group" to user(s) list instead of adding (cannot be used with -r) |
---|
[397] | 67 | |
---|
| 68 | =back |
---|
| 69 | |
---|
| 70 | =cut |
---|
| 71 | |
---|
[664] | 72 | if (@ARGV < 2) {warn "You must specify 'group' and 'user1', aborting\n"; pod2usage(); } |
---|
[655] | 73 | |
---|
[457] | 74 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
[397] | 75 | my $labase = $base ? $LA->base($base) : $LA->default_base; |
---|
| 76 | $labase && $labase->load or die "Cannot load base"; |
---|
| 77 | |
---|
[849] | 78 | $labase->unexported($nounexp ? 0 : 1); |
---|
[664] | 79 | |
---|
[397] | 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; |
---|
[398] | 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 | } |
---|
[401] | 108 | } else { |
---|
| 109 | foreach (@users) { |
---|
| 110 | $uniq_users{$_} = 1; |
---|
| 111 | } |
---|
[398] | 112 | } |
---|
[397] | 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 | |
---|