source: branches/4.0/LATMOS-Accounts/lib/LATMOS/Accounts/Maintenance.pm @ 1342

Last change on this file since 1342 was 1342, checked in by nanardon, 9 years ago

Fix expired accounts mails feature

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