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

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

Merge branch 'master' into sqlup

  • Property svn:keywords set to Id Rev
File size: 8.7 KB
Line 
1package LATMOS::Accounts::Maintenance;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts);
6use LATMOS::Accounts::Log;
7use LATMOS::Accounts::Mail;
8use FindBin qw($Bin);
9use DateTime;
10use Date::Calc;
11use Date::Parse;
12
13=head1 NAME
14
15    LATMOS::Accounts::Maintenance
16
17=head1 FUNCTIONS
18
19=cut
20
21sub _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
31Return The list of users going to expire in C<$expire> delay.
32
33=cut
34
35sub 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
77Send a mail to user having account expiring soon
78
79C<%options are>
80
81=over 4
82
83=item users => []
84
85Warn only this users (if need)
86
87=item to
88
89Send the only to this person.
90
91=back
92
93=cut
94
95sub 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 => sprintf("$MailSubject: Account %s Expire in %s days", $user->{name}, $user->{days}),
115            'X-LATMOS-Reason' => 'Account expiration',
116        );
117        my ($manager, $mail) = ($user->{obj}->get_c_field('manager'),
118            $user->{obj}->get_c_field('mail'));
119        my $managermail = $manager ? $user->{obj}->base->
120            get_object('user', $manager)->get_c_field('mail') : undef;
121 
122        # if user have no mail, mail only to manager, avoiding empty To
123        # NB: at time, for testing purpose, mail is not really sent
124        my ($to, @cc) = grep { $_ } ($mail, $managermail,
125            $self->val('_default_', 'allwayscc'));
126        if ($options{to}) {
127            $mail{to} = $options{to};
128        } else {
129            $mail{to} = $to;
130            $mail{cc} = join(', ', @cc);
131        }
132        $mail{to} or do {
133            la_log(LA_ERR, 
134                "Cannot send expiration for `%s', no mail to send to",
135                $user->{obj}->id,
136            );
137            next;
138        };
139        my $mailcc = join(', ', @cc) || '';
140        push(@summary, sprintf("%s : %s : %s\n",
141                $user->{obj}->queryformat('%{sn} %{givenName} : %{name} : %{department} : %{manager} : %{expireText} : %{endcircuit}'),
142                $to || 'Not sent, no destination',
143                ($mailcc ? $mailcc : ''),
144            )
145        );
146        my $message;
147        if ($lamail->process(\%mail, $user)) {
148            la_log(LA_NOTICE, "Expiration mail for %s (%s) sent to %s; cc %s",
149                $user->{obj}->id,
150                $user->{days},
151                $mail{to}, ($mail{cc} || ''));
152            if ($options{to}) {
153                la_log(LA_NOTICE,"\tbut sent to %s", $mail{to});
154            }
155        } 
156    }
157
158    if (@summary) {
159        if ($options{test}) {
160            print join('', @summary);
161        } else {
162            if ($self->val('_default_', 'expire_summary_to')) {
163                my $summail = LATMOS::Accounts::Mail->new(
164                    $self,
165                    \join('', @summary),
166                );
167                my %mail = (
168                );
169                if ($summail->process({
170                    Subject => "$MailSubject: account expiration summary",
171                    To => $self->val('_default_', 'expire_summary_to'),
172                })) {
173                    la_log(
174                        LA_NOTICE,
175                        "Expiration summary mail sent to %s",
176                        $self->val('_default_', 'expire_summary_to'),
177                    );
178                }
179            }
180        }
181    }
182
183    1;
184}
185
186=head2 find_expired_users ($expire)
187
188See L<LATMOS::Accounts::Base/find_expired_users>
189
190=cut
191
192sub find_expired_users {
193    my ($self, $expire) = @_;
194    $self->_base->find_expired_users($expire);
195}
196
197=head2 expired_account_reminder ( %options)
198
199Search account expired for more than C<$options{delay}> (default is 6 month)
200send mail to manager and summary to admin to aknoledge destruction.
201
202=cut
203
204sub expired_account_reminder {
205    my ($self, %options) = @_;
206    $options{delay} ||= '6 month';
207
208    my $lamail = LATMOS::Accounts::Mail->new(
209        $self,
210        'account_expired_reminder.mail',
211    );
212
213    my @users = $self->_base->find_expired_users($options{delay});
214
215    my %managers;
216
217    foreach my $user (@users) {
218        my $uobj = $self->_base->get_object('user', $user);
219        $uobj->get_attributes('unexported') and next; # can't happend
220        my $manager = $uobj->get_attributes('manager') || 'N/A';
221        push(@{$managers{$manager}{users}}, $uobj);
222
223    }
224
225    my $MailSubject = $self->val('_default_', 'mailSubject', 'LATMOS::Accounts');
226
227    unless($options{test}) {
228        foreach (keys %managers) {
229            my $oman = $self->_base->get_object('user', $_) or next; # can't happend
230            $managers{$_}{manager} = $oman;
231            my $mail = $oman->get_attributes('mail') or next;
232
233            my %mail = (
234                Subject => "$MailSubject: expired account",
235                'X-LATMOS-Reason' => 'Account destruction',
236            );
237            $mail{to} = $options{to} || $mail;
238            if ($lamail->process(\%mail, $managers{$oman->id})) {
239                la_log(LA_NOTICE,
240                    "Expired account reminder mail for %s sent to %s (cc: %s) for %s",
241                    $oman->id,
242                    $mail{to},
243                    ($mail{cc} || ''),
244                    join(', ', map { $_->id } @{$managers{$oman->id}{users}})
245                );
246            }
247        }
248    }
249    my @summary;
250    foreach my $manager (sort keys %managers) {
251        push(@summary, "\n" . (
252            $managers{$manager}{manager}
253                ? $managers{$manager}{manager}->get_attributes('displayName')
254                : $manager) . "\n");
255        foreach (@{$managers{$manager}{users}}) {
256            push(@summary, sprintf("  %s - %s (%s)\n",
257                $_->id,
258                $_->get_attributes('displayName'),
259                $_->get_attributes('expireText'),
260            ));
261        }
262    }
263
264    if (@summary) {
265        if ($options{test}) {
266            print join('', @summary);
267        } else {
268            if ($self->val('_default_', 'expire_summary_to')) {
269                my %mail = (
270                    Subject => "$MailSubject: expired account (to disable)",
271                    'X-LATMOS-Reason' => 'Account expiration',
272                    To => $self->val('_default_', 'expire_summary_to'),
273                );
274                my $summail = LATMOS::Accounts::Mail->new(
275                    $self, \join('', @summary), {}
276                );
277                if ($summail->process(\%mail)) {
278                    la_log(LA_NOTICE, "Expiration summary mail sent to %s",
279                        $self->val('_default_', 'expire_summary_to'),
280                    );
281                }
282            }
283        }
284    }
285}
286
2871;
288
289__END__
290
291=head1 SEE ALSO
292
293L<LATMOS::Accounts::Bases>
294
295=head1 AUTHOR
296
297Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
298
299=head1 COPYRIGHT AND LICENSE
300
301Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier
302
303This library is free software; you can redistribute it and/or modify
304it under the same terms as Perl itself, either Perl version 5.10.0 or,
305at your option, any later version of Perl 5 you may have available.
306
307=cut
Note: See TracBrowser for help on using the repository browser.