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 | '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 | |
---|
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 --noexp |
---|
50 | |
---|
51 | Take into account all objects (even non propagated ones, with attribute 'exported'=0) |
---|
52 | (default) |
---|
53 | |
---|
54 | =item --exp |
---|
55 | |
---|
56 | Take into account only propagated objects (attribute 'exported'=1) |
---|
57 | |
---|
58 | =item --noexpire |
---|
59 | |
---|
60 | Exclude expired accounts |
---|
61 | |
---|
62 | =cut |
---|
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->wexported($exp ? 1 : 0); |
---|
69 | my %users; |
---|
70 | my $now = strftime('%Y/%m/%d', gmtime); |
---|
71 | |
---|
72 | foreach my $user (sort $labase->search_objects('user', |
---|
73 | @filters ? @filters : 'sn=*')) { |
---|
74 | my $ouser = $labase->get_object('user', $user); |
---|
75 | $ouser->get_attributes('sn') or next; |
---|
76 | if ($noexpire && (my $expdate = $ouser->get_attributes('expireText'))) { |
---|
77 | if ($now gt $expdate) { |
---|
78 | next; |
---|
79 | } |
---|
80 | } |
---|
81 | my @oaddress; |
---|
82 | |
---|
83 | if ($labase->is_supported_object('address')) { |
---|
84 | @oaddress = |
---|
85 | grep { $_ } ( |
---|
86 | map { $labase->get_object('address', $_) } |
---|
87 | $ouser->get_attributes('otheraddress'), |
---|
88 | ); |
---|
89 | } else { |
---|
90 | @oaddress = ($ouser); |
---|
91 | } |
---|
92 | |
---|
93 | my $line = join(';', map { $_ || '' } ( |
---|
94 | $ouser->get_attributes('sn'), |
---|
95 | $ouser->get_attributes('givenName'), |
---|
96 | $ouser->get_attributes('department'), |
---|
97 | ($oaddress[0] ? join(' ', map { $_ || '' } |
---|
98 | $oaddress[0]->get_attributes('l'), grep { $_ } |
---|
99 | $oaddress[0]->get_attributes('physicalDeliveryOfficeName'), |
---|
100 | ) : ''), |
---|
101 | ($oaddress[1] ? join(' ', map { $_ || '' } grep { $_ } |
---|
102 | $oaddress[1]->get_attributes('l'), |
---|
103 | $oaddress[1]->get_attributes('physicalDeliveryOfficeName'), |
---|
104 | ) : ''), |
---|
105 | ($oaddress[0] ? $oaddress[0]->get_attributes('telephoneNumber') : ''), |
---|
106 | ($oaddress[1] ? $oaddress[1]->get_attributes('telephoneNumber') : ''), |
---|
107 | $ouser->get_attributes('mail'), |
---|
108 | $ouser->get_attributes('nickname'), |
---|
109 | )); |
---|
110 | $line =~s/Paris */PAR-/g; |
---|
111 | $line =~s/Guyancourt */GUY-/g; |
---|
112 | $line =~s/Vélizy-Villacoublay */VEL-/g; |
---|
113 | $line =~s/VerriÚres le Buisson */VLB-/g; |
---|
114 | $line =~s/St Maur des Fossés */STM-/g; |
---|
115 | $line =~s/\+ *33 *1 *//g; |
---|
116 | |
---|
117 | $users{$ouser->get_attributes('sn')}{$ouser->get_attributes('givenName')} = $line; |
---|
118 | } |
---|
119 | |
---|
120 | foreach my $sn (sort keys %users) { |
---|
121 | foreach my $givenName (sort keys %{$users{$sn}}) { |
---|
122 | print "$users{$sn}{$givenName}\n"; |
---|
123 | } |
---|
124 | } |
---|