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