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