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