source: LATMOS-Accounts/bin/la-group @ 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: 2.9 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-group -- add users to a group
14
15=head1 SYNOPSIS
16
17    la-group [options] [-s|-r] group user1 [user2 [...]]
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-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
37my $otype = 'group';
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 the "group" in the user(s) list instead of adding (cannot be used with -s)
63
64=item -s
65
66Set the "group" to user(s) list instead of adding (cannot be used with -r)
67
68=back
69
70=cut
71
72if (@ARGV < 2) {warn "You must specify 'group' and 'user1', 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, @users) = @ARGV;
82
83if (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
Note: See TracBrowser for help on using the repository browser.