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

Last change on this file since 849 was 849, checked in by nanardon, 14 years ago
  • make unxported mode options more explicit
  • fix POD according behavior
  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 3.1 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    'no-unexp|wo-unexp' => \my $nounexp,
27    'with-exp'          => \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
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 --with-unexp
52
53Take into account all objects (even non propagated ones, with attribute
54'exported'=0) (default)
55
56=item --wo-unexp
57
58Take into account only propagated objects (attribute 'exported'=1)
59
60=item -r
61
62Remove "user" from group(s) instead of adding (cannot be used with -s)
63
64=item -s
65
66Set member list to "user" in group(s) instead of adding (cannot be used with -r)
67
68=back
69
70=cut
71
72if (@ARGV < 2) {warn "You must specify 'user' and 'group1', aborting\n"; pod2usage(); }
73
74my $LA = LATMOS::Accounts->new($config, noacl => 1);
75my $labase = $base ? $LA->base($base) : $LA->default_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";
81my ($objname, @groups) = @ARGV;
82
83if (my $obj = $labase->get_object($otype, $objname)) {
84    # getting current value
85    {
86        my @missing = grep {
87            !$labase->get_object('group', $_)
88        } @groups;
89        if (@missing) {
90            la_log(LA_ERR, "cannot find user(s) %s", join(', ', sort @missing));
91            exit 1;
92        }
93    }
94    my @current_groups = $obj->get_attributes('memberOf');
95    my %uniq_groups;
96    if ($set) {
97        foreach (@groups) {
98            $uniq_groups{$_} = 1;
99        }
100    } else {
101        foreach (@current_groups) {
102            $uniq_groups{$_} = 1;
103        }
104        if ($remove) {
105            foreach (@groups) {
106                delete $uniq_groups{$_};
107            }
108        } else {
109            foreach (@groups) {
110                $uniq_groups{$_} = 1;
111            }
112        }
113    }
114    my @new_groups = keys %uniq_groups;
115    la_log(LA_INFO, "memberOf was:     %s", join(', ', sort @current_groups));
116    if ($obj->set_c_fields(memberOf => [ @new_groups ])) {
117        la_log(LA_INFO, "memberOf are now: %s", join(', ', sort @new_groups));
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
Note: See TracBrowser for help on using the repository browser.