1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use LATMOS::Accounts; |
---|
6 | use Getopt::Long; |
---|
7 | use Pod::Usage; |
---|
8 | use 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 | |
---|
21 | For the default object_type (user), obj_id = login. |
---|
22 | |
---|
23 | Example : la-query lambda |
---|
24 | |
---|
25 | =cut |
---|
26 | |
---|
27 | GetOptions( |
---|
28 | 'c|config=s' => \my $config, |
---|
29 | 'b|base=s' => \my $base, |
---|
30 | 'wo-unexp|no-unexp' => \my $nounexp, |
---|
31 | 'with-unexp' => \my $unexp, |
---|
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 | |
---|
43 | Use this configuration file instead of the default one. |
---|
44 | |
---|
45 | =item -b|--base basename |
---|
46 | |
---|
47 | Query this specific base instead of the default one. |
---|
48 | |
---|
49 | =item --with-unexp |
---|
50 | |
---|
51 | Take into account all objects (even non propagated ones, with attribute 'exported'=0) |
---|
52 | |
---|
53 | =item --wo-unexp |
---|
54 | |
---|
55 | Take into account only propagated objects (attribute 'exported'=1) (default) |
---|
56 | |
---|
57 | =item --noexpire |
---|
58 | |
---|
59 | Exclude expired accounts |
---|
60 | |
---|
61 | =cut |
---|
62 | |
---|
63 | |
---|
64 | my $LA = LATMOS::Accounts->new($config, noacl => 1); |
---|
65 | my $labase = $base ? $LA->base($base) : $LA->default_base; |
---|
66 | $labase && $labase->load or die "Cannot load base"; |
---|
67 | |
---|
68 | $labase->unexported($unexp ? 1 : 0); |
---|
69 | my %users; |
---|
70 | my $now = strftime('%Y/%m/%d', gmtime); |
---|
71 | my @depts = $labase->search_objects('group', 'sutype=dpmt'); |
---|
72 | |
---|
73 | foreach my $user (sort $labase->search_objects('user', |
---|
74 | @filters ? @filters : 'sn=*')) { |
---|
75 | my $ouser = $labase->get_object('user', $user); |
---|
76 | $ouser->get_attributes('sn') or next; |
---|
77 | if ($noexpire && (my $expdate = $ouser->get_attributes('expireText'))) { |
---|
78 | if ($now gt $expdate) { |
---|
79 | next; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | # On ne prend que le personnel LATMOS et les heberges, sauf Novimet |
---|
84 | |
---|
85 | my $company = $ouser->get_attributes('company'); |
---|
86 | my $contrat = $ouser->get_attributes('contratType'); |
---|
87 | if ($company){ |
---|
88 | if (($company ne "LATMOS")&&($contrat ne "heberges")) { |
---|
89 | next; |
---|
90 | } |
---|
91 | if ($company eq "Novimet") { |
---|
92 | next; |
---|
93 | } |
---|
94 | } else { |
---|
95 | if ((!$contrat)||($contrat ne "heberges")) { |
---|
96 | next; |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | my @oaddress; |
---|
101 | |
---|
102 | if ($labase->is_supported_object('address')) { |
---|
103 | @oaddress = |
---|
104 | grep { $_ } ( |
---|
105 | map { $labase->get_object('address', $_) } |
---|
106 | $ouser->get_attributes('otheraddress'), |
---|
107 | ); |
---|
108 | } else { |
---|
109 | @oaddress = ($ouser); |
---|
110 | } |
---|
111 | |
---|
112 | # Si c'est un heberge, on place departement a "EXT" |
---|
113 | |
---|
114 | my $department; |
---|
115 | |
---|
116 | if ($contrat eq "heberges") { |
---|
117 | $department = "EXT"; |
---|
118 | } else { |
---|
119 | # On recherche les departements en plus de l'attribut "department" |
---|
120 | # en scrutant l'ensemble des groupes de l'utilisateur |
---|
121 | my @userdepts; |
---|
122 | foreach my $gr ($ouser->get_attributes('memberOf')) { |
---|
123 | grep { $_ eq $gr } (@depts) and push(@userdepts, $gr); |
---|
124 | } |
---|
125 | $department = join(' ', @userdepts); |
---|
126 | } |
---|
127 | |
---|
128 | my $line = join(';', map { $_ || '' } ( |
---|
129 | $ouser->get_attributes('sn'), |
---|
130 | $ouser->get_attributes('givenName'), |
---|
131 | $department, |
---|
132 | ($oaddress[0] ? join(' ', map { $_ || '' } |
---|
133 | $oaddress[0]->get_attributes('l'), grep { $_ } |
---|
134 | $oaddress[0]->get_attributes('physicalDeliveryOfficeName'), |
---|
135 | ) : ''), |
---|
136 | ($oaddress[1] ? join(' ', map { $_ || '' } grep { $_ } |
---|
137 | $oaddress[1]->get_attributes('l'), |
---|
138 | $oaddress[1]->get_attributes('physicalDeliveryOfficeName'), |
---|
139 | ) : ''), |
---|
140 | ($oaddress[0] ? $oaddress[0]->get_attributes('telephoneNumber') : ''), |
---|
141 | ($oaddress[1] ? $oaddress[1]->get_attributes('telephoneNumber') : ''), |
---|
142 | $ouser->get_attributes('mail'), |
---|
143 | $ouser->get_attributes('nickname'), |
---|
144 | )); |
---|
145 | $line =~s/Paris */PAR-/g; |
---|
146 | $line =~s/Guyancourt */GUY-/g; |
---|
147 | $line =~s/Vélizy-Villacoublay */VEL-/g; |
---|
148 | $line =~s/VerriÚres le Buisson */VLB-/g; |
---|
149 | $line =~s/St Maur des Fossés */STM-/g; |
---|
150 | $line =~s/\+ *33 *1 *//g; |
---|
151 | |
---|
152 | $users{$ouser->get_attributes('sn')}{$ouser->get_attributes('givenName')} = $line; |
---|
153 | } |
---|
154 | |
---|
155 | foreach my $sn (sort keys %users) { |
---|
156 | foreach my $givenName (sort keys %{$users{$sn}}) { |
---|
157 | print "$users{$sn}{$givenName}\n"; |
---|
158 | } |
---|
159 | } |
---|