source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Maintenance.pm @ 1958

Last change on this file since 1958 was 1958, checked in by nanardon, 7 years ago

Allow to change mail's subjects from parameter config

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