[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; |
---|
[410] | 7 | use FindBin qw($Bin); |
---|
| 8 | |
---|
| 9 | sub _base { |
---|
| 10 | my ($self) = @_; |
---|
[861] | 11 | return $self->{_maintenance_base} if ($self->{_maintenance_base}); |
---|
[410] | 12 | my $base = $self->SUPER::default_base; |
---|
| 13 | $base->type eq 'sql' or die "This module work only with SQL base type\n"; |
---|
[861] | 14 | return $self->{_maintenance_base} = $base |
---|
[410] | 15 | } |
---|
| 16 | |
---|
| 17 | sub find_next_expire_users { |
---|
[850] | 18 | # Do not replace this code by $base->find_next_expire_users |
---|
[861] | 19 | # it does not exactly the same thing |
---|
[410] | 20 | my ($self, $expire) = @_; |
---|
| 21 | my $base = $self->_base; |
---|
| 22 | |
---|
| 23 | my $sth= $base->db->prepare(q{ |
---|
| 24 | select name, justify_hours(expire - now()) as delay from "user" where |
---|
| 25 | expire < now() + ?::interval |
---|
| 26 | and expire > now() |
---|
| 27 | and expire is not null |
---|
| 28 | and exported = True |
---|
| 29 | order by expire |
---|
| 30 | } |
---|
| 31 | ); |
---|
| 32 | $sth->execute($expire || '1 month'); |
---|
| 33 | my @users; |
---|
| 34 | while (my $res = $sth->fetchrow_hashref) { |
---|
[787] | 35 | $res->{delay} =~ s/ day.? .*//; |
---|
[410] | 36 | $res->{obj} = $base->get_object('user', $res->{name}); |
---|
[788] | 37 | $res->{obj}->get_attributes('locked') and next; |
---|
[410] | 38 | push(@users, $res); |
---|
| 39 | } |
---|
| 40 | @users |
---|
| 41 | } |
---|
| 42 | |
---|
[417] | 43 | =head2 warn_next_expire_users(%options) |
---|
| 44 | |
---|
| 45 | Send a mail to user having account expiring soon |
---|
| 46 | |
---|
| 47 | C<%options are> |
---|
| 48 | |
---|
| 49 | =over 4 |
---|
| 50 | |
---|
| 51 | =item users => [] |
---|
| 52 | |
---|
| 53 | Warn only this users (if need) |
---|
| 54 | |
---|
| 55 | =item to |
---|
| 56 | |
---|
| 57 | Send the only to this person. |
---|
| 58 | |
---|
| 59 | =back |
---|
| 60 | |
---|
| 61 | =cut |
---|
| 62 | |
---|
[410] | 63 | sub warn_next_expire_users { |
---|
[417] | 64 | my ($self, %options) = @_; |
---|
[410] | 65 | |
---|
[990] | 66 | require LATMOS::Account::Mail; |
---|
| 67 | my $lamail = LATMOS::Account::Mail->new( |
---|
| 68 | $self, |
---|
| 69 | 'account_expire.mail', |
---|
[410] | 70 | ); |
---|
[990] | 71 | |
---|
[775] | 72 | my @summary; |
---|
[704] | 73 | foreach my $user ($self->find_next_expire_users($options{delay})) { |
---|
[417] | 74 | if ($options{users} && ! grep { $_ eq $user->{name} } @{$options{users}}) { |
---|
| 75 | next; |
---|
| 76 | } |
---|
[410] | 77 | my %mail = ( |
---|
[423] | 78 | From => $self->val('_default_', 'mailFrom', 'nomail@localhost'), |
---|
[913] | 79 | Subject => sprintf('Account %s Expire in %s days', $user->{name}, $user->{delay}), |
---|
[410] | 80 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
| 81 | ); |
---|
[776] | 82 | my ($manager, $mail) = ($user->{obj}->get_c_field('managerContact'), |
---|
[410] | 83 | $user->{obj}->get_c_field('mail')); |
---|
| 84 | my $managermail = $manager ? $user->{obj}->base-> |
---|
| 85 | get_object('user', $manager)->get_c_field('mail') : undef; |
---|
[775] | 86 | |
---|
[410] | 87 | # if user have no mail, mail only to manager, avoiding empty To |
---|
| 88 | # NB: at time, for testing purpose, mail is not really sent |
---|
[710] | 89 | my ($to, @cc) = grep { $_ } ($mail, $managermail, |
---|
| 90 | $self->val('_default_', 'allwayscc')); |
---|
[417] | 91 | if ($options{to}) { |
---|
| 92 | $mail{to} = $options{to}; |
---|
| 93 | } else { |
---|
| 94 | $mail{to} = $to; |
---|
| 95 | $mail{cc} = join(', ', @cc); |
---|
| 96 | } |
---|
[711] | 97 | $mail{to} or do { |
---|
| 98 | la_log(LA_ERR, |
---|
| 99 | "Cannot send expiration for `%s', no mail to send to", |
---|
| 100 | $user->{obj}->id, |
---|
| 101 | ); |
---|
| 102 | next; |
---|
| 103 | }; |
---|
[785] | 104 | my $mailcc = join(', ', @cc) || ''; |
---|
[786] | 105 | push(@summary, sprintf("%s : %s : %s\n", |
---|
[789] | 106 | $user->{obj}->queryformat('%{sn} %{givenName} : %{name} : %{department} : %{managerContact} : %{expireText}'), |
---|
[785] | 107 | $to || 'Not sent, no destination', |
---|
[786] | 108 | ($mailcc ? $mailcc : ''), |
---|
[785] | 109 | ) |
---|
| 110 | ); |
---|
[410] | 111 | my $message; |
---|
[1011] | 112 | if ($lamail->process(\%mail, $user)) { |
---|
[990] | 113 | la_log(LA_NOTICE, "Expiration mail for %s (%s) sent to %s; cc %s", |
---|
| 114 | $user->{obj}->id, |
---|
| 115 | $user->{delay}, |
---|
| 116 | $mail{to}, ($mail{cc} || '')); |
---|
| 117 | if ($options{to}) { |
---|
| 118 | la_log(LA_NOTICE,"\tbut sent to %s", $mail{to}); |
---|
[710] | 119 | } |
---|
[990] | 120 | } |
---|
[410] | 121 | } |
---|
| 122 | |
---|
[785] | 123 | if (@summary) { |
---|
| 124 | if ($options{test}) { |
---|
| 125 | print join('', @summary); |
---|
| 126 | } else { |
---|
| 127 | if ($self->val('_default_', 'expire_summary_to')) { |
---|
[990] | 128 | my $summail = LATMOS::Account::Mail->new( |
---|
| 129 | $self, |
---|
| 130 | \join('', @summary), |
---|
| 131 | ); |
---|
[785] | 132 | my %mail = ( |
---|
[990] | 133 | ); |
---|
[1011] | 134 | if ($summail->process({ |
---|
[785] | 135 | Subject => 'LATMOS account expiration summary', |
---|
| 136 | To => $self->val('_default_', 'expire_summary_to'), |
---|
[990] | 137 | })) { |
---|
| 138 | la_log( |
---|
| 139 | LA_NOTICE, |
---|
| 140 | "Expiration summary mail sent to %s", |
---|
[785] | 141 | $self->val('_default_', 'expire_summary_to'), |
---|
| 142 | ); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
[410] | 148 | 1; |
---|
| 149 | } |
---|
| 150 | |
---|
[850] | 151 | sub find_expired_users { |
---|
| 152 | my ($self, $expire) = @_; |
---|
| 153 | $self->_base->find_expired_users($expire); |
---|
| 154 | } |
---|
| 155 | |
---|
[861] | 156 | sub expired_account_reminder { |
---|
| 157 | my ($self, %options) = @_; |
---|
| 158 | $options{delay} ||= '6 month'; |
---|
| 159 | |
---|
[990] | 160 | require LATMOS::Accounts::Mail; |
---|
[861] | 161 | |
---|
[990] | 162 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
| 163 | $self, |
---|
| 164 | 'account_expired_reminder.mail', |
---|
[861] | 165 | ); |
---|
| 166 | |
---|
| 167 | my @users = $self->_base->find_expired_users($options{delay}); |
---|
| 168 | |
---|
| 169 | my %managers; |
---|
| 170 | foreach my $user (@users) { |
---|
| 171 | my $uobj = $self->_base->get_object('user', $user); |
---|
| 172 | $uobj->get_attributes('unexported') and next; # can't happend |
---|
| 173 | my $manager = $uobj->get_attributes('managerContact') || 'N/A'; |
---|
| 174 | push(@{$managers{$manager}{users}}, $uobj); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | foreach (keys %managers) { |
---|
| 178 | my $oman = $self->_base->get_object('user', $_) or next; # can't happend |
---|
| 179 | $managers{$_}{manager} = $oman; |
---|
| 180 | my $mail = $oman->get_attributes('mail') or next; |
---|
| 181 | |
---|
| 182 | my %mail = ( |
---|
| 183 | Subject => 'LATMOS expired account', |
---|
[990] | 184 | 'X-LATMOS-Reason' => 'Account destruction', |
---|
[861] | 185 | ); |
---|
| 186 | $mail{to} = $options{to} || $mail; |
---|
[1011] | 187 | if ($lamail->process(\%mail, $managers{$oman->id})) { |
---|
[990] | 188 | la_log(LA_NOTICE, |
---|
| 189 | "Expired account reminder mail for %s sent to %s (cc: %s) for %s", |
---|
| 190 | $oman->id, |
---|
| 191 | $mail{to}, |
---|
| 192 | ($mail{cc} || ''), |
---|
| 193 | join(', ', map { $_->id } @{$managers{$oman->id}{users}}) |
---|
| 194 | ); |
---|
[861] | 195 | } |
---|
| 196 | } |
---|
| 197 | my @summary; |
---|
| 198 | foreach my $manager (sort keys %managers) { |
---|
| 199 | push(@summary, "\n" . ( |
---|
| 200 | $managers{$manager}{manager} |
---|
| 201 | ? $managers{$manager}{manager}->get_attributes('displayName') |
---|
| 202 | : $manager) . "\n"); |
---|
| 203 | foreach (@{$managers{$manager}{users}}) { |
---|
| 204 | push(@summary, sprintf(" %s - %s (%s)\n", |
---|
| 205 | $_->id, |
---|
| 206 | $_->get_attributes('displayName'), |
---|
| 207 | $_->get_attributes('expireText'), |
---|
| 208 | )); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | if (@summary) { |
---|
| 213 | if ($options{test}) { |
---|
| 214 | print join('', @summary); |
---|
| 215 | } else { |
---|
| 216 | if ($self->val('_default_', 'expire_summary_to')) { |
---|
| 217 | my %mail = ( |
---|
| 218 | Subject => 'LATMOS expired account (to disable)', |
---|
| 219 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
| 220 | To => $self->val('_default_', 'expire_summary_to'), |
---|
| 221 | ); |
---|
[990] | 222 | my $summail = LATMOS::Accounts::Mail( |
---|
| 223 | $self, \join('', @summary), {} |
---|
| 224 | ); |
---|
[1011] | 225 | if ($summail->process(\%mail)) { |
---|
[861] | 226 | la_log(LA_NOTICE, "Expiration summary mail sent to %s", |
---|
| 227 | $self->val('_default_', 'expire_summary_to'), |
---|
| 228 | ); |
---|
| 229 | } |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | } |
---|
| 233 | } |
---|
| 234 | |
---|
[410] | 235 | 1; |
---|