1 | package LATMOS::Accounts::Maintenance; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base qw(LATMOS::Accounts); |
---|
6 | use LATMOS::Accounts::Log; |
---|
7 | use LATMOS::Accounts::Bases::Sql::DataRequest; |
---|
8 | use LATMOS::Accounts::Mail; |
---|
9 | use FindBin qw($Bin); |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::Accounts::Maintenance |
---|
14 | |
---|
15 | =head1 FUNCTIONS |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | sub _base { |
---|
20 | my ($self) = @_; |
---|
21 | return $self->{_maintenance_base} if ($self->{_maintenance_base}); |
---|
22 | my $base = $self->SUPER::base; |
---|
23 | $base->type eq 'sql' or die "This module work only with SQL base type\n"; |
---|
24 | return $self->{_maintenance_base} = $base |
---|
25 | } |
---|
26 | |
---|
27 | =head2 find_next_expire_users ($expire) |
---|
28 | |
---|
29 | Return The list of users going to expire in C<$expire> delay. |
---|
30 | |
---|
31 | =cut |
---|
32 | |
---|
33 | sub find_next_expire_users { |
---|
34 | # Do not replace this code by $base->find_next_expire_users |
---|
35 | # it does not exactly the same thing |
---|
36 | my ($self, $expire) = @_; |
---|
37 | my $base = $self->_base; |
---|
38 | |
---|
39 | my $sth= $base->db->prepare(q{ |
---|
40 | select name, justify_hours(expire - now()) as delay from "user" where |
---|
41 | expire < now() + ?::interval |
---|
42 | and expire > now() |
---|
43 | and expire is not null |
---|
44 | and exported = True |
---|
45 | order by expire |
---|
46 | } |
---|
47 | ); |
---|
48 | $sth->execute($expire || '1 month'); |
---|
49 | my @users; |
---|
50 | while (my $res = $sth->fetchrow_hashref) { |
---|
51 | $res->{delay} =~ s/ day.? .*//; |
---|
52 | $res->{obj} = $base->get_object('user', $res->{name}); |
---|
53 | $res->{obj}->get_attributes('locked') and next; |
---|
54 | push(@users, $res); |
---|
55 | } |
---|
56 | @users |
---|
57 | } |
---|
58 | |
---|
59 | =head2 warn_next_expire_users(%options) |
---|
60 | |
---|
61 | Send a mail to user having account expiring soon |
---|
62 | |
---|
63 | C<%options are> |
---|
64 | |
---|
65 | =over 4 |
---|
66 | |
---|
67 | =item users => [] |
---|
68 | |
---|
69 | Warn only this users (if need) |
---|
70 | |
---|
71 | =item to |
---|
72 | |
---|
73 | Send the only to this person. |
---|
74 | |
---|
75 | =back |
---|
76 | |
---|
77 | =cut |
---|
78 | |
---|
79 | sub warn_next_expire_users { |
---|
80 | my ($self, %options) = @_; |
---|
81 | |
---|
82 | require LATMOS::Accounts::Mail; |
---|
83 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
84 | $self, |
---|
85 | 'account_expire.mail', |
---|
86 | ); |
---|
87 | |
---|
88 | my @summary; |
---|
89 | foreach my $user ($self->find_next_expire_users($options{delay})) { |
---|
90 | if ($options{users} && ! grep { $_ eq $user->{name} } @{$options{users}}) { |
---|
91 | next; |
---|
92 | } |
---|
93 | my %mail = ( |
---|
94 | From => $self->val('_default_', 'mailFrom', 'nomail@localhost'), |
---|
95 | Subject => sprintf('Account %s Expire in %s days', $user->{name}, $user->{delay}), |
---|
96 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
97 | ); |
---|
98 | my ($manager, $mail) = ($user->{obj}->get_c_field('managerContact'), |
---|
99 | $user->{obj}->get_c_field('mail')); |
---|
100 | my $managermail = $manager ? $user->{obj}->base-> |
---|
101 | get_object('user', $manager)->get_c_field('mail') : undef; |
---|
102 | |
---|
103 | # if user have no mail, mail only to manager, avoiding empty To |
---|
104 | # NB: at time, for testing purpose, mail is not really sent |
---|
105 | my ($to, @cc) = grep { $_ } ($mail, $managermail, |
---|
106 | $self->val('_default_', 'allwayscc')); |
---|
107 | if ($options{to}) { |
---|
108 | $mail{to} = $options{to}; |
---|
109 | } else { |
---|
110 | $mail{to} = $to; |
---|
111 | $mail{cc} = join(', ', @cc); |
---|
112 | } |
---|
113 | $mail{to} or do { |
---|
114 | la_log(LA_ERR, |
---|
115 | "Cannot send expiration for `%s', no mail to send to", |
---|
116 | $user->{obj}->id, |
---|
117 | ); |
---|
118 | next; |
---|
119 | }; |
---|
120 | my $mailcc = join(', ', @cc) || ''; |
---|
121 | push(@summary, sprintf("%s : %s : %s\n", |
---|
122 | $user->{obj}->queryformat('%{sn} %{givenName} : %{name} : %{department} : %{managerContact} : %{expireText}'), |
---|
123 | $to || 'Not sent, no destination', |
---|
124 | ($mailcc ? $mailcc : ''), |
---|
125 | ) |
---|
126 | ); |
---|
127 | my $message; |
---|
128 | if ($lamail->process(\%mail, $user)) { |
---|
129 | la_log(LA_NOTICE, "Expiration mail for %s (%s) sent to %s; cc %s", |
---|
130 | $user->{obj}->id, |
---|
131 | $user->{delay}, |
---|
132 | $mail{to}, ($mail{cc} || '')); |
---|
133 | if ($options{to}) { |
---|
134 | la_log(LA_NOTICE,"\tbut sent to %s", $mail{to}); |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | if (@summary) { |
---|
140 | if ($options{test}) { |
---|
141 | print join('', @summary); |
---|
142 | } else { |
---|
143 | if ($self->val('_default_', 'expire_summary_to')) { |
---|
144 | my $summail = LATMOS::Accounts::Mail->new( |
---|
145 | $self, |
---|
146 | \join('', @summary), |
---|
147 | ); |
---|
148 | my %mail = ( |
---|
149 | ); |
---|
150 | if ($summail->process({ |
---|
151 | Subject => 'LATMOS account expiration summary', |
---|
152 | To => $self->val('_default_', 'expire_summary_to'), |
---|
153 | })) { |
---|
154 | la_log( |
---|
155 | LA_NOTICE, |
---|
156 | "Expiration summary mail sent to %s", |
---|
157 | $self->val('_default_', 'expire_summary_to'), |
---|
158 | ); |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | } |
---|
163 | |
---|
164 | 1; |
---|
165 | } |
---|
166 | |
---|
167 | =head2 find_expired_users ($expire) |
---|
168 | |
---|
169 | See L<LATMOS::Accounts::Base/find_expired_users> |
---|
170 | |
---|
171 | =cut |
---|
172 | |
---|
173 | sub find_expired_users { |
---|
174 | my ($self, $expire) = @_; |
---|
175 | $self->_base->find_expired_users($expire); |
---|
176 | } |
---|
177 | |
---|
178 | =head2 expired_account_reminder ( %options) |
---|
179 | |
---|
180 | Search account expired for more than C<$options{delay}> (default is 6 month) |
---|
181 | send mail to manager and summary to admin to aknoledge destruction. |
---|
182 | |
---|
183 | =cut |
---|
184 | |
---|
185 | sub expired_account_reminder { |
---|
186 | my ($self, %options) = @_; |
---|
187 | $options{delay} ||= '6 month'; |
---|
188 | |
---|
189 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
190 | $self, |
---|
191 | 'account_expired_reminder.mail', |
---|
192 | ); |
---|
193 | |
---|
194 | my @users = $self->_base->find_expired_users($options{delay}); |
---|
195 | |
---|
196 | my %managers; |
---|
197 | if (my $accreq = $self->_base->get_object('accreq', 'user-removal')) { |
---|
198 | |
---|
199 | $self->base->log(LA_DEBUG, |
---|
200 | "Found accreq 'user-removal', using it to automated deletion", |
---|
201 | ); |
---|
202 | foreach my $user (@users) { |
---|
203 | my $uobj = $self->_base->get_object('user', $user); |
---|
204 | $uobj->get_attributes('unexported') and next; # can't happend |
---|
205 | my $manager = $uobj->get_attributes('managerContact') || 'N/A'; |
---|
206 | push(@{$managers{$manager}{users}}, $uobj); |
---|
207 | |
---|
208 | my $req = LATMOS::Accounts::Bases::Sql::DataRequest->new($accreq); |
---|
209 | $req->set_ptr_object($uobj); |
---|
210 | my @date = localtime( time + 3600 * 24 * 30); # eg: 1 month |
---|
211 | my $apply_date = sprintf( |
---|
212 | '%02d/%02d/%d', |
---|
213 | $date[3], |
---|
214 | $date[4] + 1, |
---|
215 | $date[5] + 1900 |
---|
216 | ); |
---|
217 | |
---|
218 | if ($self->_base->list_request_by_object( |
---|
219 | 'user', $user, 'user-removal')) { |
---|
220 | $self->base->log(LA_NOTICE, |
---|
221 | "Request %s already exists for %s, skipping", |
---|
222 | 'accreq', |
---|
223 | $user, |
---|
224 | ); |
---|
225 | } else { |
---|
226 | $req->register( |
---|
227 | { |
---|
228 | user => undef, |
---|
229 | apply => $apply_date, |
---|
230 | auto => 1, |
---|
231 | }, |
---|
232 | exported => 0, |
---|
233 | ); |
---|
234 | $self->_base->commit; |
---|
235 | } |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | unless($options{test}) { |
---|
240 | foreach (keys %managers) { |
---|
241 | my $oman = $self->_base->get_object('user', $_) or next; # can't happend |
---|
242 | $managers{$_}{manager} = $oman; |
---|
243 | my $mail = $oman->get_attributes('mail') or next; |
---|
244 | |
---|
245 | my %mail = ( |
---|
246 | Subject => 'LATMOS expired account', |
---|
247 | 'X-LATMOS-Reason' => 'Account destruction', |
---|
248 | ); |
---|
249 | $mail{to} = $options{to} || $mail; |
---|
250 | if ($lamail->process(\%mail, $managers{$oman->id})) { |
---|
251 | la_log(LA_NOTICE, |
---|
252 | "Expired account reminder mail for %s sent to %s (cc: %s) for %s", |
---|
253 | $oman->id, |
---|
254 | $mail{to}, |
---|
255 | ($mail{cc} || ''), |
---|
256 | join(', ', map { $_->id } @{$managers{$oman->id}{users}}) |
---|
257 | ); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | my @summary; |
---|
262 | foreach my $manager (sort keys %managers) { |
---|
263 | push(@summary, "\n" . ( |
---|
264 | $managers{$manager}{manager} |
---|
265 | ? $managers{$manager}{manager}->get_attributes('displayName') |
---|
266 | : $manager) . "\n"); |
---|
267 | foreach (@{$managers{$manager}{users}}) { |
---|
268 | push(@summary, sprintf(" %s - %s (%s)\n", |
---|
269 | $_->id, |
---|
270 | $_->get_attributes('displayName'), |
---|
271 | $_->get_attributes('expireText'), |
---|
272 | )); |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | if (@summary) { |
---|
277 | if ($options{test}) { |
---|
278 | print join('', @summary); |
---|
279 | } else { |
---|
280 | if ($self->val('_default_', 'expire_summary_to')) { |
---|
281 | my %mail = ( |
---|
282 | Subject => 'LATMOS expired account (to disable)', |
---|
283 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
284 | To => $self->val('_default_', 'expire_summary_to'), |
---|
285 | ); |
---|
286 | my $summail = LATMOS::Accounts::Mail->new( |
---|
287 | $self, \join('', @summary), {} |
---|
288 | ); |
---|
289 | if ($summail->process(\%mail)) { |
---|
290 | la_log(LA_NOTICE, "Expiration summary mail sent to %s", |
---|
291 | $self->val('_default_', 'expire_summary_to'), |
---|
292 | ); |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | 1; |
---|
300 | |
---|
301 | __END__ |
---|
302 | |
---|
303 | =head1 SEE ALSO |
---|
304 | |
---|
305 | L<LATMOS::Accounts::Bases> |
---|
306 | |
---|
307 | =head1 AUTHOR |
---|
308 | |
---|
309 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
310 | |
---|
311 | =head1 COPYRIGHT AND LICENSE |
---|
312 | |
---|
313 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
314 | |
---|
315 | This library is free software; you can redistribute it and/or modify |
---|
316 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
317 | at your option, any later version of Perl 5 you may have available. |
---|
318 | |
---|
319 | =cut |
---|