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

Last change on this file since 1253 was 1253, checked in by nanardon, 10 years ago

fix expired account discovery

  • Property svn:keywords set to Id Rev
File size: 9.5 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);
10
11=head1 NAME
12
13    LATMOS::Accounts::Maintenance
14
15=head1 FUNCTIONS
16
17=cut
18
19sub _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
29Return The list of users going to expire in C<$expire> delay.
30
31=cut
32
33sub 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
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
79sub 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
169See L<LATMOS::Accounts::Base/find_expired_users>
170
171=cut
172
173sub find_expired_users {
174    my ($self, $expire) = @_;
175    $self->_base->find_expired_users($expire);
176}
177
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
185sub 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    foreach my $user (@users) {
198        my $uobj = $self->_base->get_object('user', $user);
199        $uobj->get_attributes('unexported') and next; # can't happend
200        my $manager = $uobj->get_attributes('managerContact') || 'N/A';
201        push(@{$managers{$manager}{users}}, $uobj);
202    }
203
204    if (my $accreq = $self->_base->get_object('accreq', 'user-removal')) {
205
206        $self->base->log(LA_DEBUG,
207            "Found accreq 'user-removal', using it to automated deletion",
208        );
209        foreach my $user (@users) {
210            my $uobj = $self->_base->get_object('user', $user);
211
212            my $req = LATMOS::Accounts::Bases::Sql::DataRequest->new($accreq);
213            $req->set_ptr_object($uobj);
214            my @date = localtime( time + 3600 * 24 * 30); # eg: 1 month
215            my $apply_date = sprintf(
216                '%02d/%02d/%d',
217                $date[3],
218                $date[4] + 1,
219                $date[5] + 1900
220            );
221
222            if ($self->_base->list_request_by_object(
223                    'user', $user, 'user-removal')) {
224                $self->base->log(LA_NOTICE,
225                    "Request %s already exists for %s, skipping",
226                    'accreq',
227                    $user,
228                );
229            } else {
230                $req->register(
231                    {
232                        user => undef,
233                        apply => $apply_date,
234                        auto => 1,
235                    },
236                    exported => 0,
237                );
238                $self->_base->commit;
239            }
240        }
241    }
242
243    unless($options{test}) {
244        foreach (keys %managers) {
245            my $oman = $self->_base->get_object('user', $_) or next; # can't happend
246            $managers{$_}{manager} = $oman;
247            my $mail = $oman->get_attributes('mail') or next;
248
249            my %mail = (
250                Subject => 'LATMOS expired account',
251                'X-LATMOS-Reason' => 'Account destruction',
252            );
253            $mail{to} = $options{to} || $mail;
254            if ($lamail->process(\%mail, $managers{$oman->id})) {
255                la_log(LA_NOTICE,
256                    "Expired account reminder mail for %s sent to %s (cc: %s) for %s",
257                    $oman->id,
258                    $mail{to},
259                    ($mail{cc} || ''),
260                    join(', ', map { $_->id } @{$managers{$oman->id}{users}})
261                );
262            }
263        }
264    }
265    my @summary;
266    foreach my $manager (sort keys %managers) {
267        push(@summary, "\n" . (
268            $managers{$manager}{manager}
269                ? $managers{$manager}{manager}->get_attributes('displayName')
270                : $manager) . "\n");
271        foreach (@{$managers{$manager}{users}}) {
272            push(@summary, sprintf("  %s - %s (%s)\n",
273                $_->id,
274                $_->get_attributes('displayName'),
275                $_->get_attributes('expireText'),
276            ));
277        }
278    }
279
280    if (@summary) {
281        if ($options{test}) {
282            print join('', @summary);
283        } else {
284            if ($self->val('_default_', 'expire_summary_to')) {
285                my %mail = (
286                    Subject => 'LATMOS expired account (to disable)',
287                    'X-LATMOS-Reason' => 'Account expiration',
288                    To => $self->val('_default_', 'expire_summary_to'),
289                );
290                my $summail = LATMOS::Accounts::Mail->new(
291                    $self, \join('', @summary), {}
292                );
293                if ($summail->process(\%mail)) {
294                    la_log(LA_NOTICE, "Expiration summary mail sent to %s",
295                        $self->val('_default_', 'expire_summary_to'),
296                    );
297                }
298            }
299        }
300    }
301}
302
3031;
304
305__END__
306
307=head1 SEE ALSO
308
309L<LATMOS::Accounts::Bases>
310
311=head1 AUTHOR
312
313Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
314
315=head1 COPYRIGHT AND LICENSE
316
317Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier
318
319This library is free software; you can redistribute it and/or modify
320it under the same terms as Perl itself, either Perl version 5.10.0 or,
321at your option, any later version of Perl 5 you may have available.
322
323=cut
Note: See TracBrowser for help on using the repository browser.