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

Last change on this file since 1950 was 1763, checked in by nanardon, 8 years ago

Don't manage entrance anymore in expiration warning

  • Property svn:keywords set to Id Rev
File size: 9.7 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 @summary;
106    foreach my $user ($self->find_next_expire_users($options{delay})) {
107        if ($options{users} && ! grep { $_ eq $user->{name} } @{$options{users}}) {
108            next;
109        }
110        my %mail = (
111            From => $self->val('_default_', 'mailFrom', 'nomail@localhost'),
112            Subject => sprintf('Account %s Expire in %s days', $user->{name}, $user->{days}),
113            'X-LATMOS-Reason' => 'Account expiration',
114        );
115        my ($manager, $mail) = ($user->{obj}->get_c_field('manager'),
116            $user->{obj}->get_c_field('mail'));
117        my $managermail = $manager ? $user->{obj}->base->
118            get_object('user', $manager)->get_c_field('mail') : undef;
119 
120        # if user have no mail, mail only to manager, avoiding empty To
121        # NB: at time, for testing purpose, mail is not really sent
122        my ($to, @cc) = grep { $_ } ($mail, $managermail,
123            $self->val('_default_', 'allwayscc'));
124        if ($options{to}) {
125            $mail{to} = $options{to};
126        } else {
127            $mail{to} = $to;
128            $mail{cc} = join(', ', @cc);
129        }
130        $mail{to} or do {
131            la_log(LA_ERR, 
132                "Cannot send expiration for `%s', no mail to send to",
133                $user->{obj}->id,
134            );
135            next;
136        };
137        my $mailcc = join(', ', @cc) || '';
138        push(@summary, sprintf("%s : %s : %s\n",
139                $user->{obj}->queryformat('%{sn} %{givenName} : %{name} : %{department} : %{manager} : %{expireText} : %{endcircuit}'),
140                $to || 'Not sent, no destination',
141                ($mailcc ? $mailcc : ''),
142            )
143        );
144        my $message;
145        if ($lamail->process(\%mail, $user)) {
146            la_log(LA_NOTICE, "Expiration mail for %s (%s) sent to %s; cc %s",
147                $user->{obj}->id,
148                $user->{days},
149                $mail{to}, ($mail{cc} || ''));
150            if ($options{to}) {
151                la_log(LA_NOTICE,"\tbut sent to %s", $mail{to});
152            }
153        } 
154    }
155
156    if (@summary) {
157        if ($options{test}) {
158            print join('', @summary);
159        } else {
160            if ($self->val('_default_', 'expire_summary_to')) {
161                my $summail = LATMOS::Accounts::Mail->new(
162                    $self,
163                    \join('', @summary),
164                );
165                my %mail = (
166                );
167                if ($summail->process({
168                    Subject => 'LATMOS account expiration summary',
169                    To => $self->val('_default_', 'expire_summary_to'),
170                })) {
171                    la_log(
172                        LA_NOTICE,
173                        "Expiration summary mail sent to %s",
174                        $self->val('_default_', 'expire_summary_to'),
175                    );
176                }
177            }
178        }
179    }
180
181    1;
182}
183
184=head2 find_expired_users ($expire)
185
186See L<LATMOS::Accounts::Base/find_expired_users>
187
188=cut
189
190sub find_expired_users {
191    my ($self, $expire) = @_;
192    $self->_base->find_expired_users($expire);
193}
194
195=head2 expired_account_reminder ( %options)
196
197Search account expired for more than C<$options{delay}> (default is 6 month)
198send mail to manager and summary to admin to aknoledge destruction.
199
200=cut
201
202sub expired_account_reminder {
203    my ($self, %options) = @_;
204    $options{delay} ||= '6 month';
205
206    my $lamail = LATMOS::Accounts::Mail->new(
207        $self,
208        'account_expired_reminder.mail',
209    );
210
211    my @users = $self->_base->find_expired_users($options{delay});
212
213    my %managers;
214    my $accreq = $self->_base->get_object('accreq', 'user-removal');
215
216
217    $self->base->log(LA_DEBUG,
218        "Found accreq 'user-removal', using it to automated deletion",
219    ) if ($accreq);
220
221    foreach my $user (@users) {
222        my $uobj = $self->_base->get_object('user', $user);
223        $uobj->get_attributes('unexported') and next; # can't happend
224        my $manager = $uobj->get_attributes('manager') || 'N/A';
225        push(@{$managers{$manager}{users}}, $uobj);
226
227        if ($accreq) {
228            my $req = LATMOS::Accounts::Bases::Sql::DataRequest->new($accreq);
229            $req->set_ptr_object($uobj);
230            my @date = localtime( time + 3600 * 24 * 30); # eg: 1 month
231            my $apply_date = sprintf(
232                '%02d/%02d/%d',
233                $date[3],
234                $date[4] + 1,
235                $date[5] + 1900
236            );
237
238            if ($self->_base->list_request_by_object(
239                    'user', $user, 'user-removal')) {
240                $self->base->log(LA_NOTICE,
241                    "Request %s already exists for %s, skipping",
242                    'accreq',
243                    $user,
244                );
245            } else {
246                $req->register(
247                    {
248                        user => undef,
249                        apply => $apply_date,
250                        auto => 1,
251                    },
252                    exported => 0,
253                );
254                $self->_base->commit;
255            }
256        }
257    }
258
259    unless($options{test}) {
260        foreach (keys %managers) {
261            my $oman = $self->_base->get_object('user', $_) or next; # can't happend
262            $managers{$_}{manager} = $oman;
263            my $mail = $oman->get_attributes('mail') or next;
264
265            my %mail = (
266                Subject => 'LATMOS expired account',
267                'X-LATMOS-Reason' => 'Account destruction',
268            );
269            $mail{to} = $options{to} || $mail;
270            if ($lamail->process(\%mail, $managers{$oman->id})) {
271                la_log(LA_NOTICE,
272                    "Expired account reminder mail for %s sent to %s (cc: %s) for %s",
273                    $oman->id,
274                    $mail{to},
275                    ($mail{cc} || ''),
276                    join(', ', map { $_->id } @{$managers{$oman->id}{users}})
277                );
278            }
279        }
280    }
281    my @summary;
282    foreach my $manager (sort keys %managers) {
283        push(@summary, "\n" . (
284            $managers{$manager}{manager}
285                ? $managers{$manager}{manager}->get_attributes('displayName')
286                : $manager) . "\n");
287        foreach (@{$managers{$manager}{users}}) {
288            push(@summary, sprintf("  %s - %s (%s)\n",
289                $_->id,
290                $_->get_attributes('displayName'),
291                $_->get_attributes('expireText'),
292            ));
293        }
294    }
295
296    if (@summary) {
297        if ($options{test}) {
298            print join('', @summary);
299        } else {
300            if ($self->val('_default_', 'expire_summary_to')) {
301                my %mail = (
302                    Subject => 'LATMOS expired account (to disable)',
303                    'X-LATMOS-Reason' => 'Account expiration',
304                    To => $self->val('_default_', 'expire_summary_to'),
305                );
306                my $summail = LATMOS::Accounts::Mail->new(
307                    $self, \join('', @summary), {}
308                );
309                if ($summail->process(\%mail)) {
310                    la_log(LA_NOTICE, "Expiration summary mail sent to %s",
311                        $self->val('_default_', 'expire_summary_to'),
312                    );
313                }
314            }
315        }
316    }
317}
318
3191;
320
321__END__
322
323=head1 SEE ALSO
324
325L<LATMOS::Accounts::Bases>
326
327=head1 AUTHOR
328
329Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
330
331=head1 COPYRIGHT AND LICENSE
332
333Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier
334
335This library is free software; you can redistribute it and/or modify
336it under the same terms as Perl itself, either Perl version 5.10.0 or,
337at your option, any later version of Perl 5 you may have available.
338
339=cut
Note: See TracBrowser for help on using the repository browser.