source: LATMOS-Accounts/bin/la-guser @ 669

Last change on this file since 669 was 669, checked in by vivat, 14 years ago

Double negation dans l'aide au lieu de simple, reformulation pour --(no)exp

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