source: LATMOS-Accounts/bin/la-web-directory @ 833

Last change on this file since 833 was 833, checked in by nanardon, 14 years ago
  • don't hardcode department list, make code smaller
  • Property svn:executable set to *
File size: 4.4 KB
Line 
1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use LATMOS::Accounts;
6use Getopt::Long;
7use Pod::Usage;
8use POSIX qw(strftime);
9
10=head1 NAME
11
12    la-query - Tools to query base in LATMOS::Accounts system
13
14=head1 SYNOPSIS
15
16    la-query [options] [obj_id]
17
18=item [obj_id] : If present, all set attributes (rw) will be displayed for that obj_id.
19        If none is given, all obj_ids will be printed.
20
21For the default object_type (user), obj_id = login.
22
23Example : la-query lambda
24
25=cut
26
27GetOptions(
28    'c|config=s' => \my $config,
29    'b|base=s'   => \my $base,
30    'noexp'      => \my $noexp,
31    'exp'        => \my $exp,
32    'noexpire'   => \my $noexpire,
33    'filter=s'   => \my @filters,
34    'help'       => sub { pod2usage(0) },
35) or pod2usage();
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 --noexp
50
51Take into account all objects (even non propagated ones, with attribute 'exported'=0)
52(default)
53
54=item --exp
55
56Take into account only propagated objects (attribute 'exported'=1)
57
58=item --noexpire
59
60Exclude expired accounts
61
62=cut
63
64
65my $LA = LATMOS::Accounts->new($config, noacl => 1);
66my $labase = $base ? $LA->base($base) : $LA->default_base;
67$labase && $labase->load or die "Cannot load base";
68
69$labase->wexported($exp ? 1 : 0);
70my %users;
71my $now = strftime('%Y/%m/%d', gmtime);
72my @depts = $labase->search_objects('group', 'sutype=dpmt');
73
74foreach my $user (sort $labase->search_objects('user',
75            @filters ? @filters : 'sn=*')) {
76    my $ouser = $labase->get_object('user', $user);
77    $ouser->get_attributes('sn') or next;
78    if ($noexpire && (my $expdate = $ouser->get_attributes('expireText'))) {
79        if ($now gt $expdate) {
80            next;
81        }
82    }
83
84# On ne prend que le personnel LATMOS et les heberges, sauf Novimet
85
86    my $company = $ouser->get_attributes('company');
87    my $contrat = $ouser->get_attributes('contratType');
88    if ($company){
89        if (($company ne "LATMOS")&&($contrat ne "heberges")) {
90            next;
91        }
92        if ($company eq "Novimet") {
93            next;
94        }
95    } else {
96        if ((!$contrat)||($contrat ne "heberges")) {
97            next;
98        }
99    }
100
101    my @oaddress;
102
103    if ($labase->is_supported_object('address')) {
104        @oaddress = 
105            grep { $_ } (
106            map { $labase->get_object('address', $_) }
107                $ouser->get_attributes('otheraddress'),
108            );
109    } else {
110        @oaddress = ($ouser);
111    }
112
113    # Si c'est un heberge, on place departement a "EXT"
114
115    my $department;
116
117    if ($contrat eq "heberges") {
118        $department = "EXT";
119    } else {
120        # On recherche les departements en plus de l'attribut "department"
121        # en scrutant l'ensemble des groupes de l'utilisateur
122        my @userdepts;
123        foreach my $gr ($ouser->get_attributes('memberOf')) {
124            grep { $_ eq $gr } (@depts) and push(@userdepts, $gr);
125        } 
126        $department = join(' ', @userdepts);
127    }
128           
129    my $line = join(';', map { $_ || '' } (
130            $ouser->get_attributes('sn'),
131            $ouser->get_attributes('givenName'),
132            $department,
133            ($oaddress[0] ? join(' ', map { $_ || '' }
134                    $oaddress[0]->get_attributes('l'), grep { $_ }
135                    $oaddress[0]->get_attributes('physicalDeliveryOfficeName'),
136                ) : ''),
137            ($oaddress[1] ? join(' ', map { $_ || '' } grep { $_ }
138                    $oaddress[1]->get_attributes('l'),
139                    $oaddress[1]->get_attributes('physicalDeliveryOfficeName'),
140                ) : ''),
141            ($oaddress[0] ? $oaddress[0]->get_attributes('telephoneNumber') : ''),
142            ($oaddress[1] ? $oaddress[1]->get_attributes('telephoneNumber') : ''),
143            $ouser->get_attributes('mail'),
144            $ouser->get_attributes('nickname'),
145        ));
146        $line =~s/Paris */PAR-/g;
147        $line =~s/Guyancourt */GUY-/g;
148        $line =~s/Vélizy-Villacoublay */VEL-/g;
149        $line =~s/VerriÚres le Buisson */VLB-/g;
150        $line =~s/St Maur des Fossés */STM-/g;
151        $line =~s/\+ *33 *1 *//g;
152
153    $users{$ouser->get_attributes('sn')}{$ouser->get_attributes('givenName')} = $line;
154}
155
156foreach my $sn (sort keys %users) {
157    foreach my $givenName (sort keys %{$users{$sn}}) {
158        print "$users{$sn}{$givenName}\n";
159    }
160}
Note: See TracBrowser for help on using the repository browser.