[410] | 1 | package LATMOS::Accounts::Maintenance; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base qw(LATMOS::Accounts); |
---|
[711] | 6 | use LATMOS::Accounts::Log; |
---|
[1100] | 7 | use LATMOS::Accounts::Bases::Sql::DataRequest; |
---|
[410] | 8 | use FindBin qw($Bin); |
---|
| 9 | |
---|
[1014] | 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | LATMOS::Accounts::Maintenance |
---|
| 13 | |
---|
[1023] | 14 | =head1 FUNCTIONS |
---|
| 15 | |
---|
[1014] | 16 | =cut |
---|
| 17 | |
---|
[410] | 18 | sub _base { |
---|
| 19 | my ($self) = @_; |
---|
[861] | 20 | return $self->{_maintenance_base} if ($self->{_maintenance_base}); |
---|
[1085] | 21 | my $base = $self->SUPER::base; |
---|
[410] | 22 | $base->type eq 'sql' or die "This module work only with SQL base type\n"; |
---|
[861] | 23 | return $self->{_maintenance_base} = $base |
---|
[410] | 24 | } |
---|
| 25 | |
---|
[1023] | 26 | =head2 find_next_expire_users ($expire) |
---|
| 27 | |
---|
| 28 | Return The list of users going to expire in C<$expire> delay. |
---|
| 29 | |
---|
| 30 | =cut |
---|
| 31 | |
---|
[410] | 32 | sub find_next_expire_users { |
---|
[850] | 33 | # Do not replace this code by $base->find_next_expire_users |
---|
[861] | 34 | # it does not exactly the same thing |
---|
[410] | 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) { |
---|
[787] | 50 | $res->{delay} =~ s/ day.? .*//; |
---|
[410] | 51 | $res->{obj} = $base->get_object('user', $res->{name}); |
---|
[788] | 52 | $res->{obj}->get_attributes('locked') and next; |
---|
[410] | 53 | push(@users, $res); |
---|
| 54 | } |
---|
| 55 | @users |
---|
| 56 | } |
---|
| 57 | |
---|
[417] | 58 | =head2 warn_next_expire_users(%options) |
---|
| 59 | |
---|
| 60 | Send a mail to user having account expiring soon |
---|
| 61 | |
---|
| 62 | C<%options are> |
---|
| 63 | |
---|
| 64 | =over 4 |
---|
| 65 | |
---|
| 66 | =item users => [] |
---|
| 67 | |
---|
| 68 | Warn only this users (if need) |
---|
| 69 | |
---|
| 70 | =item to |
---|
| 71 | |
---|
| 72 | Send the only to this person. |
---|
| 73 | |
---|
| 74 | =back |
---|
| 75 | |
---|
| 76 | =cut |
---|
| 77 | |
---|
[410] | 78 | sub warn_next_expire_users { |
---|
[417] | 79 | my ($self, %options) = @_; |
---|
[410] | 80 | |
---|
[990] | 81 | require LATMOS::Account::Mail; |
---|
| 82 | my $lamail = LATMOS::Account::Mail->new( |
---|
| 83 | $self, |
---|
| 84 | 'account_expire.mail', |
---|
[410] | 85 | ); |
---|
[990] | 86 | |
---|
[775] | 87 | my @summary; |
---|
[704] | 88 | foreach my $user ($self->find_next_expire_users($options{delay})) { |
---|
[417] | 89 | if ($options{users} && ! grep { $_ eq $user->{name} } @{$options{users}}) { |
---|
| 90 | next; |
---|
| 91 | } |
---|
[410] | 92 | my %mail = ( |
---|
[423] | 93 | From => $self->val('_default_', 'mailFrom', 'nomail@localhost'), |
---|
[913] | 94 | Subject => sprintf('Account %s Expire in %s days', $user->{name}, $user->{delay}), |
---|
[410] | 95 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
| 96 | ); |
---|
[776] | 97 | my ($manager, $mail) = ($user->{obj}->get_c_field('managerContact'), |
---|
[410] | 98 | $user->{obj}->get_c_field('mail')); |
---|
| 99 | my $managermail = $manager ? $user->{obj}->base-> |
---|
| 100 | get_object('user', $manager)->get_c_field('mail') : undef; |
---|
[775] | 101 | |
---|
[410] | 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 |
---|
[710] | 104 | my ($to, @cc) = grep { $_ } ($mail, $managermail, |
---|
| 105 | $self->val('_default_', 'allwayscc')); |
---|
[417] | 106 | if ($options{to}) { |
---|
| 107 | $mail{to} = $options{to}; |
---|
| 108 | } else { |
---|
| 109 | $mail{to} = $to; |
---|
| 110 | $mail{cc} = join(', ', @cc); |
---|
| 111 | } |
---|
[711] | 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 | }; |
---|
[785] | 119 | my $mailcc = join(', ', @cc) || ''; |
---|
[786] | 120 | push(@summary, sprintf("%s : %s : %s\n", |
---|
[789] | 121 | $user->{obj}->queryformat('%{sn} %{givenName} : %{name} : %{department} : %{managerContact} : %{expireText}'), |
---|
[785] | 122 | $to || 'Not sent, no destination', |
---|
[786] | 123 | ($mailcc ? $mailcc : ''), |
---|
[785] | 124 | ) |
---|
| 125 | ); |
---|
[410] | 126 | my $message; |
---|
[1011] | 127 | if ($lamail->process(\%mail, $user)) { |
---|
[990] | 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}); |
---|
[710] | 134 | } |
---|
[990] | 135 | } |
---|
[410] | 136 | } |
---|
| 137 | |
---|
[785] | 138 | if (@summary) { |
---|
| 139 | if ($options{test}) { |
---|
| 140 | print join('', @summary); |
---|
| 141 | } else { |
---|
| 142 | if ($self->val('_default_', 'expire_summary_to')) { |
---|
[990] | 143 | my $summail = LATMOS::Account::Mail->new( |
---|
| 144 | $self, |
---|
| 145 | \join('', @summary), |
---|
| 146 | ); |
---|
[785] | 147 | my %mail = ( |
---|
[990] | 148 | ); |
---|
[1011] | 149 | if ($summail->process({ |
---|
[785] | 150 | Subject => 'LATMOS account expiration summary', |
---|
| 151 | To => $self->val('_default_', 'expire_summary_to'), |
---|
[990] | 152 | })) { |
---|
| 153 | la_log( |
---|
| 154 | LA_NOTICE, |
---|
| 155 | "Expiration summary mail sent to %s", |
---|
[785] | 156 | $self->val('_default_', 'expire_summary_to'), |
---|
| 157 | ); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | |
---|
[410] | 163 | 1; |
---|
| 164 | } |
---|
| 165 | |
---|
[1023] | 166 | =head2 find_expired_users ($expire) |
---|
| 167 | |
---|
| 168 | See L<LATMOS::Accounts::Base/find_expired_users> |
---|
| 169 | |
---|
| 170 | =cut |
---|
| 171 | |
---|
[850] | 172 | sub find_expired_users { |
---|
| 173 | my ($self, $expire) = @_; |
---|
| 174 | $self->_base->find_expired_users($expire); |
---|
| 175 | } |
---|
| 176 | |
---|
[1023] | 177 | =head2 expired_account_reminder ( %options) |
---|
| 178 | |
---|
| 179 | Search account expired for more than C<$options{delay}> (default is 6 month) |
---|
| 180 | send mail to manager and summary to admin to aknoledge destruction. |
---|
| 181 | |
---|
| 182 | =cut |
---|
| 183 | |
---|
[861] | 184 | sub expired_account_reminder { |
---|
| 185 | my ($self, %options) = @_; |
---|
| 186 | $options{delay} ||= '6 month'; |
---|
| 187 | |
---|
[990] | 188 | require LATMOS::Accounts::Mail; |
---|
[861] | 189 | |
---|
[990] | 190 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
| 191 | $self, |
---|
| 192 | 'account_expired_reminder.mail', |
---|
[861] | 193 | ); |
---|
| 194 | |
---|
| 195 | my @users = $self->_base->find_expired_users($options{delay}); |
---|
| 196 | |
---|
| 197 | my %managers; |
---|
[1100] | 198 | if (my $accreq = $self->_base->get_object('accreq', 'user-removal')) { |
---|
[1180] | 199 | |
---|
| 200 | $self->base->log(LA_DEBUG, |
---|
| 201 | "Found accreq 'user-removal', using it to automated deletion", |
---|
| 202 | ); |
---|
[1100] | 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 | } |
---|
[861] | 238 | } |
---|
| 239 | |
---|
[1100] | 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', |
---|
[990] | 249 | ); |
---|
[1100] | 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 | } |
---|
[861] | 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 | ); |
---|
[1180] | 287 | my $summail = LATMOS::Accounts::Mail->new( |
---|
[990] | 288 | $self, \join('', @summary), {} |
---|
| 289 | ); |
---|
[1011] | 290 | if ($summail->process(\%mail)) { |
---|
[861] | 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 | |
---|
[410] | 300 | 1; |
---|
[1023] | 301 | |
---|
| 302 | __END__ |
---|
| 303 | |
---|
| 304 | =head1 SEE ALSO |
---|
| 305 | |
---|
| 306 | L<LATMOS::Accounts::Bases> |
---|
| 307 | |
---|
| 308 | =head1 AUTHOR |
---|
| 309 | |
---|
| 310 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 311 | |
---|
| 312 | =head1 COPYRIGHT AND LICENSE |
---|
| 313 | |
---|
| 314 | Copyright (C) 2009, 2010, 2011, 2012 by Thauvin Olivier |
---|
| 315 | |
---|
| 316 | This library is free software; you can redistribute it and/or modify |
---|
| 317 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 318 | at your option, any later version of Perl 5 you may have available. |
---|
| 319 | |
---|
| 320 | =cut |
---|