1 | package LATMOS::Accounts::Bases::Sql::User; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | use overload '""' => 'stringify'; |
---|
7 | |
---|
8 | use LATMOS::Accounts::Utils; |
---|
9 | use LATMOS::Accounts::Log; |
---|
10 | use POSIX qw(strftime); |
---|
11 | use Date::Parse qw(str2time); |
---|
12 | use DateTime; |
---|
13 | use DateTime::TimeZone; |
---|
14 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
15 | use LATMOS::Accounts::I18N; |
---|
16 | use Date::Calc; |
---|
17 | use LATMOS::Accounts::Mail; |
---|
18 | |
---|
19 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
20 | |
---|
21 | =head1 NAME |
---|
22 | |
---|
23 | LATMOS::Ad - Perl extension for blah blah blah |
---|
24 | |
---|
25 | =head1 DESCRIPTION |
---|
26 | |
---|
27 | Account base access over standard unix file format. |
---|
28 | |
---|
29 | =head1 FUNCTIONS |
---|
30 | |
---|
31 | =cut |
---|
32 | |
---|
33 | =head2 new(%config) |
---|
34 | |
---|
35 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
36 | |
---|
37 | domain / server: either the Ad domain or directly the server |
---|
38 | |
---|
39 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
40 | |
---|
41 | =cut |
---|
42 | |
---|
43 | sub _object_table { 'user' } |
---|
44 | |
---|
45 | sub _key_field { 'name' } |
---|
46 | |
---|
47 | sub _has_extended_attributes { 1 } |
---|
48 | |
---|
49 | sub stringify { |
---|
50 | my ($self) = @_; |
---|
51 | |
---|
52 | return join(' ', grep { $_ } |
---|
53 | ( |
---|
54 | $self->get_field('givenName'), |
---|
55 | $self->get_field('sn') |
---|
56 | ) |
---|
57 | ) |
---|
58 | || $self->get_field('description') |
---|
59 | || $self->id; |
---|
60 | } |
---|
61 | |
---|
62 | sub GetOtypeDef { |
---|
63 | my ($class) = @_; |
---|
64 | |
---|
65 | { |
---|
66 | address => 'user', |
---|
67 | employment => 'user', |
---|
68 | }, |
---|
69 | } |
---|
70 | |
---|
71 | sub _get_attr_schema { |
---|
72 | my ($class, $base) = @_; |
---|
73 | |
---|
74 | my $subsetaddress = sub { |
---|
75 | my ($self, $data) = @_; |
---|
76 | my $fmainaddress = $self->object->_get_c_field('mainaddress'); |
---|
77 | |
---|
78 | # set address attribute => create address object on the fly |
---|
79 | # except if attr is empty ! |
---|
80 | if (!$fmainaddress && $data) { |
---|
81 | $fmainaddress = $self->object->id . '-' . join('', map { ('a'..'z')[rand(26)] } |
---|
82 | (0..4)); |
---|
83 | $self->base->_create_c_object( |
---|
84 | 'address', $fmainaddress, |
---|
85 | user => $self->object->id, |
---|
86 | isMainAddress => 1, ) or do { |
---|
87 | $self->base->log(LA_ERR, |
---|
88 | "Cannot create main address for user %s", $self->object->id); |
---|
89 | return; |
---|
90 | }; |
---|
91 | } |
---|
92 | if ($fmainaddress && |
---|
93 | (my $address = $self->base->get_object('address', $fmainaddress))) { |
---|
94 | if ($address->attribute($self->name) && |
---|
95 | !$address->attribute($self->name)->ro) { |
---|
96 | return $address->set_c_fields($self->name => $data) ||0; |
---|
97 | } |
---|
98 | } |
---|
99 | }; |
---|
100 | |
---|
101 | my $attrs = { |
---|
102 | exported => { |
---|
103 | post => sub { |
---|
104 | my ($self, $value) = @_; |
---|
105 | my $attr = $self->name; |
---|
106 | |
---|
107 | if (my $obj = $self->base-> |
---|
108 | get_object('revaliases', $self->object->id)) { |
---|
109 | my $ares = $obj->set_c_fields( |
---|
110 | ($attr eq 'exported' ? 'exported' : 'unexported') => $value |
---|
111 | ); |
---|
112 | if (!defined($ares)) { |
---|
113 | $self->base->log(LA_ERR, |
---|
114 | 'Cannot set revaliases exported attribute for user %s', |
---|
115 | $self->object->id); |
---|
116 | } |
---|
117 | } |
---|
118 | my $must_expire = $self->name eq 'exported' |
---|
119 | ? ($value ? 0 : 1 ) |
---|
120 | : ($value ? 1 : 0 ); |
---|
121 | |
---|
122 | foreach my $al (grep { $_ } $self->object->get_attributes('aliases'), $self->object->id) { |
---|
123 | my $obj = $self->base->get_object('aliases', $al) or next; |
---|
124 | $obj->_set_c_fields( |
---|
125 | exported => $value, |
---|
126 | ); |
---|
127 | } |
---|
128 | foreach my $al ($self->object->get_attributes('otheraddress')) { |
---|
129 | my $obj = $self->base->get_object('address', $al) or next; |
---|
130 | $obj->_set_c_fields( |
---|
131 | exported => $value, |
---|
132 | ); |
---|
133 | } |
---|
134 | }, |
---|
135 | }, |
---|
136 | uidNumber => { |
---|
137 | inline => 1, |
---|
138 | iname => 'uidnumber', |
---|
139 | uniq => 1, |
---|
140 | mandatory => 1, |
---|
141 | formopts => { length => 7 }, |
---|
142 | label => l('UID'), |
---|
143 | }, |
---|
144 | uidnumber => { inline => 1, hide => 1, monitored => 1 }, |
---|
145 | gidNumber => { |
---|
146 | inline => 1, |
---|
147 | iname => 'gidnumber', |
---|
148 | mandatory => 1, |
---|
149 | can_values => sub { |
---|
150 | map { $_->id, $_->get_attributes('gidNumber') } |
---|
151 | map { $base->get_object('group', $_) } |
---|
152 | $base->list_objects('group') |
---|
153 | }, |
---|
154 | can_values => sub { |
---|
155 | my $sth = $base->db->prepare_cached( |
---|
156 | q{ |
---|
157 | select name, gidnumber from "group" where exported = true |
---|
158 | } |
---|
159 | ); |
---|
160 | $sth->execute(); |
---|
161 | my @res; |
---|
162 | while (my $res = $sth->fetchrow_hashref) { |
---|
163 | push(@res, $res->{name}); |
---|
164 | push(@res, $res->{gidnumber}); |
---|
165 | } |
---|
166 | return @res; |
---|
167 | }, |
---|
168 | display => sub { |
---|
169 | my ($self, $val) = @_; |
---|
170 | if ($val =~ /^\d+$/) { |
---|
171 | my ($gr) = $self->base->search_objects('group', "gidNumber=$val") |
---|
172 | or return; |
---|
173 | return $gr; |
---|
174 | } else { |
---|
175 | my ($gr) = $self->base->search_objects('group', "name=$val") |
---|
176 | or return; |
---|
177 | return $gr; |
---|
178 | } |
---|
179 | }, |
---|
180 | input => sub { |
---|
181 | my ($val) = @_; |
---|
182 | $val =~ /^\d+$/ and return $val; |
---|
183 | my ($gr) = $base->search_objects('group', "name=$val") or return; |
---|
184 | return $base->get_object('group', $gr)->get_attributes('gidNumber'); |
---|
185 | }, |
---|
186 | #reference => 'group', |
---|
187 | label => l('GID'), |
---|
188 | }, |
---|
189 | loginShell => { |
---|
190 | mandatory => 1, |
---|
191 | label => l('Shell'), |
---|
192 | }, |
---|
193 | gidnumber => { inline => 1, hide => 1, |
---|
194 | can_values => sub { |
---|
195 | map { $_->id, $_->get_attributes('gidNumber') } |
---|
196 | map { $base->get_object('group', $_) } |
---|
197 | $base->list_objects('group') |
---|
198 | }, |
---|
199 | display => sub { |
---|
200 | my ($self, $val) = @_; |
---|
201 | my ($gr) = $self->base->search_objects('group', "gidNumber=$val") |
---|
202 | or return; |
---|
203 | return $gr; |
---|
204 | }, |
---|
205 | input => sub { |
---|
206 | my ($val) = @_; |
---|
207 | $val =~ /^\d+$/ and return $val; |
---|
208 | my ($gr) = $base->search_objects('group', "name=$val") or return; |
---|
209 | return $base->get_object('group', $gr)->get_attributes('gidNumber'); |
---|
210 | }, |
---|
211 | mandatory => 1, |
---|
212 | reference => 'group', |
---|
213 | monitored => 1, |
---|
214 | }, |
---|
215 | locked => { |
---|
216 | formtype => 'CHECKBOX', |
---|
217 | formopts => { rawvalue => 1, }, |
---|
218 | monitored => 1, |
---|
219 | label => l('Locked'), |
---|
220 | }, |
---|
221 | expire => { |
---|
222 | inline => 1, |
---|
223 | formtype => 'DATETIME', |
---|
224 | monitored => 1, |
---|
225 | label => l('Expire on'), |
---|
226 | }, |
---|
227 | endcircuit => { |
---|
228 | inline => 1, |
---|
229 | formtype => 'DATE', |
---|
230 | monitored => 1, |
---|
231 | label => l('End of entrance'), |
---|
232 | }, |
---|
233 | _endEmployment => { |
---|
234 | formtype => 'DATETIME', |
---|
235 | managed => 1, |
---|
236 | ro => 1, |
---|
237 | hide => 1, |
---|
238 | get => sub { |
---|
239 | my ($attr) = @_; |
---|
240 | my $self = $attr->object; |
---|
241 | $self->_computeEndEmployment($self->base->config('employment_delay') || 0); |
---|
242 | }, |
---|
243 | label => l('End of employment'), |
---|
244 | }, |
---|
245 | endEmployment => { |
---|
246 | formtype => 'DATETIME', |
---|
247 | ro => 1, |
---|
248 | label => l('End of employment'), |
---|
249 | }, |
---|
250 | _departure => { |
---|
251 | formtype => 'DATETIME', |
---|
252 | managed => 1, |
---|
253 | ro => 1, |
---|
254 | hide => 1, |
---|
255 | get => sub { |
---|
256 | my ($attr) = @_; |
---|
257 | my $self = $attr->object; |
---|
258 | $self->_computeEndEmployment($self->base->config('employment_delay') || 0, 1, 1); |
---|
259 | }, |
---|
260 | label => l('Start of employment'), |
---|
261 | }, |
---|
262 | departure => { |
---|
263 | formtype => 'DATETIME', |
---|
264 | ro => 1, |
---|
265 | label => l('Leaving'), |
---|
266 | }, |
---|
267 | _endStrictEmployment => { |
---|
268 | formtype => 'DATETIME', |
---|
269 | managed => 1, |
---|
270 | ro => 1, |
---|
271 | hide => 1, |
---|
272 | get => sub { |
---|
273 | my ($attr) = @_; |
---|
274 | my $self = $attr->object; |
---|
275 | $self->_computeEndEmployment(); |
---|
276 | }, |
---|
277 | }, |
---|
278 | endStrictEmployment => { |
---|
279 | formtype => 'DATETIME', |
---|
280 | ro => 1, |
---|
281 | }, |
---|
282 | _endCurrentEmployment => { |
---|
283 | formtype => 'DATETIME', |
---|
284 | managed => 1, |
---|
285 | ro => 1, |
---|
286 | hide => 1, |
---|
287 | get => sub { |
---|
288 | my ($attr) = @_; |
---|
289 | my $self = $attr->object; |
---|
290 | |
---|
291 | my $list_empl = $self->base->db->prepare_cached(q{ |
---|
292 | SELECT * FROM employment WHERE "user" = ? and |
---|
293 | firstday <= now() and |
---|
294 | (lastday is null or lastday >= now() - '1 days'::interval) |
---|
295 | order by firstday asc |
---|
296 | }); |
---|
297 | $list_empl->execute($self->id); |
---|
298 | my $end; |
---|
299 | while (my $res = $list_empl->fetchrow_hashref) { |
---|
300 | if (!$res->{lastday}) { |
---|
301 | # Ultimate employment. |
---|
302 | $list_empl->finish; |
---|
303 | return undef; |
---|
304 | } else { |
---|
305 | $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); |
---|
306 | $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
307 | $end->add(hours => 23, minutes => 59, seconds => 59); |
---|
308 | } |
---|
309 | last; |
---|
310 | } |
---|
311 | $list_empl->finish; |
---|
312 | |
---|
313 | return $end ? $end->iso8601 : undef |
---|
314 | }, |
---|
315 | }, |
---|
316 | endCurrentEmployment => { |
---|
317 | formtype => 'DATETIME', |
---|
318 | ro => 1, |
---|
319 | }, |
---|
320 | _endLastEmployment => { |
---|
321 | formtype => 'DATETIME', |
---|
322 | managed => 1, |
---|
323 | ro => 1, |
---|
324 | hide => 1, |
---|
325 | get => sub { |
---|
326 | my ($attr) = @_; |
---|
327 | my $self = $attr->object; |
---|
328 | |
---|
329 | my $list_empl = $self->base->db->prepare_cached(q{ |
---|
330 | SELECT * FROM employment WHERE "user" = ? |
---|
331 | order by lastday desc nulls first |
---|
332 | }); |
---|
333 | $list_empl->execute($self->id); |
---|
334 | my $res = $list_empl->fetchrow_hashref; |
---|
335 | $list_empl->finish; |
---|
336 | my $end; |
---|
337 | if ($res && $res->{lastday}) { |
---|
338 | $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); |
---|
339 | $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
340 | $end->add(hours => 23, minutes => 59, seconds => 59); |
---|
341 | } |
---|
342 | return $end ? $end->iso8601 : undef |
---|
343 | }, |
---|
344 | label => l('End of any employment'), |
---|
345 | }, |
---|
346 | endLastEmployment => { |
---|
347 | formtype => 'DATETIME', |
---|
348 | ro => 1, |
---|
349 | label => l('End of any employment'), |
---|
350 | }, |
---|
351 | _startEmployment => { |
---|
352 | formtype => 'DATETIME', |
---|
353 | managed => 1, |
---|
354 | ro => 1, |
---|
355 | hide => 1, |
---|
356 | get => sub { |
---|
357 | my ($attr) = @_; |
---|
358 | my $self = $attr->object; |
---|
359 | $self->_computeStartEmployment($self->base->config('employment_delay') || 0); |
---|
360 | }, |
---|
361 | label => l('Start of employment'), |
---|
362 | }, |
---|
363 | startEmployment => { |
---|
364 | formtype => 'DATETIME', |
---|
365 | ro => 1, |
---|
366 | label => l('Start of employment'), |
---|
367 | }, |
---|
368 | _arrival => { |
---|
369 | formtype => 'DATETIME', |
---|
370 | managed => 1, |
---|
371 | ro => 1, |
---|
372 | hide => 1, |
---|
373 | get => sub { |
---|
374 | my ($attr) = @_; |
---|
375 | my $self = $attr->object; |
---|
376 | $self->_computeStartEmployment($self->base->config('employment_delay') || 0, 1, 1); |
---|
377 | }, |
---|
378 | label => l('Start of employment'), |
---|
379 | }, |
---|
380 | arrival => { |
---|
381 | formtype => 'DATETIME', |
---|
382 | ro => 1, |
---|
383 | label => l('Arrival'), |
---|
384 | }, |
---|
385 | _startStrictEmployment => { |
---|
386 | formtype => 'DATETIME', |
---|
387 | managed => 1, |
---|
388 | ro => 1, |
---|
389 | hide => 1, |
---|
390 | get => sub { |
---|
391 | my ($attr) = @_; |
---|
392 | my $self = $attr->object; |
---|
393 | $self->_computeStartEmployment(); |
---|
394 | }, |
---|
395 | }, |
---|
396 | startStrictEmployment => { |
---|
397 | formtype => 'DATETIME', |
---|
398 | ro => 1, |
---|
399 | }, |
---|
400 | _startCurrentEmployment => { |
---|
401 | formtype => 'DATETIME', |
---|
402 | managed => 1, |
---|
403 | ro => 1, |
---|
404 | hide => 1, |
---|
405 | get => sub { |
---|
406 | my ($attr) = @_; |
---|
407 | my $self = $attr->object; |
---|
408 | |
---|
409 | my $list_empl = $self->base->db->prepare_cached(q{ |
---|
410 | SELECT * FROM employment WHERE "user" = ? and |
---|
411 | firstday <= now() and |
---|
412 | (lastday is null or lastday >= now() - '1 days'::interval) |
---|
413 | order by firstday asc |
---|
414 | }); |
---|
415 | $list_empl->execute($self->id); |
---|
416 | my $start; |
---|
417 | while (my $res = $list_empl->fetchrow_hashref) { |
---|
418 | $start = DateTime->from_epoch(epoch => str2time($res->{firstday})); |
---|
419 | $start->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
420 | last; |
---|
421 | } |
---|
422 | $list_empl->finish; |
---|
423 | |
---|
424 | return $start ? $start->iso8601 : undef |
---|
425 | }, |
---|
426 | }, |
---|
427 | startCurrentEmployment => { |
---|
428 | formtype => 'DATETIME', |
---|
429 | ro => 1, |
---|
430 | }, |
---|
431 | _startFirstEmployment => { |
---|
432 | formtype => 'DATETIME', |
---|
433 | managed => 1, |
---|
434 | ro => 1, |
---|
435 | hide => 1, |
---|
436 | get => sub { |
---|
437 | my ($attr) = @_; |
---|
438 | my $self = $attr->object; |
---|
439 | |
---|
440 | my $list_empl = $self->base->db->prepare_cached(q{ |
---|
441 | SELECT * FROM employment WHERE "user" = ? |
---|
442 | order by firstday asc |
---|
443 | }); |
---|
444 | $list_empl->execute($self->id); |
---|
445 | my $res = $list_empl->fetchrow_hashref; |
---|
446 | $list_empl->finish; |
---|
447 | my $start; |
---|
448 | if ($res && $res->{firstday}) { |
---|
449 | $start = DateTime->from_epoch(epoch => str2time($res->{firstday})); |
---|
450 | $start->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
451 | } |
---|
452 | return $start ? $start->iso8601 : undef |
---|
453 | }, |
---|
454 | label => l('Start of any employment'), |
---|
455 | }, |
---|
456 | startFirstEmployment => { |
---|
457 | formtype => 'DATETIME', |
---|
458 | ro => 1, |
---|
459 | label => l('Start of any employment'), |
---|
460 | }, |
---|
461 | employmentLength => { |
---|
462 | ro => 1, |
---|
463 | managed => 1, |
---|
464 | get => sub { |
---|
465 | my ($self) = @_; |
---|
466 | my $lastday = $self->object->get_attributes('endEmployment') || DateTime->now->ymd('-'); |
---|
467 | my $firstday = $self->object->get_attributes('startEmployment') or return; |
---|
468 | |
---|
469 | my @fd = split('-', DateTime->from_epoch(epoch => str2time($firstday))->ymd('-')); |
---|
470 | my @ld = split('-', DateTime->from_epoch(epoch => str2time($lastday))->ymd('-')); |
---|
471 | |
---|
472 | return Date::Calc::Delta_Days(@fd, @ld) +1; |
---|
473 | }, |
---|
474 | label => l('Work duration'), |
---|
475 | }, |
---|
476 | employmentLengthText => { |
---|
477 | ro => 1, |
---|
478 | managed => 1, |
---|
479 | get => sub { |
---|
480 | my ($self) = @_; |
---|
481 | my $firstday = $self->object->get_attributes('startEmployment') or return; |
---|
482 | my $lastday = $self->object->get_attributes('endEmployment')|| DateTime->now->ymd('-'); |
---|
483 | { |
---|
484 | my $dtlast = DateTime->from_epoch(epoch => str2time($lastday)); |
---|
485 | $dtlast->add(days => 1); |
---|
486 | $lastday = $dtlast->ymd('-'); |
---|
487 | } |
---|
488 | |
---|
489 | my @fd = split('-', DateTime->from_epoch(epoch => str2time($firstday))->ymd('-')); |
---|
490 | my @ld = split('-', DateTime->from_epoch(epoch => str2time($lastday))->ymd('-')); |
---|
491 | |
---|
492 | my ($Dy,$Dm,$Dd) = Date::Calc::N_Delta_YMD(@fd, @ld); |
---|
493 | return join(', ', |
---|
494 | ($Dy ? l('%d years', $Dy) : ()), |
---|
495 | ($Dm ? l('%d months', $Dm) : ()), |
---|
496 | ($Dd ? l('%d days', $Dd) : ()), |
---|
497 | ); |
---|
498 | }, |
---|
499 | label => l('Work duration'), |
---|
500 | }, |
---|
501 | cn => { |
---|
502 | inline => 1, ro => 1, |
---|
503 | get => sub { |
---|
504 | my ($self) = @_; |
---|
505 | return join(' ', grep { $_ } |
---|
506 | ( |
---|
507 | $self->object->_get_c_field('givenName'), |
---|
508 | $self->object->_get_c_field('sn') |
---|
509 | ) |
---|
510 | ) |
---|
511 | || $self->object->_get_c_field('description') |
---|
512 | || $self->object->id; |
---|
513 | }, |
---|
514 | }, |
---|
515 | memberOf => { |
---|
516 | reference => 'group', |
---|
517 | multiple => 1, delayed => 1, |
---|
518 | get => sub { |
---|
519 | my ($self) = @_; |
---|
520 | my $obj = $self->object; |
---|
521 | my $sth = $obj->db->prepare_cached( |
---|
522 | q{ |
---|
523 | select name from "group" join |
---|
524 | group_attributes on group_attributes.okey = "group".ikey |
---|
525 | where value = ? and attr = ? and internobject = false |
---|
526 | } . ($self->base->{wexported} ? '' : ' and "group".exported = true') |
---|
527 | ); |
---|
528 | $sth->execute($obj->id, 'memberUID'); |
---|
529 | my @res; |
---|
530 | while (my $res = $sth->fetchrow_hashref) { |
---|
531 | push(@res, $res->{name}); |
---|
532 | } |
---|
533 | return \@res; |
---|
534 | }, |
---|
535 | set => sub { |
---|
536 | my ($self, $values) = @_; |
---|
537 | my %old = map { $_ => 'o' } @{ $self->get }; |
---|
538 | foreach my $group (grep { $_ } ref $values ? @{ $values } : $values) { |
---|
539 | if ($old{$group}) { |
---|
540 | $old{$group} = undef; # no change |
---|
541 | } else { |
---|
542 | $old{$group} = 'n'; |
---|
543 | } |
---|
544 | } |
---|
545 | |
---|
546 | my $res = 0; |
---|
547 | foreach my $group (keys %old) { |
---|
548 | $old{$group} or next; # no change |
---|
549 | |
---|
550 | my $ogroup = $self->base->get_object('group', $group) or next; |
---|
551 | ($ogroup->_get_c_field('sutype') || '') =~ /^(jobtype|contrattype)$/ and next; |
---|
552 | |
---|
553 | if ($old{$group} eq 'n') { |
---|
554 | $res += $ogroup->_addAttributeValue('memberUID', $self->object->id); |
---|
555 | } else { |
---|
556 | if (($self->object->_get_c_field('department') || '') eq $ogroup->id) { |
---|
557 | $self->base->log(LA_WARN, |
---|
558 | "Don't removing user %s from group %s: is its department", |
---|
559 | $self->object->id, $ogroup->id); |
---|
560 | next; |
---|
561 | } |
---|
562 | $res += $ogroup->_delAttributeValue('memberUID', $self->object->id); |
---|
563 | } |
---|
564 | } |
---|
565 | return $res; |
---|
566 | }, |
---|
567 | label => l('Member of'), |
---|
568 | }, |
---|
569 | forward => { |
---|
570 | managed => 1, |
---|
571 | multiple => 1, |
---|
572 | get => sub { |
---|
573 | my ($self) = @_; |
---|
574 | my $sth = $self->base->db->prepare(q{ |
---|
575 | select forward from aliases where name = ? and internobject = false |
---|
576 | } . ($self->base->{wexported} ? '' : ' and exported = true')); |
---|
577 | $sth->execute($self->object->id); |
---|
578 | my $res = $sth->fetchrow_hashref; |
---|
579 | $sth->finish; |
---|
580 | return $res->{forward} |
---|
581 | }, |
---|
582 | set => sub { |
---|
583 | my ($self, $data) = @_; |
---|
584 | if ($data) { |
---|
585 | my @datas = ref $data ? @$data : split(/\s*,\s*/, $data); |
---|
586 | if (my $f = $self->base->get_object('aliases', $self->object->id)) { |
---|
587 | return $f->_set_c_fields( |
---|
588 | forward => \@datas, |
---|
589 | comment => undef, |
---|
590 | description => 'Forward for user ' . $self->object->id, |
---|
591 | ); |
---|
592 | } else { |
---|
593 | if ($self->base->_create_c_object( |
---|
594 | 'aliases', $self->object->id, |
---|
595 | forward => \@datas, |
---|
596 | description => 'automatically created for ' . $self->object->id, |
---|
597 | )) { |
---|
598 | return 1; |
---|
599 | } else { |
---|
600 | $self->base->log(LA_ERR, "Cannot add forward for %s", |
---|
601 | $self->object->id); |
---|
602 | } |
---|
603 | } |
---|
604 | } elsif ($self->base->get_object('aliases', $self->object->id)) { |
---|
605 | if (my $res = $self->base->_delete_object('aliases', $self->object->id)) { |
---|
606 | return $res; |
---|
607 | } else { |
---|
608 | $self->base->log(LA_ERR, "Cannot remove forward for %s", |
---|
609 | $self->object->id); |
---|
610 | } |
---|
611 | } else { |
---|
612 | return 1; |
---|
613 | } |
---|
614 | return; |
---|
615 | }, |
---|
616 | label => l('Forward'), |
---|
617 | }, |
---|
618 | aliases => { |
---|
619 | #reference => 'aliases', |
---|
620 | delayed => 1, |
---|
621 | formtype => 'TEXT', |
---|
622 | multiple => 1, |
---|
623 | get => sub { |
---|
624 | my ($self) = @_; |
---|
625 | my $sth = $self->base->db->prepare(q{ |
---|
626 | select name from aliases where internobject = false and lower($1) = |
---|
627 | lower(array_to_string("forward", ',')) |
---|
628 | } . ($self->base->{wexported} ? '' : 'and exported = true')); |
---|
629 | $sth->execute($self->object->id); |
---|
630 | my @values; |
---|
631 | while (my $res = $sth->fetchrow_hashref) { |
---|
632 | push(@values, $res->{name}); |
---|
633 | } |
---|
634 | return \@values; |
---|
635 | }, |
---|
636 | set => sub { |
---|
637 | my ($self, $data) = @_; |
---|
638 | |
---|
639 | my $res = 0; |
---|
640 | my %aliases = map { $_ => 1 } grep { $_ } (ref $data ? @{$data} : $data); |
---|
641 | foreach ($self->object->_get_attributes('aliases')) { |
---|
642 | $aliases{$_} ||= 0; |
---|
643 | $aliases{$_} +=2; |
---|
644 | } |
---|
645 | foreach (keys %aliases) { |
---|
646 | if ($aliases{$_} == 2) { |
---|
647 | if ($self->base->_delete_object('aliases', $_)) { |
---|
648 | $res++ |
---|
649 | } else { |
---|
650 | $self->base->log(LA_ERR, |
---|
651 | "Cannot remove aliases %s from user %s", $_, |
---|
652 | $self->object->id); |
---|
653 | } |
---|
654 | } elsif ($aliases{$_} == 1) { |
---|
655 | if ($self->base->_create_c_object( |
---|
656 | 'aliases', $_, |
---|
657 | forward => [ $self->object->id ], |
---|
658 | description => 'automatically created for ' . $self->object->id, |
---|
659 | )) { |
---|
660 | $res++ |
---|
661 | } else { |
---|
662 | $self->base->log(LA_ERR, 'Cannot set forward %s to user %s', |
---|
663 | $_, $self->object->id); |
---|
664 | return; |
---|
665 | } |
---|
666 | } # 3 no change |
---|
667 | } |
---|
668 | $res |
---|
669 | }, |
---|
670 | label => l('Aliases'), |
---|
671 | }, |
---|
672 | revaliases => { |
---|
673 | formtype => 'TEXT', |
---|
674 | get => sub { |
---|
675 | my ($self) = @_; |
---|
676 | |
---|
677 | if (my $obj = $self->base-> |
---|
678 | get_object('revaliases', $self->object->id)) { |
---|
679 | return $obj->get_attributes('as'); |
---|
680 | } else { |
---|
681 | return; |
---|
682 | } |
---|
683 | }, |
---|
684 | set => sub { |
---|
685 | my ($self, $data) = @_; |
---|
686 | |
---|
687 | my $res = 0; |
---|
688 | if ($data) { |
---|
689 | if (my $obj = $self->base-> |
---|
690 | get_object('revaliases', $self->object->id)) { |
---|
691 | my $ares = $obj->set_c_fields( |
---|
692 | 'as' => $data, |
---|
693 | 'exported' => ($self->object->get_attributes('exported') || 0), |
---|
694 | ); |
---|
695 | if (defined($ares)) { |
---|
696 | $res+=$ares; |
---|
697 | } else { |
---|
698 | $self->base->log(LA_ERR, 'Cannot set revaliases for user %s', |
---|
699 | $self->object->id); |
---|
700 | } |
---|
701 | } else { |
---|
702 | if ($self->base->_create_c_object( |
---|
703 | 'revaliases', |
---|
704 | $self->object->id, as => $data, |
---|
705 | 'exported' => ($self->object->get_attributes('exported') || 0), |
---|
706 | description => 'automatically created for ' . $self->object->id, |
---|
707 | )) { |
---|
708 | $res++; |
---|
709 | } else { |
---|
710 | $self->base->log(LA_ERR, 'Cannot set revaliases for user %s', |
---|
711 | $self->object->id); |
---|
712 | } |
---|
713 | } |
---|
714 | } else { |
---|
715 | $self->base->_delete_object('revaliases', $self->object->id); |
---|
716 | $res++; |
---|
717 | } |
---|
718 | |
---|
719 | $res |
---|
720 | }, |
---|
721 | }, |
---|
722 | manager => { |
---|
723 | reference => 'user', |
---|
724 | ro => 1, |
---|
725 | get => sub { |
---|
726 | my ($self) = @_; |
---|
727 | if (my $manager = $self->object->_get_c_field('managerContact')) { |
---|
728 | return $manager; |
---|
729 | } elsif (my $department = $self->object->_get_c_field('department')) { |
---|
730 | my $obj = $self->base->get_object('group', $department); |
---|
731 | return $obj->_get_c_field('managedBy'); |
---|
732 | } else { |
---|
733 | return; |
---|
734 | } |
---|
735 | }, |
---|
736 | label => l('Responsible'), |
---|
737 | }, |
---|
738 | department => { |
---|
739 | reference => 'group', |
---|
740 | can_values => sub { |
---|
741 | $base->search_objects('group', 'sutype=dpmt') |
---|
742 | }, |
---|
743 | monitored => 1, |
---|
744 | label => l('Department'), |
---|
745 | }, |
---|
746 | contratType => { |
---|
747 | reference => 'group', |
---|
748 | can_values => sub { |
---|
749 | $base->search_objects('group', 'sutype=contrattype') |
---|
750 | }, |
---|
751 | monitored => 1, |
---|
752 | label => l('Type of contract'), |
---|
753 | }, |
---|
754 | site => { |
---|
755 | reference => 'site', |
---|
756 | can_values => sub { |
---|
757 | $base->search_objects('site') |
---|
758 | }, |
---|
759 | get => sub { |
---|
760 | my ($self) = @_; |
---|
761 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
762 | $self->base->get_object('address', $fmainaddress) |
---|
763 | ->_get_c_field($self->name); |
---|
764 | } else { |
---|
765 | return; |
---|
766 | } |
---|
767 | }, |
---|
768 | set => $subsetaddress, |
---|
769 | label => l('Site'), |
---|
770 | }, |
---|
771 | co => { |
---|
772 | get => sub { |
---|
773 | my ($self) = @_; |
---|
774 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
775 | $self->base->get_object('address', $fmainaddress) |
---|
776 | ->_get_c_field($self->name); |
---|
777 | } else { |
---|
778 | return; |
---|
779 | } |
---|
780 | }, |
---|
781 | set => $subsetaddress, |
---|
782 | }, |
---|
783 | l => { |
---|
784 | get => sub { |
---|
785 | my ($self) = @_; |
---|
786 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
787 | $self->base->get_object('address', $fmainaddress) |
---|
788 | ->_get_c_field($self->name); |
---|
789 | } else { |
---|
790 | return; |
---|
791 | } |
---|
792 | }, |
---|
793 | set => $subsetaddress, |
---|
794 | label => l('City'), |
---|
795 | }, |
---|
796 | postalCode => { |
---|
797 | get => sub { |
---|
798 | my ($self) = @_; |
---|
799 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
800 | $self->base->get_object('address', $fmainaddress) |
---|
801 | ->_get_c_field($self->name); |
---|
802 | } else { |
---|
803 | return; |
---|
804 | } |
---|
805 | }, |
---|
806 | set => $subsetaddress, |
---|
807 | label => l('Postal code'), |
---|
808 | }, |
---|
809 | streetAddress => { |
---|
810 | formtype => 'TEXTAREA', |
---|
811 | get => sub { |
---|
812 | my ($self) = @_; |
---|
813 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
814 | $self->base->get_object('address', $fmainaddress) |
---|
815 | ->_get_c_field($self->name); |
---|
816 | } else { |
---|
817 | return; |
---|
818 | } |
---|
819 | }, |
---|
820 | set => $subsetaddress, |
---|
821 | label => l('Street'), |
---|
822 | }, |
---|
823 | postOfficeBox => { |
---|
824 | get => sub { |
---|
825 | my ($self) = @_; |
---|
826 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
827 | $self->base->get_object('address', $fmainaddress) |
---|
828 | ->_get_c_field($self->name); |
---|
829 | } else { |
---|
830 | return; |
---|
831 | } |
---|
832 | }, |
---|
833 | set => $subsetaddress, |
---|
834 | label => l('Post office box'), |
---|
835 | }, |
---|
836 | st => { |
---|
837 | get => sub { |
---|
838 | my ($self) = @_; |
---|
839 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
840 | $self->base->get_object('address', $fmainaddress) |
---|
841 | ->_get_c_field($self->name); |
---|
842 | } else { |
---|
843 | return; |
---|
844 | } |
---|
845 | }, |
---|
846 | set => $subsetaddress, |
---|
847 | }, |
---|
848 | facsimileTelephoneNumber => { |
---|
849 | get => sub { |
---|
850 | my ($self) = @_; |
---|
851 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
852 | $self->base->get_object('address', $fmainaddress) |
---|
853 | ->_get_c_field($self->name); |
---|
854 | } else { |
---|
855 | return; |
---|
856 | } |
---|
857 | }, |
---|
858 | set => $subsetaddress, |
---|
859 | label => l('Fax number'), |
---|
860 | }, |
---|
861 | o => { |
---|
862 | ro => 1, |
---|
863 | iname => 'company', |
---|
864 | label => l('Company'), |
---|
865 | }, |
---|
866 | ou => { |
---|
867 | iname => 'department', |
---|
868 | ro => 1, |
---|
869 | label => l('Department'), |
---|
870 | }, |
---|
871 | telephoneNumber => { |
---|
872 | get => sub { |
---|
873 | my ($self) = @_; |
---|
874 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
875 | $self->base->get_object('address', $fmainaddress) |
---|
876 | ->_get_c_field($self->name); |
---|
877 | } else { |
---|
878 | return; |
---|
879 | } |
---|
880 | }, |
---|
881 | set => $subsetaddress, |
---|
882 | label => l('Phone number'), |
---|
883 | }, |
---|
884 | physicalDeliveryOfficeName => { |
---|
885 | get => sub { |
---|
886 | my ($self) = @_; |
---|
887 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
888 | $self->base->get_object('address', $fmainaddress) |
---|
889 | ->_get_c_field($self->name); |
---|
890 | } else { |
---|
891 | return; |
---|
892 | } |
---|
893 | }, |
---|
894 | set => $subsetaddress, |
---|
895 | label => l('Office'), |
---|
896 | }, |
---|
897 | uid => { |
---|
898 | iname => 'name', |
---|
899 | ro => 1, |
---|
900 | label => l('Login'), |
---|
901 | }, |
---|
902 | cn => { iname => 'name', ro => 1 }, |
---|
903 | gecos => { |
---|
904 | managed => 1, |
---|
905 | ro => 1, |
---|
906 | get => sub { |
---|
907 | my ($self) = @_; |
---|
908 | my $obj = $self->object; |
---|
909 | |
---|
910 | my $ec = $obj->_get_c_field('endcircuit') || ''; |
---|
911 | |
---|
912 | my $date = $ec |
---|
913 | ? sprintf('%s (%s)', $obj->_get_c_field('expireTextEC'), $obj->_get_c_field('expireText')) |
---|
914 | : $obj->_get_c_field('expireText'); |
---|
915 | |
---|
916 | $date ||= ''; |
---|
917 | |
---|
918 | my $gecos = sprintf("%s,%s,%s,%s", |
---|
919 | join(' ', grep { $_ } |
---|
920 | ($obj->_get_c_field('givenName'), |
---|
921 | ($obj->_get_c_field('sn')))) |
---|
922 | || $obj->_get_c_field('description') || '', |
---|
923 | join(' - ', grep { $_ } (($obj->_get_c_field('site') || |
---|
924 | $obj->_get_c_field('l')), |
---|
925 | $obj->_get_c_field('physicalDeliveryOfficeName'))) || '', |
---|
926 | $obj->_get_c_field('telephoneNumber') || '', |
---|
927 | $date, |
---|
928 | ); |
---|
929 | $gecos =~ s/:/ /g; |
---|
930 | return to_ascii($gecos); |
---|
931 | }, |
---|
932 | label => l('GECOS'), |
---|
933 | }, |
---|
934 | displayName => { |
---|
935 | ro => 1, managed => 1, |
---|
936 | get => sub { |
---|
937 | my ($self) = @_; |
---|
938 | return join(' ', grep { $_ } |
---|
939 | ( |
---|
940 | $self->object->_get_c_field('givenName'), |
---|
941 | $self->object->_get_c_field('sn') |
---|
942 | ) |
---|
943 | ) |
---|
944 | || $self->object->_get_c_field('description') |
---|
945 | || $self->object->id; |
---|
946 | }, |
---|
947 | label => l('Name'), |
---|
948 | }, |
---|
949 | sAMAccountName => { |
---|
950 | ro => 1, |
---|
951 | managed => 1, |
---|
952 | iname => 'name', |
---|
953 | }, |
---|
954 | accountExpires => { |
---|
955 | ro => 1, |
---|
956 | managed => 1, |
---|
957 | get => sub { |
---|
958 | my ($self) = @_; |
---|
959 | my $obj = $self->object; |
---|
960 | my $sth = $obj->db->prepare_cached( |
---|
961 | sprintf( |
---|
962 | q{select extract(epoch from COALESCE(endcircuit, expire)) + 11644474161 as expire |
---|
963 | from %s where %s = ?}, |
---|
964 | $obj->db->quote_identifier($obj->_object_table), |
---|
965 | $obj->db->quote_identifier($obj->_key_field), |
---|
966 | ) |
---|
967 | ); |
---|
968 | $sth->execute($obj->id); |
---|
969 | my $res = $sth->fetchrow_hashref; |
---|
970 | $sth->finish; |
---|
971 | return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807'; |
---|
972 | } |
---|
973 | }, |
---|
974 | shadowExpire => { |
---|
975 | ro => 1, |
---|
976 | managed => 1, |
---|
977 | get => sub { |
---|
978 | my ($self) = @_; |
---|
979 | my $obj = $self->object; |
---|
980 | my $sth = $obj->db->prepare_cached( |
---|
981 | sprintf( |
---|
982 | q{select justify_hours(COALESCE(endcircuit, expire) - '1/1/1970'::timestamp) as expire |
---|
983 | from %s where %s = ?}, |
---|
984 | $obj->db->quote_identifier($obj->_object_table), |
---|
985 | $obj->db->quote_identifier($obj->_key_field), |
---|
986 | ) |
---|
987 | ); |
---|
988 | $sth->execute($obj->id); |
---|
989 | my $res = $sth->fetchrow_hashref; |
---|
990 | $sth->finish; |
---|
991 | return -1 unless($res->{expire}); |
---|
992 | $res->{expire} =~ /(\d+) days\s*(\w)?/; |
---|
993 | # Add one day is time is not 00H00 |
---|
994 | return $1 + ($2 ? 1 : 0); |
---|
995 | } |
---|
996 | }, |
---|
997 | directReports => { |
---|
998 | reference => 'user', |
---|
999 | ro => 1, |
---|
1000 | delayed => 1, |
---|
1001 | get => sub { |
---|
1002 | my ($self) = @_; |
---|
1003 | my $obj = $self->object; |
---|
1004 | my $sth = $obj->db->prepare_cached( |
---|
1005 | q{ |
---|
1006 | SELECT |
---|
1007 | "user".name FROM |
---|
1008 | public."user", |
---|
1009 | public.user_attributes_groups, |
---|
1010 | public.group_attributes_users, |
---|
1011 | public.group_attributes_base gb, |
---|
1012 | public."group" |
---|
1013 | WHERE |
---|
1014 | "user".ikey = user_attributes_groups.okey AND |
---|
1015 | user_attributes_groups.value = "group".name AND |
---|
1016 | group_attributes_users.okey = gb.okey AND |
---|
1017 | "group".ikey = group_attributes_users.okey AND |
---|
1018 | gb.attr = 'sutype' AND |
---|
1019 | gb.value = 'dpmt' AND |
---|
1020 | group_attributes_users.attr = 'managedBy' AND |
---|
1021 | group_attributes_users.value = ? |
---|
1022 | } . ($self->base->{wexported} ? '' : ' and "user".exported = true') . q{ |
---|
1023 | and "user".ikey not in |
---|
1024 | (select okey from user_attributes_users |
---|
1025 | where attr = 'manager' and value != ? ) |
---|
1026 | union |
---|
1027 | select "user".name FROM public."user", |
---|
1028 | user_attributes_users where |
---|
1029 | user_attributes_users.attr = 'manager' and user_attributes_users.value = ? |
---|
1030 | and "user".ikey = user_attributes_users.okey |
---|
1031 | } . ($self->base->{wexported} ? '' : ' and "user".exported = true') |
---|
1032 | ); |
---|
1033 | $sth->execute($obj->id, $obj->id, $obj->id); |
---|
1034 | my @res; |
---|
1035 | while (my $res = $sth->fetchrow_hashref) { |
---|
1036 | push(@res, $res->{name}); |
---|
1037 | } |
---|
1038 | return \@res; |
---|
1039 | }, |
---|
1040 | }, |
---|
1041 | managedObjects => { ro => 1, reference => 'group', }, |
---|
1042 | otheraddress => { |
---|
1043 | ro => 1, |
---|
1044 | reference => 'address', |
---|
1045 | get => sub { |
---|
1046 | my ($self) = @_; |
---|
1047 | my $sth = $self->base->db->prepare_cached(q{ |
---|
1048 | select name from address left join address_attributes |
---|
1049 | on address.ikey = address_attributes.okey and |
---|
1050 | address_attributes.attr = 'isMainAddress' |
---|
1051 | where "user" = ? |
---|
1052 | order by address_attributes.attr |
---|
1053 | }); |
---|
1054 | $sth->execute($self->object->id); |
---|
1055 | my @values; |
---|
1056 | while (my $res = $sth->fetchrow_hashref) { |
---|
1057 | push(@values, $res->{name}); |
---|
1058 | } |
---|
1059 | return \@values; |
---|
1060 | }, |
---|
1061 | }, |
---|
1062 | mainaddress => { |
---|
1063 | ro => 1, |
---|
1064 | reference => 'address', |
---|
1065 | get => sub { |
---|
1066 | my ($self) = @_; |
---|
1067 | my $sth = $self->base->db->prepare_cached(q{ |
---|
1068 | select name from address join address_attributes on ikey = okey |
---|
1069 | where "user" = ? and attr = 'isMainAddress' |
---|
1070 | }); |
---|
1071 | $sth->execute($self->object->id); |
---|
1072 | my $res = $sth->fetchrow_hashref; |
---|
1073 | $sth->finish; |
---|
1074 | return $res->{name}; |
---|
1075 | }, |
---|
1076 | }, |
---|
1077 | postalAddress => { |
---|
1078 | ro => 1, |
---|
1079 | get => sub { |
---|
1080 | my ($self) = @_; |
---|
1081 | if (my $fmainaddress = $self->object->_get_c_field('mainaddress')) { |
---|
1082 | $self->base->get_object('address', $fmainaddress) |
---|
1083 | ->_get_c_field($self->name); |
---|
1084 | } else { |
---|
1085 | return; |
---|
1086 | } |
---|
1087 | }, |
---|
1088 | label => l('Postal Address'), |
---|
1089 | }, |
---|
1090 | facsimileTelephoneNumber => { |
---|
1091 | ro => 1, |
---|
1092 | label => l('Fax number'), |
---|
1093 | }, |
---|
1094 | allsite => { |
---|
1095 | ro => 1, |
---|
1096 | reference => 'site', |
---|
1097 | }, |
---|
1098 | managerContact => { |
---|
1099 | delayed => 1, |
---|
1100 | can_values => sub { |
---|
1101 | my %uniq = map { $_ => 1 } grep { $_ } |
---|
1102 | (($_[1] ? $_[1]->get_attributes('managerContact') : ()), |
---|
1103 | $base->search_objects('user', 'active=1')); |
---|
1104 | sort keys %uniq; |
---|
1105 | }, |
---|
1106 | reference => 'user', |
---|
1107 | monitored => 1, |
---|
1108 | iname => 'manager', |
---|
1109 | label => l('Manager'), |
---|
1110 | }, |
---|
1111 | expireTextEC => { |
---|
1112 | ro => 1, |
---|
1113 | managed => 1, |
---|
1114 | get => sub { |
---|
1115 | my ($self) = @_; |
---|
1116 | my $obj = $self->object; |
---|
1117 | my $sth = $obj->db->prepare_cached( |
---|
1118 | sprintf( |
---|
1119 | q{select to_char(COALESCE(endcircuit, expire), 'YYYY/MM/DD') as expire |
---|
1120 | from %s where %s = ?}, |
---|
1121 | $obj->db->quote_identifier($obj->_object_table), |
---|
1122 | $obj->db->quote_identifier($obj->_key_field), |
---|
1123 | ) |
---|
1124 | ); |
---|
1125 | $sth->execute($obj->id) or $obj->db->rollback; |
---|
1126 | my $res = $sth->fetchrow_hashref; |
---|
1127 | $sth->finish; |
---|
1128 | return $res->{expire} |
---|
1129 | }, |
---|
1130 | }, |
---|
1131 | expireText => { |
---|
1132 | ro => 1, |
---|
1133 | managed => 1, |
---|
1134 | get => sub { |
---|
1135 | my ($self) = @_; |
---|
1136 | my $obj = $self->object; |
---|
1137 | my $sth = $obj->db->prepare_cached( |
---|
1138 | sprintf( |
---|
1139 | q{select to_char(expire, 'YYYY/MM/DD') as expire |
---|
1140 | from %s where %s = ?}, |
---|
1141 | $obj->db->quote_identifier($obj->_object_table), |
---|
1142 | $obj->db->quote_identifier($obj->_key_field), |
---|
1143 | ) |
---|
1144 | ); |
---|
1145 | $sth->execute($obj->id) or $obj->db->rollback; |
---|
1146 | my $res = $sth->fetchrow_hashref; |
---|
1147 | $sth->finish; |
---|
1148 | return $res->{expire} |
---|
1149 | }, |
---|
1150 | }, |
---|
1151 | krb5ValidEnd => { |
---|
1152 | ro => 1, |
---|
1153 | managed => 1, |
---|
1154 | get => sub { |
---|
1155 | my ($self) = @_; |
---|
1156 | my $sth = $self->object->db->prepare_cached( |
---|
1157 | sprintf( |
---|
1158 | q{select date_part('epoch', COALESCE(endcircuit, expire))::int as expire |
---|
1159 | from %s where %s = ?}, |
---|
1160 | $self->object->db->quote_identifier($self->object->_object_table), |
---|
1161 | $self->object->db->quote_identifier($self->object->_key_field), |
---|
1162 | ) |
---|
1163 | ); |
---|
1164 | $sth->execute($self->object->id) or $self->object->db->rollback; |
---|
1165 | my $res = $sth->fetchrow_hashref; |
---|
1166 | $sth->finish; |
---|
1167 | return $res->{expire} |
---|
1168 | }, |
---|
1169 | }, |
---|
1170 | cells => { |
---|
1171 | ro => 1, |
---|
1172 | reference => 'group', |
---|
1173 | }, |
---|
1174 | departments => { |
---|
1175 | reference => 'group', |
---|
1176 | delayed => 1, |
---|
1177 | ro => 1, |
---|
1178 | label => l('Departments'), |
---|
1179 | }, |
---|
1180 | arrivalDate => { }, |
---|
1181 | expired => { |
---|
1182 | ro => 1, |
---|
1183 | label => l('Expired'), |
---|
1184 | }, |
---|
1185 | active => { |
---|
1186 | ro => 1, |
---|
1187 | label => l('Active'), |
---|
1188 | }, |
---|
1189 | status => { |
---|
1190 | ro => 1, |
---|
1191 | label => l('Statut du compte'), |
---|
1192 | }, |
---|
1193 | pwdAccountLockedTime => { |
---|
1194 | managed => 1, |
---|
1195 | ro => 1, |
---|
1196 | get => sub { |
---|
1197 | my ($self) = @_; |
---|
1198 | my $obj = $self->object; |
---|
1199 | if ($obj->_get_c_field('locked')) { |
---|
1200 | return '000001010000Z'; |
---|
1201 | } else { |
---|
1202 | my $sth = $obj->db->prepare_cached( |
---|
1203 | sprintf( |
---|
1204 | q{select to_char(COALESCE(endcircuit, expire) AT TIME ZONE 'Z', 'YYYYMMDDHH24MISSZ') as expire |
---|
1205 | from %s where %s = ? and expire < now()}, |
---|
1206 | $obj->db->quote_identifier($obj->_object_table), |
---|
1207 | $obj->db->quote_identifier($obj->_key_field), |
---|
1208 | ) |
---|
1209 | ); |
---|
1210 | $sth->execute($obj->id); |
---|
1211 | my $res = $sth->fetchrow_hashref; |
---|
1212 | $sth->finish; |
---|
1213 | return $res->{expire} |
---|
1214 | } |
---|
1215 | }, |
---|
1216 | }, |
---|
1217 | userPassword => { readable => 0, }, |
---|
1218 | wWWHomePage => { |
---|
1219 | label => l('Web Page'), |
---|
1220 | formopts => { length => 35 }, |
---|
1221 | }, |
---|
1222 | title => { }, |
---|
1223 | snNative => { |
---|
1224 | label => l('Native name'), |
---|
1225 | }, |
---|
1226 | givenNameNative => { |
---|
1227 | label => l('Native first name'), |
---|
1228 | }, |
---|
1229 | sn => { |
---|
1230 | label => l('Name'), |
---|
1231 | }, |
---|
1232 | shadowWarning => { }, |
---|
1233 | shadowMin => { }, |
---|
1234 | shadowMax => { }, |
---|
1235 | shadowLastChange => { }, |
---|
1236 | shadowInactive => { }, |
---|
1237 | shadowFlag => { }, |
---|
1238 | otherTelephone => { }, |
---|
1239 | nickname => { |
---|
1240 | label => l('Nickname'), |
---|
1241 | }, |
---|
1242 | mobile => { }, |
---|
1243 | mail => { |
---|
1244 | label => l('Email'), |
---|
1245 | }, |
---|
1246 | otherEmail => { |
---|
1247 | label => l('External mail'), |
---|
1248 | }, |
---|
1249 | labeledURI => { }, |
---|
1250 | jobType => { }, |
---|
1251 | ipPhone => { }, |
---|
1252 | initials => { |
---|
1253 | label => l('Initials'), |
---|
1254 | }, |
---|
1255 | homePhone => { }, |
---|
1256 | homeDirectory => { |
---|
1257 | label => l('Home directory'), |
---|
1258 | }, |
---|
1259 | halReference => { |
---|
1260 | label => l('HAL id'), |
---|
1261 | }, |
---|
1262 | grade => { }, |
---|
1263 | givenName => { |
---|
1264 | label => l('First name'), |
---|
1265 | }, |
---|
1266 | encryptedPassword => { }, |
---|
1267 | description => { |
---|
1268 | label => l('Description'), |
---|
1269 | }, |
---|
1270 | company => { |
---|
1271 | label => l('Company'), |
---|
1272 | }, |
---|
1273 | employer => { |
---|
1274 | label => l('Employer'), |
---|
1275 | }, |
---|
1276 | comment => { |
---|
1277 | label => l('Comment'), |
---|
1278 | }, |
---|
1279 | college => { }, |
---|
1280 | passwordLastSet => { |
---|
1281 | ro => 1, |
---|
1282 | label => l('Password set'), |
---|
1283 | }, |
---|
1284 | oldPassword => { |
---|
1285 | multiple => 1, |
---|
1286 | }, |
---|
1287 | bannedPassword => { |
---|
1288 | multiple => 1, |
---|
1289 | }, |
---|
1290 | sshPublicKey => { |
---|
1291 | multiple => 1, |
---|
1292 | formopts => { length => 45 }, |
---|
1293 | }, |
---|
1294 | currentEmployment => { |
---|
1295 | managed => 1, |
---|
1296 | ro => 1, |
---|
1297 | reference => 'employment', |
---|
1298 | get => sub { |
---|
1299 | my ($attr) = @_; |
---|
1300 | my $self = $attr->object; |
---|
1301 | |
---|
1302 | my $sth = $self->base->db->prepare_cached( |
---|
1303 | q{ |
---|
1304 | select name from employment where firstday <= now() and |
---|
1305 | (lastday is null or lastday >= now() - '1 days'::interval) and "user" = ? |
---|
1306 | limit 1 |
---|
1307 | } |
---|
1308 | ); |
---|
1309 | $sth->execute($self->id); |
---|
1310 | my $res = $sth->fetchrow_hashref; |
---|
1311 | $sth->finish; |
---|
1312 | if ($res) { |
---|
1313 | return $res->{name} |
---|
1314 | } else { |
---|
1315 | return; |
---|
1316 | } |
---|
1317 | }, |
---|
1318 | }, |
---|
1319 | nextEmployment => { |
---|
1320 | managed => 1, |
---|
1321 | ro => 1, |
---|
1322 | reference => 'employment', |
---|
1323 | get => sub { |
---|
1324 | my ($attr) = @_; |
---|
1325 | my $self = $attr->object; |
---|
1326 | |
---|
1327 | my $sth = $self->base->db->prepare_cached( |
---|
1328 | q{ |
---|
1329 | select name from employment where firstday > now() and "user" = ? |
---|
1330 | order by firstday asc |
---|
1331 | limit 1 |
---|
1332 | } |
---|
1333 | ); |
---|
1334 | $sth->execute($self->id); |
---|
1335 | my $res = $sth->fetchrow_hashref; |
---|
1336 | $sth->finish; |
---|
1337 | if ($res) { |
---|
1338 | return $res->{name} |
---|
1339 | } else { |
---|
1340 | return; |
---|
1341 | } |
---|
1342 | } |
---|
1343 | }, |
---|
1344 | prevEmployment => { |
---|
1345 | managed => 1, |
---|
1346 | ro => 1, |
---|
1347 | reference => 'employment', |
---|
1348 | get => sub { |
---|
1349 | my ($attr) = @_; |
---|
1350 | my $self = $attr->object; |
---|
1351 | |
---|
1352 | my $sth = $self->base->db->prepare_cached( |
---|
1353 | q{ |
---|
1354 | select name from employment where |
---|
1355 | (lastday is not null and lastday <= now() - '1 days'::interval) and "user" = ? |
---|
1356 | order by firstday desc |
---|
1357 | limit 1 |
---|
1358 | } |
---|
1359 | ); |
---|
1360 | $sth->execute($self->id); |
---|
1361 | my $res = $sth->fetchrow_hashref; |
---|
1362 | $sth->finish; |
---|
1363 | if ($res) { |
---|
1364 | return $res->{name} |
---|
1365 | } else { |
---|
1366 | return; |
---|
1367 | } |
---|
1368 | } |
---|
1369 | }, |
---|
1370 | appliedEmployement => { |
---|
1371 | hide => 1, |
---|
1372 | reference => 'employment', |
---|
1373 | }, |
---|
1374 | contratTypeHistory => { |
---|
1375 | reference => 'group', |
---|
1376 | can_values => sub { |
---|
1377 | $base->search_objects('group', 'sutype=contrattype') |
---|
1378 | }, |
---|
1379 | multiple => 1, |
---|
1380 | }, |
---|
1381 | employmentHistory => { |
---|
1382 | reference => 'employment', |
---|
1383 | multiple => 1, |
---|
1384 | ro => 1, |
---|
1385 | }, |
---|
1386 | hosted => { |
---|
1387 | formtype => 'CHECKBOX', |
---|
1388 | label => l('Hosted'), |
---|
1389 | }, |
---|
1390 | |
---|
1391 | }; |
---|
1392 | |
---|
1393 | my $employmentro = sub { |
---|
1394 | my $setting = $base->config('employment_lock_user') || 'any'; |
---|
1395 | |
---|
1396 | for ($setting) { |
---|
1397 | /^always$/ and return 1; |
---|
1398 | /^never$/ and return 0; |
---|
1399 | |
---|
1400 | $_[0] or return 0; |
---|
1401 | |
---|
1402 | /^any$/i and return $_[0]->listEmployment ? 1 : 0; |
---|
1403 | /^active/i and do { |
---|
1404 | return $_[0]->_get_c_field('currentEmployment') |
---|
1405 | ? 1 |
---|
1406 | : $_[0]->_get_c_field('nextEmployment') ? 1 : 0; |
---|
1407 | }; |
---|
1408 | /(\S+)=(\S+)/ and do { |
---|
1409 | my $attr = $_[0]->_get_c_field($1); |
---|
1410 | if (defined($attr)) { |
---|
1411 | if ($2 eq '*') { |
---|
1412 | return 1; |
---|
1413 | } elsif($2 eq $attr) { |
---|
1414 | return 1; |
---|
1415 | } else { |
---|
1416 | return 0; |
---|
1417 | } |
---|
1418 | } else { |
---|
1419 | return 0; |
---|
1420 | } |
---|
1421 | }; |
---|
1422 | } |
---|
1423 | return $_[0]->listEmployment ? 1 : 0; # default is any! |
---|
1424 | }; |
---|
1425 | |
---|
1426 | foreach (qw(contratType managerContact company endcircuit department hosted |
---|
1427 | contratTypeHistory employer)) { |
---|
1428 | $attrs->{$_}{ro} = $employmentro; |
---|
1429 | } |
---|
1430 | |
---|
1431 | $attrs->{expire}{ro} = sub { |
---|
1432 | my $expireOn = $base->config('expireOn') || ''; |
---|
1433 | if ($expireOn eq 'never') { |
---|
1434 | return 0; |
---|
1435 | } |
---|
1436 | |
---|
1437 | $employmentro->($_[0]); |
---|
1438 | }; |
---|
1439 | |
---|
1440 | $class->SUPER::_get_attr_schema($base, $attrs) |
---|
1441 | } |
---|
1442 | |
---|
1443 | sub CreateAlias { |
---|
1444 | my ($class, $base, $name, $for) = @_; |
---|
1445 | |
---|
1446 | my $stAddAlias = $base->db->prepare_cached( |
---|
1447 | q{INSERT INTO "user" (name, uidnumber, gidnumber, oalias, oaliascache) values |
---|
1448 | (?, -nextval('ikey_seq'), ?, ?, ?)} |
---|
1449 | ); |
---|
1450 | |
---|
1451 | my $ref = $base->_derefObject($class->type, $for); |
---|
1452 | my $res = $stAddAlias->execute($name, -1, $for, $ref ? $ref->id : undef); |
---|
1453 | return $res ? 1 : 0; |
---|
1454 | } |
---|
1455 | |
---|
1456 | sub _get_state { |
---|
1457 | my ($self, $state) = @_; |
---|
1458 | for ($state) { |
---|
1459 | /^expired$/ and do { |
---|
1460 | my $attribute = $self->attribute('expire'); |
---|
1461 | $attribute->check_acl('r') or return; |
---|
1462 | my $sth = $self->db->prepare_cached( |
---|
1463 | q{ select coalesce(expire < now(), false) as exp from "user" |
---|
1464 | where "user".name = ?} |
---|
1465 | ); |
---|
1466 | $sth->execute($self->id); |
---|
1467 | my $res = $sth->fetchrow_hashref; |
---|
1468 | $sth->finish; |
---|
1469 | return $res->{exp} ? 1 : 0; |
---|
1470 | }; |
---|
1471 | } |
---|
1472 | } |
---|
1473 | |
---|
1474 | sub set_fields { |
---|
1475 | my ($self, %data) = @_; |
---|
1476 | |
---|
1477 | my $old; |
---|
1478 | if (exists($data{department})) { |
---|
1479 | $old = $self->_get_attributes('department'); |
---|
1480 | } |
---|
1481 | |
---|
1482 | if (($data{department} || '') eq ($old || '')) { |
---|
1483 | # We do not remove the group, there is no change |
---|
1484 | $old = undef; |
---|
1485 | } |
---|
1486 | |
---|
1487 | if ($old) { |
---|
1488 | # If the department is no longer a department |
---|
1489 | # we do nothing |
---|
1490 | my @names = $self->base->search_objects('group', 'name=' . $old, 'sutype=dpmt'); |
---|
1491 | if (! @names) { |
---|
1492 | $old = undef; |
---|
1493 | } |
---|
1494 | } |
---|
1495 | |
---|
1496 | my $res = $self->SUPER::set_fields(%data) or return; |
---|
1497 | |
---|
1498 | if ($self->base->config('remove_old_dpmt') && $old) { |
---|
1499 | $self->base->log(LA_DEBUG, |
---|
1500 | "Removing %s from group %s (department change to %s)", |
---|
1501 | $self->id, |
---|
1502 | $old, |
---|
1503 | $data{department} || ''); |
---|
1504 | $self->_delAttributeValue('memberOf', $old) or return; |
---|
1505 | $res++; |
---|
1506 | } |
---|
1507 | |
---|
1508 | $res |
---|
1509 | } |
---|
1510 | |
---|
1511 | =head2 listEmployment |
---|
1512 | |
---|
1513 | Return the ordered list of contract |
---|
1514 | |
---|
1515 | =cut |
---|
1516 | |
---|
1517 | sub listEmployment { |
---|
1518 | my ($self) = @_; |
---|
1519 | |
---|
1520 | my $sth = $self->base->db->prepare_cached( |
---|
1521 | q{ |
---|
1522 | select name from employment where "user" = ? |
---|
1523 | order by lastday desc NULLS first |
---|
1524 | } |
---|
1525 | ); |
---|
1526 | $sth->execute($self->id); |
---|
1527 | my @list = (); |
---|
1528 | while (my $res = $sth->fetchrow_hashref) { |
---|
1529 | push(@list, $res->{name}); |
---|
1530 | } |
---|
1531 | |
---|
1532 | @list |
---|
1533 | } |
---|
1534 | |
---|
1535 | sub _reported_atributes { qw(contratType endcircuit hosted company employer) } |
---|
1536 | |
---|
1537 | =head2 applyCurrentEmployment |
---|
1538 | |
---|
1539 | Search the current employment is any and apply paramter to user |
---|
1540 | |
---|
1541 | =cut |
---|
1542 | |
---|
1543 | sub applyCurrentEmployment { |
---|
1544 | my ($self) = @_; |
---|
1545 | |
---|
1546 | # Get current employment name |
---|
1547 | my $currentempl = $self->get_attributes('currentEmployment') || ''; |
---|
1548 | |
---|
1549 | $self->base->log( |
---|
1550 | LA_DEBUG, |
---|
1551 | "Applying Employement %s to user %s", |
---|
1552 | $currentempl || '(none)', |
---|
1553 | $self->id |
---|
1554 | ); |
---|
1555 | |
---|
1556 | if (my $currentemployment = $self->base->get_object('employment', $currentempl)) { |
---|
1557 | |
---|
1558 | # If an employement apply we set the value to the user object |
---|
1559 | |
---|
1560 | $self->computeEmploymentDate; |
---|
1561 | |
---|
1562 | my %attrsets = ( |
---|
1563 | appliedEmployement => $currentemployment->id, |
---|
1564 | ); |
---|
1565 | foreach my $attr (_reported_atributes(), qw(department managerContact)) { |
---|
1566 | my $uval = $self->get_attributes($attr) || ''; |
---|
1567 | my $cval = $currentemployment->get_attributes($attr) || ''; |
---|
1568 | |
---|
1569 | for ($attr) { |
---|
1570 | if ($attr eq 'managerContact') { |
---|
1571 | if (!$cval) { |
---|
1572 | my $dpmt = $currentemployment->get_attributes('department') or last; |
---|
1573 | my $odmpt = $currentemployment->base->get_object('group', $dpmt) or last; |
---|
1574 | $cval = $odmpt->get_attributes('managedBy'); |
---|
1575 | } |
---|
1576 | } |
---|
1577 | } |
---|
1578 | |
---|
1579 | if ($uval ne $cval) { |
---|
1580 | my $oattr = $currentemployment->base->attribute('user', $attr); |
---|
1581 | $attrsets{$oattr->iname} = $cval; |
---|
1582 | } |
---|
1583 | } |
---|
1584 | |
---|
1585 | if (keys %attrsets) { |
---|
1586 | if (my $res = $self->set_fields(%attrsets)) { |
---|
1587 | $self->ReportChange('Update', 'Attr %s updated to match Employment %s', join(', ', sort keys %attrsets), $currentemployment->id); |
---|
1588 | return $res; |
---|
1589 | } |
---|
1590 | } else { |
---|
1591 | return 1; |
---|
1592 | } |
---|
1593 | } else { |
---|
1594 | # No current employment, resetting values: |
---|
1595 | |
---|
1596 | return $self->_resetEmployment; |
---|
1597 | } |
---|
1598 | |
---|
1599 | } |
---|
1600 | |
---|
1601 | # Reset attribute value set by employment |
---|
1602 | # except managerContact and expire |
---|
1603 | |
---|
1604 | sub _resetEmployment { |
---|
1605 | my ($self) = @_; |
---|
1606 | |
---|
1607 | $self->computeEmploymentDate; |
---|
1608 | |
---|
1609 | my %changes = ( |
---|
1610 | appliedEmployement => undef, |
---|
1611 | ); |
---|
1612 | |
---|
1613 | my @attributesToReset = _reported_atributes; |
---|
1614 | if (!$self->_get_attributes('_startEmployment')) { |
---|
1615 | push(@attributesToReset, qw(department)); |
---|
1616 | } |
---|
1617 | |
---|
1618 | foreach my $attr (@attributesToReset) { |
---|
1619 | my $default = $self->base->config("unemployment.$attr") || ''; |
---|
1620 | my $old = $self->_get_attributes($attr) || ''; |
---|
1621 | if ($old ne $default) { |
---|
1622 | $changes{$attr} = $default || undef; |
---|
1623 | } |
---|
1624 | } |
---|
1625 | if (%changes) { |
---|
1626 | if ($self->set_fields(%changes)) { |
---|
1627 | $self->base->log(LA_NOTICE, "Updating user %s to match unemployment", $self->id); |
---|
1628 | $self->ReportChange('Update', 'Update %s to match unemployment', join(', ', sort keys %changes)); |
---|
1629 | return 1; |
---|
1630 | } else { |
---|
1631 | return 0; |
---|
1632 | } |
---|
1633 | } else { |
---|
1634 | return 1; |
---|
1635 | } |
---|
1636 | |
---|
1637 | } |
---|
1638 | |
---|
1639 | |
---|
1640 | =head2 computeEmploymentDate |
---|
1641 | |
---|
1642 | Compute and copy to user start and end employment date |
---|
1643 | |
---|
1644 | =cut |
---|
1645 | |
---|
1646 | sub computeEmploymentDate { |
---|
1647 | my ($self) = @_; |
---|
1648 | |
---|
1649 | my $currentemployment = $self->get_attributes('currentEmployment') || ''; |
---|
1650 | |
---|
1651 | my $expire = str2time($self->_get_attributes('expire') || '1970-01-01T00:00:00'); |
---|
1652 | |
---|
1653 | my %changes; |
---|
1654 | my @employmentDate = qw( |
---|
1655 | endEmployment endStrictEmployment endCurrentEmployment endLastEmployment |
---|
1656 | startEmployment startStrictEmployment startCurrentEmployment startFirstEmployment |
---|
1657 | arrival departure |
---|
1658 | ); |
---|
1659 | foreach (@employmentDate) { |
---|
1660 | my $old = $self->_get_attributes($_) || ''; |
---|
1661 | my $new = $self->_get_attributes("_$_") || ''; |
---|
1662 | if ($old ne $new) { |
---|
1663 | $changes{$_} = $new || undef; |
---|
1664 | } |
---|
1665 | } |
---|
1666 | |
---|
1667 | # If there is no current employment we try to find any to not let expire |
---|
1668 | # unset |
---|
1669 | |
---|
1670 | my $expireOn = $self->base->config('expireOn') || ''; |
---|
1671 | if (!grep { $_ eq $expireOn } (@employmentDate, '', 'never')) { |
---|
1672 | $self->base->log(LA_ERR, "expireOn set to invalid parameter %s, using endEmployment instead", $expireOn); |
---|
1673 | $expireOn = undef; |
---|
1674 | } |
---|
1675 | $expireOn ||= 'endEmployment'; |
---|
1676 | |
---|
1677 | # We check if matching start exists to know if using end* for expiration is |
---|
1678 | # safe, even undef |
---|
1679 | my %end2start = ( |
---|
1680 | endEmployment => 'startEmployment', |
---|
1681 | endStrictEmployment => 'startStrictEmployment', |
---|
1682 | endCurrentEmployment => 'startCurrentEmployment', |
---|
1683 | endLastEmployment => 'startFirstEmployment', |
---|
1684 | ); |
---|
1685 | |
---|
1686 | # TODO rework this, working code but bloat |
---|
1687 | if ($expireOn ne 'never') { |
---|
1688 | my $endemploy = ''; |
---|
1689 | if ($self->_get_attributes("_$end2start{$expireOn}")) { |
---|
1690 | $endemploy = $self->_get_attributes("_$expireOn") || ''; |
---|
1691 | $endemploy ||= 'UNCHANGED' unless($currentemployment); |
---|
1692 | } elsif (($self->base->config('unemployed_expire') ||'') eq 'no') { |
---|
1693 | $endemploy = ''; |
---|
1694 | } else { |
---|
1695 | # No expiration date apply, don't touch |
---|
1696 | $endemploy = 'UNCHANGED'; |
---|
1697 | } |
---|
1698 | |
---|
1699 | if ($endemploy ne 'UNCHANGED') { |
---|
1700 | my $nextexpire = str2time( $endemploy || '1970-01-01T00:00:00' ); |
---|
1701 | |
---|
1702 | if ($expire != $nextexpire) { |
---|
1703 | $changes{expire} = $endemploy; |
---|
1704 | } |
---|
1705 | } |
---|
1706 | } |
---|
1707 | |
---|
1708 | if (keys %changes) { |
---|
1709 | $self->base->log(LA_DEBUG, 'Applying employment state to user %s for field %s', $self->id, join(', ', keys %changes)); |
---|
1710 | $self->ReportChange('Update', 'Update %s to match employment', join(', ', sort keys %changes)); |
---|
1711 | if (exists($changes{expire})) { |
---|
1712 | $self->ReportChange('Update', 'Expire update to %s to match employment', ($changes{expire} || '(none)')); |
---|
1713 | $self->base->log(LA_DEBUG, 'New expiration is %s for user %s', ($changes{expire} || '(none)'), $self->id); |
---|
1714 | } |
---|
1715 | $self->set_fields(%changes); |
---|
1716 | } else { |
---|
1717 | $self->base->log(LA_DEBUG, 'No employment change for user %s', $self->id); |
---|
1718 | } |
---|
1719 | |
---|
1720 | $self->_computeEmploymentHistory(); |
---|
1721 | |
---|
1722 | return 1; |
---|
1723 | } |
---|
1724 | |
---|
1725 | sub _computeStartEmployment { |
---|
1726 | my ($self, $delay, $any, $workday) = @_; |
---|
1727 | |
---|
1728 | $delay ||= 0; |
---|
1729 | my $start; |
---|
1730 | my $nstart; |
---|
1731 | |
---|
1732 | if (my $next = $self->_get_attributes('nextEmployment')) { |
---|
1733 | my $onext = $self->base->get_object('employment', $next); |
---|
1734 | $nstart = DateTime->from_epoch(epoch => str2time($onext->_get_attributes('firstday'))); |
---|
1735 | $nstart->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1736 | } |
---|
1737 | |
---|
1738 | my $list_empl = $self->base->db->prepare_cached(q{ |
---|
1739 | SELECT *, (lastday is null or lastday >= now() - '1days'::interval) as "current" FROM employment |
---|
1740 | WHERE "user" = ? and firstday < now() |
---|
1741 | order by firstday desc |
---|
1742 | }); |
---|
1743 | $list_empl->execute($self->id); |
---|
1744 | |
---|
1745 | while (my $res = $list_empl->fetchrow_hashref) { |
---|
1746 | if ($res->{current}) { |
---|
1747 | } elsif ($nstart) { |
---|
1748 | my $prevend = DateTime->from_epoch(epoch => str2time($res->{lastday})); |
---|
1749 | $prevend->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1750 | my $tstart = $nstart->clone; |
---|
1751 | $tstart->subtract(days => $delay + 1); |
---|
1752 | if ($tstart->ymd gt $prevend->ymd) { |
---|
1753 | last; |
---|
1754 | } |
---|
1755 | } elsif ((!$res->{current}) && (!$any)) { |
---|
1756 | last; |
---|
1757 | } |
---|
1758 | $nstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); |
---|
1759 | $nstart->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1760 | $start = $nstart->clone; |
---|
1761 | } |
---|
1762 | $list_empl->finish; |
---|
1763 | |
---|
1764 | $start ||= $nstart if ($any); |
---|
1765 | |
---|
1766 | if ($start) { |
---|
1767 | if ($workday) { |
---|
1768 | my $day_of_week = $start->day_of_week; |
---|
1769 | $start->add(days => |
---|
1770 | $day_of_week == 6 ? 2 : |
---|
1771 | $day_of_week == 7 ? 1 : 0 |
---|
1772 | ); |
---|
1773 | } |
---|
1774 | } |
---|
1775 | |
---|
1776 | return $start ? $start->iso8601 : undef |
---|
1777 | } |
---|
1778 | |
---|
1779 | sub _computeEndEmployment { |
---|
1780 | my ($self, $delay, $any, $workday) = @_; |
---|
1781 | |
---|
1782 | $delay ||= 0; |
---|
1783 | my $end; |
---|
1784 | my $pend; |
---|
1785 | |
---|
1786 | if (my $prev = $self->_get_attributes('prevEmployment')) { |
---|
1787 | my $oprev = $self->base->get_object('employment', $prev); |
---|
1788 | $pend = DateTime->from_epoch(epoch => str2time($oprev->_get_attributes('lastday'))); |
---|
1789 | $pend->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1790 | $pend->add(hours => 23, minutes => 59, seconds => 59); |
---|
1791 | } |
---|
1792 | |
---|
1793 | my $list_empl = $self->base->db->prepare_cached(q{ |
---|
1794 | SELECT *, firstday <= now() as "current" FROM employment WHERE "user" = ? and |
---|
1795 | (lastday is null or lastday >= now() - '1 days'::interval) |
---|
1796 | order by firstday asc |
---|
1797 | }); |
---|
1798 | $list_empl->execute($self->id); |
---|
1799 | while (my $res = $list_empl->fetchrow_hashref) { |
---|
1800 | if (!$res->{lastday}) { |
---|
1801 | # Ultimate employment. |
---|
1802 | $list_empl->finish; |
---|
1803 | return undef; |
---|
1804 | } |
---|
1805 | if ($res->{current}) { |
---|
1806 | } elsif ($end) { |
---|
1807 | my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); |
---|
1808 | $nextstart->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1809 | my $tend = $end->clone; |
---|
1810 | $tend->add(days => $delay + 1); |
---|
1811 | if ($tend->ymd lt $nextstart->ymd) { |
---|
1812 | last; |
---|
1813 | } |
---|
1814 | } elsif ($pend) { |
---|
1815 | my $nextstart = DateTime->from_epoch(epoch => str2time($res->{firstday})); |
---|
1816 | $nextstart->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1817 | my $tend = $pend->clone; |
---|
1818 | $tend->add(days => $delay + 1); |
---|
1819 | if ($tend->ymd lt $nextstart->ymd) { |
---|
1820 | last; |
---|
1821 | } |
---|
1822 | $end = $tend |
---|
1823 | } elsif ((!$res->{current}) && (!$any)) { |
---|
1824 | last; |
---|
1825 | } |
---|
1826 | $end = DateTime->from_epoch(epoch => str2time($res->{lastday})); |
---|
1827 | $end->set_time_zone( DateTime::TimeZone->new( name => 'local' ) ); |
---|
1828 | $end->add(hours => 23, minutes => 59, seconds => 59); |
---|
1829 | } |
---|
1830 | $list_empl->finish; |
---|
1831 | |
---|
1832 | $end ||= $pend if($any); |
---|
1833 | |
---|
1834 | if ($end) { |
---|
1835 | if ($workday) { |
---|
1836 | my $day_of_week = $end->day_of_week; |
---|
1837 | $end->subtract(days => |
---|
1838 | $day_of_week == 7 ? 2 : |
---|
1839 | $day_of_week == 6 ? 1 : 0 |
---|
1840 | ); |
---|
1841 | } |
---|
1842 | } |
---|
1843 | |
---|
1844 | return $end ? $end->iso8601 : undef |
---|
1845 | } |
---|
1846 | |
---|
1847 | # Compute a summary of employement and store the value |
---|
1848 | # into an attribute |
---|
1849 | |
---|
1850 | sub _computeEmploymentHistory { |
---|
1851 | my ($self) = @_; |
---|
1852 | |
---|
1853 | my %changes; |
---|
1854 | |
---|
1855 | { |
---|
1856 | my $sth = $self->db->prepare_cached(q{ |
---|
1857 | select employment.name from employment |
---|
1858 | where "user" = ? and employment.firstday < now() |
---|
1859 | }); |
---|
1860 | $sth->execute($self->id); |
---|
1861 | my @values; |
---|
1862 | while (my $res = $sth->fetchrow_hashref) { |
---|
1863 | push(@values, $res->{name}); |
---|
1864 | } |
---|
1865 | $changes{"employmentHistory"} = \@values; |
---|
1866 | } |
---|
1867 | |
---|
1868 | foreach my $attribute (qw(contratType)) { |
---|
1869 | my $sth = $self->db->prepare_cached(q{ |
---|
1870 | select employment_attributes.value from employment |
---|
1871 | join employment_attributes |
---|
1872 | on employment.ikey = employment_attributes.okey |
---|
1873 | where "user" = ? and employment_attributes.attr = ? |
---|
1874 | and employment.firstday < now() |
---|
1875 | group by employment_attributes.value |
---|
1876 | }); |
---|
1877 | $sth->execute($self->id, $attribute); |
---|
1878 | my @values; |
---|
1879 | while (my $res = $sth->fetchrow_hashref) { |
---|
1880 | push(@values, $res->{value}); |
---|
1881 | } |
---|
1882 | $changes{$attribute . "History"} = \@values; |
---|
1883 | } |
---|
1884 | |
---|
1885 | $self->set_fields(%changes); |
---|
1886 | } |
---|
1887 | |
---|
1888 | =head2 storeBannedPassword($epassword) |
---|
1889 | |
---|
1890 | Add an encrypted password to untrust list |
---|
1891 | |
---|
1892 | =cut |
---|
1893 | |
---|
1894 | sub storeBannedPassword { |
---|
1895 | my ($self, $EncPass) = @_; |
---|
1896 | |
---|
1897 | my @banned = sort { $b cmp $a } $self->_get_attributes('bannedPassword'); |
---|
1898 | my $now = DateTime->now; |
---|
1899 | unshift(@banned, $now->iso8601 . ';' . $EncPass); |
---|
1900 | $self->set_fields('bannedPassword', [ grep { $_ } @banned ]); |
---|
1901 | |
---|
1902 | } |
---|
1903 | |
---|
1904 | =head2 banCurrentPassword |
---|
1905 | |
---|
1906 | Store the current password as banned |
---|
1907 | |
---|
1908 | =cut |
---|
1909 | |
---|
1910 | sub banCurrentPassword { |
---|
1911 | my ($self) = @_; |
---|
1912 | |
---|
1913 | my $old = $self->get_field('userPassword') or return; |
---|
1914 | $self->storeBannedPassword($old); |
---|
1915 | } |
---|
1916 | |
---|
1917 | sub check_password { |
---|
1918 | my ( $self, $password ) = @_; |
---|
1919 | |
---|
1920 | my $res = $self->SUPER::check_password($password); |
---|
1921 | if ($res !~ /^ok$/) { |
---|
1922 | return $res; |
---|
1923 | } |
---|
1924 | |
---|
1925 | foreach my $banned ($self->_get_attributes('bannedPassword')) { |
---|
1926 | my ($date, $oldPassword) = $banned =~ /^([^;]*);(.*)/; |
---|
1927 | if (crypt($password, $oldPassword) eq $oldPassword) { |
---|
1928 | return "Banned password, cannot be used anymore"; |
---|
1929 | } |
---|
1930 | } |
---|
1931 | |
---|
1932 | return 'ok'; |
---|
1933 | } |
---|
1934 | |
---|
1935 | sub _set_password { |
---|
1936 | my ($self, $clear_pass) = @_; |
---|
1937 | if (my $attr = $self->base->attribute($self->type, 'userPassword')) { |
---|
1938 | my $field = $attr->iname; |
---|
1939 | |
---|
1940 | # Storing as old password |
---|
1941 | my @olds = sort { $b cmp $a } $self->_get_attributes('oldPassword'); |
---|
1942 | if (my $old = $self->get_field('userPassword')) { |
---|
1943 | my $now = DateTime->now; |
---|
1944 | unshift(@olds, $now->iso8601 . ';' . $old); |
---|
1945 | $self->set_fields('oldPassword', [ grep { $_ } @olds[0 .. 14] ]); |
---|
1946 | } |
---|
1947 | |
---|
1948 | my @salt_char = (('a' .. 'z'), ('A' .. 'Z'), (0 .. 9), '/', '.'); |
---|
1949 | my $salt = join('', map { $salt_char[rand(scalar(@salt_char))] } (1 .. 8)); |
---|
1950 | my $res = $self->set_fields($field, crypt($clear_pass, '$1$' . $salt)); |
---|
1951 | if ($res) { |
---|
1952 | if ($self->base->get_global_value('rsa_public_key')) { |
---|
1953 | $self->setCryptPassword($clear_pass) or return; |
---|
1954 | } |
---|
1955 | } |
---|
1956 | |
---|
1957 | $self->set_fields('passwordLastSet', DateTime->now->datetime); |
---|
1958 | $self->base->log(LA_NOTICE, |
---|
1959 | 'Mot de passe changé pour %s', |
---|
1960 | $self->id |
---|
1961 | ); |
---|
1962 | |
---|
1963 | |
---|
1964 | return $res; |
---|
1965 | } else { |
---|
1966 | $self->log(LA_WARN, |
---|
1967 | "Cannot set password: userPassword attributes is unsupported"); |
---|
1968 | } |
---|
1969 | } |
---|
1970 | |
---|
1971 | =head2 setCryptPassword($clear_pass) |
---|
1972 | |
---|
1973 | Store password encrypted using RSA encryption. |
---|
1974 | |
---|
1975 | =cut |
---|
1976 | |
---|
1977 | sub setCryptPassword { |
---|
1978 | my ($self, $clear_pass) = @_; |
---|
1979 | if (my $serialize = $self->base->get_global_value('rsa_public_key')) { |
---|
1980 | my $public = Crypt::RSA::Key::Public->new; |
---|
1981 | $public = $public->deserialize(String => [ $serialize ]); |
---|
1982 | my $rsa = new Crypt::RSA ES => 'PKCS1v15'; |
---|
1983 | my $rsa_password = $rsa->encrypt ( |
---|
1984 | Message => $clear_pass, |
---|
1985 | Key => $public, |
---|
1986 | Armour => 1, |
---|
1987 | ) || die $rsa->errstr(); |
---|
1988 | if (!$self->_set_c_fields('encryptedPassword', $rsa_password)) { |
---|
1989 | $self->log(LA_ERR, |
---|
1990 | "Cannot set 'encryptedPassword' attribute for object %s/%s", |
---|
1991 | $self->type, $self->id, |
---|
1992 | ); |
---|
1993 | return; |
---|
1994 | } |
---|
1995 | } |
---|
1996 | $self->ReportChange('Password', 'Password stored using internal key'); |
---|
1997 | return 1; |
---|
1998 | } |
---|
1999 | |
---|
2000 | =head2 _InjectCryptPasswd($cryptpasswd) |
---|
2001 | |
---|
2002 | Inject a password encrypted using standard UNIX method. |
---|
2003 | |
---|
2004 | The passwrod will be used to authenticate user inside the application but it |
---|
2005 | will not be transmit to any other database. |
---|
2006 | |
---|
2007 | =cut |
---|
2008 | |
---|
2009 | sub _InjectCryptPasswd { |
---|
2010 | my ($self, $cryptpasswd) = @_; |
---|
2011 | |
---|
2012 | if (my $current = $self->get_field('userPassword')) { |
---|
2013 | if ($cryptpasswd eq $current) { |
---|
2014 | return 1; |
---|
2015 | } |
---|
2016 | } |
---|
2017 | my $res = $self->set_fields('userPassword', $cryptpasswd); |
---|
2018 | |
---|
2019 | if ($res) { |
---|
2020 | $self->base->log(LA_NOTICE, 'Crypted password injected for %s', $self->id); |
---|
2021 | return 1; |
---|
2022 | } else { |
---|
2023 | $self->base->log(LA_ERR, 'Cannot inject crypted password for %s', $self->id); |
---|
2024 | return 0; |
---|
2025 | } |
---|
2026 | } |
---|
2027 | |
---|
2028 | =head2 GenPasswordResetId |
---|
2029 | |
---|
2030 | Return a new id allowing passowrd reset |
---|
2031 | |
---|
2032 | =cut |
---|
2033 | |
---|
2034 | sub GenPasswordResetId { |
---|
2035 | my ($self) = @_; |
---|
2036 | |
---|
2037 | my $id = LATMOS::Accounts::Utils::genpassword(length => 32); |
---|
2038 | |
---|
2039 | my $sth = $self->base->db->prepare_cached(q{ |
---|
2040 | INSERT INTO passwordreset (id, "user") values (?,?) |
---|
2041 | }); |
---|
2042 | |
---|
2043 | if ($sth->execute($id, $self->id)) { |
---|
2044 | return $id; |
---|
2045 | } else { |
---|
2046 | return; |
---|
2047 | } |
---|
2048 | } |
---|
2049 | |
---|
2050 | =head2 SendPasswordReset($url) |
---|
2051 | |
---|
2052 | Generate a password reset Id and the to the user. |
---|
2053 | |
---|
2054 | C<$url> is the URL where the password can changed (printf forward, the %s is |
---|
2055 | replaced by the request id) |
---|
2056 | |
---|
2057 | =cut |
---|
2058 | |
---|
2059 | sub SendPasswordReset { |
---|
2060 | my ($self, $url) = @_; |
---|
2061 | |
---|
2062 | my $id = $self->GenPasswordResetId; |
---|
2063 | |
---|
2064 | my $mail = $self->_get_attributes('mail') or do { |
---|
2065 | $self->base->log(LA_ERR, "Cannot sent reset password mail: no mail found"); |
---|
2066 | return; |
---|
2067 | }; |
---|
2068 | |
---|
2069 | my $MailSubject = $self->la->val('_default_', 'mailSubject', 'LATMOS::Accounts'); |
---|
2070 | |
---|
2071 | my %mail = ( |
---|
2072 | Subject => "$MailSubject: pasword reset", |
---|
2073 | 'X-LATMOS-Reason' => 'Password Reset', |
---|
2074 | to => $mail, |
---|
2075 | ); |
---|
2076 | |
---|
2077 | if (my $otherEmail = $self->_get_attributes('otherEmail')) { |
---|
2078 | $mail{cc} = $otherEmail; |
---|
2079 | } |
---|
2080 | |
---|
2081 | my $vars = { |
---|
2082 | url => sprintf($url, $id), |
---|
2083 | }; |
---|
2084 | $vars->{id} = $id; |
---|
2085 | $vars->{obj} = $self; |
---|
2086 | |
---|
2087 | my $lamail = LATMOS::Accounts::Mail->new( |
---|
2088 | $self->base->la, |
---|
2089 | 'passwordreset.mail', |
---|
2090 | ); |
---|
2091 | |
---|
2092 | if ($lamail->process(\%mail, $vars)) { |
---|
2093 | $self->base->log(LA_NOTICE, |
---|
2094 | "Reset password sent to %s for user %s", |
---|
2095 | $mail{to}, |
---|
2096 | $self->id, |
---|
2097 | ); |
---|
2098 | return 1; |
---|
2099 | } else { |
---|
2100 | return; |
---|
2101 | } |
---|
2102 | |
---|
2103 | } |
---|
2104 | |
---|
2105 | =head2 CheckPasswordResetId($id) |
---|
2106 | |
---|
2107 | Return True if the reset password ID can be found and is less than one day old |
---|
2108 | |
---|
2109 | =cut |
---|
2110 | |
---|
2111 | sub CheckPasswordResetId { |
---|
2112 | my ($self, $id) = @_; |
---|
2113 | |
---|
2114 | my $sth = $self->base->db->prepare_cached(q{ |
---|
2115 | SELECT * FROM passwordreset WHERE |
---|
2116 | "user" = ? and |
---|
2117 | id = ? and |
---|
2118 | "create" >= now() - '1 days'::interval |
---|
2119 | }); |
---|
2120 | $sth->execute($self->id, $id); |
---|
2121 | |
---|
2122 | my $res = $sth->fetchrow_hashref; |
---|
2123 | $sth->finish; |
---|
2124 | |
---|
2125 | return $res ? 1 : 0; |
---|
2126 | } |
---|
2127 | |
---|
2128 | =head2 DeletePasswordId($id) |
---|
2129 | |
---|
2130 | Delete password reset C<$id> and all expired request |
---|
2131 | |
---|
2132 | =cut |
---|
2133 | |
---|
2134 | sub DeletePasswordId { |
---|
2135 | my ($self, $id) = @_; |
---|
2136 | |
---|
2137 | my $sth = $self->base->db->prepare_cached(q{ |
---|
2138 | DELETE FROM passwordreset WHERE |
---|
2139 | "user" = ? AND (id = ? or "create" < now() - '1 days'::interval) |
---|
2140 | }); |
---|
2141 | |
---|
2142 | $sth->execute($self->id, $id); |
---|
2143 | } |
---|
2144 | |
---|
2145 | 1; |
---|
2146 | |
---|
2147 | __END__ |
---|
2148 | |
---|
2149 | =head1 SEE ALSO |
---|
2150 | |
---|
2151 | =head1 AUTHOR |
---|
2152 | |
---|
2153 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
2154 | |
---|
2155 | =head1 COPYRIGHT AND LICENSE |
---|
2156 | |
---|
2157 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
2158 | |
---|
2159 | This library is free software; you can redistribute it and/or modify |
---|
2160 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
2161 | at your option, any later version of Perl 5 you may have available. |
---|
2162 | |
---|
2163 | =cut |
---|