1 | package LATMOS::Accounts::Maintenance; |
---|
2 | |
---|
3 | use strict; |
---|
4 | use warnings; |
---|
5 | use base qw(LATMOS::Accounts); |
---|
6 | use LATMOS::Accounts::Log; |
---|
7 | use FindBin qw($Bin); |
---|
8 | |
---|
9 | sub _base { |
---|
10 | my ($self) = @_; |
---|
11 | return $self->{_maintenance_base} if ($self->{_maintenance_base}); |
---|
12 | my $base = $self->SUPER::default_base; |
---|
13 | $base->type eq 'sql' or die "This module work only with SQL base type\n"; |
---|
14 | return $self->{_maintenance_base} = $base |
---|
15 | } |
---|
16 | |
---|
17 | sub find_next_expire_users { |
---|
18 | # Do not replace this code by $base->find_next_expire_users |
---|
19 | # it does not exactly the same thing |
---|
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) { |
---|
35 | $res->{delay} =~ s/ day.? .*//; |
---|
36 | $res->{obj} = $base->get_object('user', $res->{name}); |
---|
37 | $res->{obj}->get_attributes('locked') and next; |
---|
38 | push(@users, $res); |
---|
39 | } |
---|
40 | @users |
---|
41 | } |
---|
42 | |
---|
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 | |
---|
63 | sub warn_next_expire_users { |
---|
64 | my ($self, %options) = @_; |
---|
65 | |
---|
66 | require LATMOS::Account::Mail; |
---|
67 | my $lamail = LATMOS::Account::Mail->new( |
---|
68 | $self, |
---|
69 | 'account_expire.mail', |
---|
70 | ); |
---|
71 | |
---|
72 | my @summary; |
---|
73 | foreach my $user ($self->find_next_expire_users($options{delay})) { |
---|
74 | if ($options{users} && ! grep { $_ eq $user->{name} } @{$options{users}}) { |
---|
75 | next; |
---|
76 | } |
---|
77 | my %mail = ( |
---|
78 | From => $self->val('_default_', 'mailFrom', 'nomail@localhost'), |
---|
79 | Subject => sprintf('Account %s Expire in %s days', $user->{name}, $user->{delay}), |
---|
80 | 'X-LATMOS-Reason' => 'Account expiration', |
---|
81 | ); |
---|
82 | my ($manager, $mail) = ($user->{obj}->get_c_field('managerContact'), |
---|
83 | $user->{obj}->get_c_field('mail')); |
---|
84 | my $managermail = $manager ? $user->{obj}->base-> |
---|
85 | get_object('user', $manager)->get_c_field('mail') : undef; |
---|
86 | |
---|
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 |
---|
89 | my ($to, @cc) = grep { $_ } ($mail, $managermail, |
---|
90 | $self->val('_default_', 'allwayscc')); |
---|
91 | if ($options{to}) { |
---|
92 | $mail{to} = $options{to}; |
---|
93 | } else { |
---|
94 | $mail{to} = $to; |
---|
95 | $mail{cc} = join(', ', @cc); |
---|
96 | } |
---|
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 | }; |
---|
104 | my $mailcc = join(', ', @cc) || ''; |
---|
105 | push(@summary, sprintf("%s : %s : %s\n", |
---|
106 | $user->{obj}->queryformat('%{sn} %{givenName} : %{name} : %{department} : %{managerContact} : %{expireText}'), |
---|
107 | $to || 'Not sent, no destination', |
---|
108 | ($mailcc ? $mailcc : ''), |
---|
109 | ) |
---|
110 | ); |
---|
111 | my $message; |
---|
112 | if ($lamail->process($user, \%mail)) { |
---|
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}); |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | if (@summary) { |
---|
124 | if ($options{test}) { |
---|
125 | print join('', @summary); |
---|
126 | } else { |
---|
127 | if ($self->val('_default_', 'expire_summary_to')) { |
---|
128 | my $summail = LATMOS::Account::Mail->new( |
---|
129 | $self, |
---|
130 | \join('', @summary), |
---|
131 | ); |
---|
132 | my %mail = ( |
---|
133 | ); |
---|
134 | if ($summail->process({}, { |
---|
135 | Subject => 'LATMOS account expiration summary', |
---|
136 | To => $self->val('_default_', 'expire_summary_to'), |
---|
137 | })) { |
---|
138 | la_log( |
---|
139 | LA_NOTICE, |
---|
140 | "Expiration summary mail sent to %s", |
---|
141 | $self->val('_default_', 'expire_summary_to'), |
---|
142 | ); |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | 1; |
---|
149 | } |
---|
150 | |
---|
151 | sub find_expired_users { |
---|
152 | my ($self, $expire) = @_; |
---|
153 | $self->_base->find_expired_users($expire); |
---|
154 | } |
---|
155 | |
---|
156 | sub expired_account_reminder { |
---|
157 | my ($self, %options) = @_; |
---|
158 | $options{delay} ||= '6 month'; |
---|
159 | |
---|
160 | require LATMOS::Accounts::Mail; |
---|
161 | |
---|
162 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
163 | $self, |
---|
164 | 'account_expired_reminder.mail', |
---|
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', |
---|
184 | 'X-LATMOS-Reason' => 'Account destruction', |
---|
185 | ); |
---|
186 | $mail{to} = $options{to} || $mail; |
---|
187 | if ($lamail->process($managers{$oman->id}, \%mail)) { |
---|
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 | ); |
---|
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 | ); |
---|
222 | my $summail = LATMOS::Accounts::Mail( |
---|
223 | $self, \join('', @summary), {} |
---|
224 | ); |
---|
225 | if ($summail->process({}, \%mail)) { |
---|
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 | |
---|
235 | 1; |
---|