[1872] | 1 | package LATMOS::Accounts::Task::Delexpiredusers; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base qw(LATMOS::Accounts::Task); |
---|
| 6 | use LATMOS::Accounts; |
---|
| 7 | use LATMOS::Accounts::Log; |
---|
| 8 | use LATMOS::Accounts::Utils; |
---|
| 9 | use DateTime; |
---|
| 10 | |
---|
| 11 | =head1 NAME |
---|
| 12 | |
---|
| 13 | LATMOS::Accounts::Task::Expiredusers - Warn and unexport expired user |
---|
| 14 | |
---|
| 15 | =head1 DESCRIPTION |
---|
| 16 | |
---|
| 17 | This module unexport user when they are expired since a given delay. |
---|
| 18 | |
---|
| 19 | Before deleting a warning can be sent to the manager. |
---|
| 20 | |
---|
| 21 | =head1 PARAMETER |
---|
| 22 | |
---|
| 23 | =head2 warndelay |
---|
| 24 | |
---|
| 25 | The number of days before warning manager the user will be unexported. |
---|
| 26 | |
---|
| 27 | If unset the warning is disable. |
---|
| 28 | |
---|
| 29 | =head2 unexportdelay |
---|
| 30 | |
---|
| 31 | The number of days after C<warndelay> the user is effectivelly unexported. |
---|
| 32 | |
---|
| 33 | If unset no user are unexported. |
---|
| 34 | |
---|
[1878] | 35 | =head2 modifdelay |
---|
| 36 | |
---|
| 37 | The time to wait in minutes after user modification before unexporting it. |
---|
| 38 | |
---|
| 39 | This time avoid the module deleting a user in the same time an administrator |
---|
| 40 | is trying to reactivate it. |
---|
| 41 | |
---|
| 42 | Default is 60 minutes. |
---|
| 43 | |
---|
[1881] | 44 | =head2 test |
---|
| 45 | |
---|
| 46 | If set only log are emitted but changes are not done and mail not really sent |
---|
| 47 | |
---|
[1872] | 48 | =head2 cc |
---|
| 49 | |
---|
| 50 | A copy of warning are sent to this email address |
---|
| 51 | |
---|
[1879] | 52 | =head2 summary |
---|
| 53 | |
---|
| 54 | If set, send a summmary to this address |
---|
| 55 | |
---|
[1872] | 56 | =head2 to |
---|
| 57 | |
---|
| 58 | Instead sending mail to manager mail are sent to this email address. |
---|
| 59 | |
---|
| 60 | =head1 warndelay/unexportdelay example |
---|
| 61 | |
---|
| 62 | Unexporting user immediatly /o any warning: |
---|
| 63 | |
---|
| 64 | # warndelay = 0 |
---|
| 65 | unexportdelay = 0 |
---|
| 66 | |
---|
| 67 | Warning manager with immediate unexport: |
---|
| 68 | |
---|
| 69 | warndelay = 0 |
---|
| 70 | unexportdelay = 0 |
---|
| 71 | |
---|
| 72 | Warning manager immediatelly but unexporting user 15 days later |
---|
| 73 | |
---|
| 74 | warndelay = 0 |
---|
| 75 | unexportdelay = 15 |
---|
| 76 | |
---|
| 77 | Wait 6 month after expiration, unexport user 15 days after warning: |
---|
| 78 | |
---|
| 79 | warndelay = 180 |
---|
| 80 | unexportdelay = 15 |
---|
| 81 | |
---|
| 82 | =cut |
---|
| 83 | |
---|
[1880] | 84 | # Run every 2 hours |
---|
| 85 | sub runDelay { 2 * 60 * 60 } |
---|
[1872] | 86 | |
---|
[1880] | 87 | # Wait 30 minutes |
---|
| 88 | sub waitDelay { 30 * 60 } |
---|
| 89 | |
---|
[1873] | 90 | sub _DATEFLAG { 'expireText' } |
---|
[1872] | 91 | |
---|
| 92 | sub init { |
---|
| 93 | my ($self) = @_; |
---|
| 94 | my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1); |
---|
| 95 | my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base; |
---|
| 96 | $labase && $labase->load or die "Cannot load base"; |
---|
| 97 | |
---|
| 98 | $self->{_la} = $LA; |
---|
| 99 | $self->{_base} = $labase; |
---|
| 100 | |
---|
| 101 | 1; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | sub run { |
---|
| 105 | my ($self) = @_; |
---|
| 106 | |
---|
| 107 | $self->_unexport; |
---|
| 108 | |
---|
| 109 | $self->_send_warning; |
---|
| 110 | |
---|
| 111 | 1; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | sub _send_warning { |
---|
| 115 | my ($self) = @_; |
---|
| 116 | |
---|
[1881] | 117 | my $test = $self->{syncm}->ini->val($self->{name}, 'test'); |
---|
[1872] | 118 | my $warnDelay = $self->{syncm}->ini->val($self->{name}, 'warndelay'); |
---|
[1881] | 119 | |
---|
| 120 | my $modifDelay = $self->{syncm}->ini->val($self->{name}, 'modifdelay', 60); |
---|
| 121 | my $waitAfterModif = DateTime->now->subtract( minutes => $modifDelay )->iso8601; |
---|
[1872] | 122 | |
---|
| 123 | defined($warnDelay) or do { |
---|
| 124 | $self->{_base}->log(LA_DEBUG, "Don't warning manager because no warndelay given"); |
---|
| 125 | return; |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | my $expiredDate = DateTime->now()->subtract(days => $warnDelay); |
---|
| 129 | |
---|
| 130 | my %expired; |
---|
| 131 | |
---|
| 132 | foreach my $user ($self->{_base}->search_objects('user', |
---|
[1881] | 133 | 'exported=1', |
---|
[1885] | 134 | 'expire<' . $expiredDate->ymd('-'), |
---|
[1881] | 135 | 'date<' . $waitAfterModif, |
---|
| 136 | 'oalias=NULL' |
---|
| 137 | )) { |
---|
[1872] | 138 | my $ouser = $self->{_base}->get_object('user', $user) or next; |
---|
| 139 | |
---|
| 140 | if ($ouser->get_attributes('nextEmployment')) { |
---|
| 141 | # This user has expire account but a futur employement is coming |
---|
| 142 | next; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if (my $val = $self->{_base}->get_global_value("expiredWarning.user.$user")) { |
---|
[1873] | 146 | if ($val eq $ouser->get_attributes(_DATEFLAG)) { |
---|
[1872] | 147 | # Warning has been already sent |
---|
| 148 | next; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | my $manager = $ouser->get_attributes('manager') || 'N/A'; |
---|
| 153 | |
---|
[1881] | 154 | $self->{_base}->log( |
---|
| 155 | $test ? LA_DEBUG : LA_NOTICE, |
---|
| 156 | "%sUser %s is expired (%s), sending warning to %s", |
---|
| 157 | $test ? 'Test: ' : '', |
---|
| 158 | $user, |
---|
| 159 | $ouser->get_attributes('expireText'), |
---|
| 160 | $manager, |
---|
| 161 | ); |
---|
| 162 | |
---|
[1872] | 163 | $expired{$manager} ||= { users => [] }; |
---|
| 164 | push (@{$expired{$manager}{users}}, $ouser); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
| 168 | $self->{_la}, |
---|
| 169 | 'account_expired_reminder.mail', |
---|
| 170 | ); |
---|
| 171 | |
---|
| 172 | foreach my $manager (sort keys %expired) { |
---|
| 173 | |
---|
| 174 | my $omanager = $self->{_base}->get_object('user', $manager) or next; |
---|
| 175 | my $mail = $omanager->get_attributes('mail') or next; |
---|
| 176 | |
---|
| 177 | $expired{$manager}{manager} = $omanager; |
---|
| 178 | |
---|
| 179 | my %mail = ( |
---|
| 180 | Subject => 'LATMOS expired account', |
---|
| 181 | 'X-LATMOS-Reason' => 'Account destruction', |
---|
| 182 | ); |
---|
| 183 | |
---|
| 184 | # to is for debuging |
---|
| 185 | $mail{to} = $self->{syncm}->ini->val($self->{name}, 'to', $mail); |
---|
| 186 | $mail{cc} = $self->{syncm}->ini->val($self->{name}, 'cc'); |
---|
| 187 | |
---|
| 188 | |
---|
[1881] | 189 | if ($test) { |
---|
| 190 | $self->{_base}->log(LA_NOTICE, "Test: sending mail to %s for expired account %s", |
---|
[1872] | 191 | $omanager->id, |
---|
| 192 | join(', ', sort map { $_->id } @{ $expired{$manager}{users}}), |
---|
| 193 | ); |
---|
[1881] | 194 | } else { |
---|
| 195 | if ($lamail->process(\%mail, $expired{$manager})) { |
---|
| 196 | $self->{_base}->log(LA_NOTICE, |
---|
| 197 | "Expired account reminder mail sent to %s (%s) (cc: %s) for %s", |
---|
| 198 | $mail{to}, |
---|
| 199 | $omanager->id, |
---|
| 200 | ($mail{cc} || ''), |
---|
| 201 | join(', ', sort map { $_->id } @{ $expired{$manager}{users}}), |
---|
| 202 | ); |
---|
[1872] | 203 | |
---|
[1881] | 204 | foreach my $ouser (@{ $expired{$manager}{users}}) { |
---|
| 205 | $self->{_base}->set_global_value('expiredWarning.user.' . $ouser->id, $ouser->get_attributes(_DATEFLAG)); |
---|
| 206 | } |
---|
| 207 | $self->{_base}->commit; |
---|
[1872] | 208 | } |
---|
| 209 | } |
---|
| 210 | } |
---|
[1879] | 211 | |
---|
| 212 | if (my $sumto = $self->{syncm}->ini->val($self->{name}, 'summary')) { |
---|
| 213 | my @summary; |
---|
| 214 | foreach my $manager (sort keys %expired) { |
---|
| 215 | foreach (@{$expired{$manager}{users}}) { |
---|
| 216 | push(@summary, sprintf("%s - %s (%s, %s)\n", |
---|
| 217 | $_->id, |
---|
| 218 | ($expired{$manager}{manager} |
---|
| 219 | ? $expired{$manager}{manager}->get_attributes('displayName') |
---|
| 220 | : $manager), |
---|
| 221 | $_->get_attributes('displayName'), |
---|
| 222 | $_->get_attributes('expireText'), |
---|
| 223 | ) |
---|
| 224 | ); |
---|
| 225 | } |
---|
| 226 | } |
---|
| 227 | |
---|
[1881] | 228 | if (@summary && !$test) { |
---|
[1879] | 229 | my %mail = ( |
---|
| 230 | Subject => 'LATMOS expired account (to disable)', |
---|
| 231 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
| 232 | To => $sumto, |
---|
| 233 | ); |
---|
| 234 | my $summail = LATMOS::Accounts::Mail->new( |
---|
[1886] | 235 | $self->{_la}, \join('', @summary), {} |
---|
[1879] | 236 | ); |
---|
| 237 | if ($summail->process(\%mail)) { |
---|
| 238 | la_log(LA_NOTICE, "Expiration summary mail sent to %s", |
---|
| 239 | $self->val('_default_', 'expire_summary_to'), |
---|
| 240 | ); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
[1872] | 244 | } |
---|
| 245 | |
---|
| 246 | sub _unexport { |
---|
| 247 | my ($self) = @_; |
---|
| 248 | |
---|
[1881] | 249 | my $test = $self->{syncm}->ini->val($self->{name}, 'test'); |
---|
| 250 | |
---|
[1872] | 251 | my $warnDelay = $self->{syncm}->ini->val($self->{name}, 'warndelay', 0); |
---|
| 252 | my $unexportDelay = $self->{syncm}->ini->val($self->{name}, 'unexportdelay'); |
---|
| 253 | defined($unexportDelay) or do { |
---|
| 254 | $self->{_base}->log(LA_DEBUG, "Don't unexporting user because no unexportdelay given"); |
---|
| 255 | return; |
---|
| 256 | }; |
---|
| 257 | |
---|
| 258 | my $expiredDate = DateTime->now()->subtract(days => $warnDelay + $unexportDelay); |
---|
| 259 | |
---|
[1878] | 260 | my $modifDelay = $self->{syncm}->ini->val($self->{name}, 'modifdelay', 60); |
---|
| 261 | my $waitAfterModif = DateTime->now->subtract( minutes => $modifDelay )->iso8601; |
---|
[1877] | 262 | |
---|
[1872] | 263 | foreach my $user ($self->{_base}->search_objects('user', |
---|
[1877] | 264 | 'exported=1', |
---|
[1885] | 265 | 'expire<' . $expiredDate->ymd('-'), |
---|
[1881] | 266 | 'date<' . $waitAfterModif, |
---|
[1877] | 267 | 'oalias=NULL' |
---|
| 268 | )) { |
---|
[1872] | 269 | my $ouser = $self->{_base}->get_object('user', $user) or next; |
---|
| 270 | if ($ouser->get_attributes('nextEmployment')) { |
---|
| 271 | # This user has expire account but a futur employement is coming |
---|
| 272 | next; |
---|
| 273 | } |
---|
| 274 | |
---|
[1885] | 275 | $self->{_base}->log(LA_NOTICE, "%sUser $user unexported because is expired (%s)", |
---|
| 276 | $test ? 'Test: ' : '', $ouser->get_attributes('expireText')); |
---|
[1881] | 277 | |
---|
| 278 | if (!$test) { |
---|
| 279 | $ouser->_set_c_fields( |
---|
| 280 | unexported => 1 |
---|
| 281 | ); |
---|
| 282 | $self->{_base}->del_global_value("expiredWarning.user.$user"); |
---|
| 283 | $self->{_base}->commit; |
---|
| 284 | } |
---|
[1872] | 285 | } |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | 1; |
---|
| 289 | |
---|
| 290 | __END__ |
---|
| 291 | |
---|
| 292 | =head1 SEE ALSO |
---|
| 293 | |
---|
| 294 | L<LATMOS::Accounts>, L<LATMOS::Accounts::Task> |
---|
| 295 | |
---|
| 296 | =head1 AUTHOR |
---|
| 297 | |
---|
| 298 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 299 | |
---|
| 300 | =head1 COPYRIGHT AND LICENSE |
---|
| 301 | |
---|
| 302 | Copyright (C) 2012 CNRS SA/CETP/LATMOS |
---|
| 303 | |
---|
| 304 | This library is free software; you can redistribute it and/or modify |
---|
| 305 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 306 | at your option, any later version of Perl 5 you may have available. |
---|
| 307 | |
---|
| 308 | =cut |
---|
| 309 | |
---|