source: LATMOS-Accounts/bin/la-group @ 594

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

Typos dans l'aide en ligne de commande

  • Property svn:executable set to *
  • Property svn:keywords set to Id Rev
File size: 2.5 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    '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
35my $otype = 'group';
36
37=head1 OPTIONS
38
39=over 4
40
41=item -c|--config configfile
42
43Use this configuration file instead of the default one.
44
45=item -b|--base basename
46
47Query this specific base instead of the default one.
48
49=item -r
50
51Remove the "group" in the user(s) list instead of adding (cannot be used with -s)
52
53=item -s
54
55Set the "group" to user(s) list instead of adding (cannot be used with -r)
56
57=back
58
59=cut
60
61my $LA = LATMOS::Accounts->new($config, noacl => 1);
62my $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";
66my ($objname, @users) = @ARGV;
67
68if (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
Note: See TracBrowser for help on using the repository browser.