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

Last change on this file since 1180 was 1180, checked in by nanardon, 12 years ago

merge Buildnet into regular task module

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