1 | package LATMOS::Accounts::Bases::Sql::objects; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Objects); |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use Crypt::RSA; |
---|
10 | use Crypt::RSA::Key::Public::SSH; |
---|
11 | use DateTime; |
---|
12 | use LATMOS::Accounts::I18N; |
---|
13 | |
---|
14 | our $VERSION = (q$Rev: 2105 $ =~ /^Rev: (\d+) /)[0]; |
---|
15 | |
---|
16 | =head1 NAME |
---|
17 | |
---|
18 | LATMOS::Accounts::Bases::Sql::objects - Parent class for SQL object |
---|
19 | |
---|
20 | =cut |
---|
21 | |
---|
22 | # Write extend attribute table |
---|
23 | sub _object_table_attributes { $_[0]->_object_table . '_attributes' } |
---|
24 | # Read search table (with compute attribute) |
---|
25 | sub _object_table_attributes_read { $_[0]->_object_table . '_attributes' } |
---|
26 | |
---|
27 | sub listReal { |
---|
28 | my ($class, $base) = @_; |
---|
29 | |
---|
30 | my $sth = $base->db->prepare_cached( |
---|
31 | sprintf( |
---|
32 | q{select %s as k from %s where internobject = false |
---|
33 | and oalias IS NULL %s order by %s}, |
---|
34 | $base->db->quote_identifier($class->_key_field), |
---|
35 | $base->db->quote_identifier($class->_object_table), |
---|
36 | ($base->{wexported} ? '' : 'and exported = true'), |
---|
37 | $base->db->quote_identifier($class->_key_field), |
---|
38 | ) |
---|
39 | ); |
---|
40 | $sth->execute; |
---|
41 | my @keys; |
---|
42 | while(my $res = $sth->fetchrow_hashref) { |
---|
43 | push(@keys, $res->{k}); |
---|
44 | } |
---|
45 | @keys |
---|
46 | } |
---|
47 | |
---|
48 | sub list { |
---|
49 | my ($class, $base) = @_; |
---|
50 | |
---|
51 | my $sth = $base->db->prepare_cached( |
---|
52 | sprintf( |
---|
53 | q{select %s as k from %s where internobject = false %s order by %s}, |
---|
54 | $base->db->quote_identifier($class->_key_field), |
---|
55 | $base->db->quote_identifier($class->_object_table), |
---|
56 | ($base->{wexported} ? '' : 'and exported = true'), |
---|
57 | $base->db->quote_identifier($class->_key_field), |
---|
58 | ) |
---|
59 | ); |
---|
60 | $sth->execute; |
---|
61 | my @keys; |
---|
62 | while(my $res = $sth->fetchrow_hashref) { |
---|
63 | push(@keys, $res->{k}); |
---|
64 | } |
---|
65 | @keys |
---|
66 | } |
---|
67 | |
---|
68 | sub list_from_rev { |
---|
69 | my ($class, $base, $rev) = @_; |
---|
70 | my $sth = $base->db->prepare_cached( |
---|
71 | sprintf( |
---|
72 | q{select %s as k from %s where rev > ? and internobject = false |
---|
73 | and oalias IS NULL %s order by %s}, |
---|
74 | $base->db->quote_identifier($class->_key_field), |
---|
75 | $base->db->quote_identifier($class->_object_table), |
---|
76 | ($base->{wexported} ? '' : 'and exported = true'), |
---|
77 | $base->db->quote_identifier($class->_key_field), |
---|
78 | ) |
---|
79 | ); |
---|
80 | $sth->execute($rev); |
---|
81 | my @keys; |
---|
82 | while(my $res = $sth->fetchrow_hashref) { |
---|
83 | push(@keys, $res->{k}); |
---|
84 | } |
---|
85 | @keys |
---|
86 | } |
---|
87 | |
---|
88 | sub _has_extended_attributes { 0 } |
---|
89 | |
---|
90 | sub _get_attr_schema { |
---|
91 | my ($class, $base, $info) = @_; |
---|
92 | $info ||= {}; |
---|
93 | if (!$base->{__cache}{$class->_object_table}{inline}) { |
---|
94 | $base->{__cache}{$class->_object_table}{inline} = []; |
---|
95 | my $sth = $base->db->prepare( |
---|
96 | q{SELECT column_name FROM information_schema.columns |
---|
97 | WHERE table_name = ?} |
---|
98 | ); |
---|
99 | $sth->execute($class->_object_table); |
---|
100 | while (my $res = $sth->fetchrow_hashref) { |
---|
101 | push(@{$base->{__cache}{$class->_object_table}{inline}}, |
---|
102 | $res->{column_name}); |
---|
103 | } |
---|
104 | } |
---|
105 | foreach (@{$base->{__cache}{$class->_object_table}{inline}}) { |
---|
106 | $info->{$_}{inline} = 1; |
---|
107 | if (m/^(rev|date|create|ikey)$/) { |
---|
108 | $info->{$_}{ro} = 1 |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | # Common to all object attribute: |
---|
113 | my %commons = ( |
---|
114 | name => { |
---|
115 | inline => 1, |
---|
116 | ro => 1, |
---|
117 | label => l('Name'), |
---|
118 | }, |
---|
119 | create => { |
---|
120 | inline => 1, |
---|
121 | ro => 1, |
---|
122 | label => l('Created'), |
---|
123 | }, |
---|
124 | date => { |
---|
125 | inline => 1, |
---|
126 | ro => 1, |
---|
127 | label => l('Last modified'), |
---|
128 | }, |
---|
129 | exported => { inline => 1, formtype => 'CHECKBOX', hide => 1, monitored => 1 }, |
---|
130 | unexported => { |
---|
131 | inline => 1, |
---|
132 | managed => 1, |
---|
133 | formtype => 'CHECKBOX', |
---|
134 | get => sub { |
---|
135 | my ($self) = @_; |
---|
136 | return $self->object->get_field('exported') ? undef : 1; |
---|
137 | }, |
---|
138 | set => sub { |
---|
139 | my ($self, $data) = @_; |
---|
140 | $self->object->_set_c_fields('exported', $data ? 0 : 1); |
---|
141 | }, |
---|
142 | label => l('Hidden'), |
---|
143 | }, |
---|
144 | services => { |
---|
145 | managed => 1, |
---|
146 | multiple => 1, |
---|
147 | reference => 'service', |
---|
148 | label => l('Service'), |
---|
149 | }, |
---|
150 | modifiedby => { |
---|
151 | inline => 1, |
---|
152 | reference => 'user', |
---|
153 | ro => 1, |
---|
154 | label => l('Modified by'), |
---|
155 | }, |
---|
156 | createdby => { |
---|
157 | inline => 1, |
---|
158 | reference => 'user', |
---|
159 | ro => 1, |
---|
160 | label => l('Created by'), |
---|
161 | }, |
---|
162 | oalias => { |
---|
163 | inline => 1, |
---|
164 | reference => $class->type, |
---|
165 | label => l('Object alias for'), |
---|
166 | post => sub { |
---|
167 | my ($self, $value) = @_; |
---|
168 | $self->object->_update_aliases_ptr(); |
---|
169 | $self->object->_update_aliases_cache(); |
---|
170 | }, |
---|
171 | checkinput => sub { |
---|
172 | my ($oalias) = @_; |
---|
173 | my $otype = $class->type; |
---|
174 | $oalias ||= ''; |
---|
175 | |
---|
176 | if (my ($aliasotype, $aliasoname, $aliasattr) = $oalias =~ m/^([^\/]+)\.([^\.]+)\.(.*)$/) { |
---|
177 | my $attribute = $base->attribute($aliasotype, $aliasattr) or do { |
---|
178 | $base->log(LA_DEBUG, "Oalias %s (%s): can fetch attibute %s/%s", |
---|
179 | $otype, $oalias, $aliasotype, $aliasattr); |
---|
180 | return; |
---|
181 | }; |
---|
182 | my $refotype = $attribute->reference or do { |
---|
183 | $base->log(LA_DEBUG, "Oalias %s (%s): Attribute does not reference an object", |
---|
184 | $otype, $oalias); |
---|
185 | return; |
---|
186 | }; |
---|
187 | |
---|
188 | if ($attribute->multiple) { |
---|
189 | $base->log(LA_DEBUG, "Oalias %s (%s): Attribute must not be multiple", |
---|
190 | $otype, $oalias); |
---|
191 | return; |
---|
192 | }; |
---|
193 | } elsif(!$base->get_object($otype, $oalias)) { |
---|
194 | $base->log(LA_DEBUG, "Cannot get object $otype/$oalias"); |
---|
195 | return; |
---|
196 | } |
---|
197 | return 1; |
---|
198 | }, |
---|
199 | }, |
---|
200 | oaliascache => { |
---|
201 | inline => 1, |
---|
202 | reference => $class->type, |
---|
203 | label => l('Cache for oalias data'), |
---|
204 | hide => 1, |
---|
205 | }, |
---|
206 | internobject => { |
---|
207 | inline => 1, |
---|
208 | label => l('Internal object'), |
---|
209 | hide => 1, |
---|
210 | }, |
---|
211 | nodelete => { |
---|
212 | inline => 1, |
---|
213 | label => l('Deletion protection'), |
---|
214 | hide => 1, |
---|
215 | }, |
---|
216 | setReftoMe => { |
---|
217 | multiple => 1, |
---|
218 | managed => 1, |
---|
219 | delayed => 1, |
---|
220 | set => sub { |
---|
221 | my ($self, $data) = @_; |
---|
222 | my $count = 0; |
---|
223 | foreach (ref $data ? @{$data} : $data) { |
---|
224 | my ($otype, $oname, $attribute) = $_ =~ m/^([^\.]+)\.(.+)\.([^\.]+)$/ or next; |
---|
225 | my $object = $base->get_object($otype, $oname) or next; |
---|
226 | my $attr = $base->attribute($otype, $attribute) or next; |
---|
227 | if ($attr->reference ne $self->object->type) { |
---|
228 | $base->log(LA_ERR, "Cannot set $_ pointing to " . $self->object->id . ": attribute does not reference " . $self->object->type); |
---|
229 | return; |
---|
230 | } |
---|
231 | if ($attr->multiple) { |
---|
232 | my @values = $object->get_attributes($attribute); |
---|
233 | $count += $object->set_c_fields($attribute => [ @values, $self->object->id ]); |
---|
234 | } else { |
---|
235 | $count += $object->set_c_fields($attribute => $self->object->id); |
---|
236 | } |
---|
237 | } |
---|
238 | return $count; |
---|
239 | }, |
---|
240 | label => l('Set me as reference'), |
---|
241 | }, |
---|
242 | log => { |
---|
243 | multiple => 1, |
---|
244 | managed => 1, |
---|
245 | ro => 1, |
---|
246 | hide => 1, |
---|
247 | get => sub { |
---|
248 | my ($self) = @_; |
---|
249 | return [ |
---|
250 | map { sprintf( |
---|
251 | "%s (%d), %s: %s/%s (%d) %s", |
---|
252 | $_->{logdate}, |
---|
253 | $_->{irev} || -1, |
---|
254 | $_->{username}, |
---|
255 | $_->{otype}, |
---|
256 | $_->{name}, |
---|
257 | $_->{ikey}, |
---|
258 | $_->{message} |
---|
259 | ) } $self->object->base->getobjectlogs( |
---|
260 | $self->object->type, |
---|
261 | $self->object->id, |
---|
262 | ) ]; |
---|
263 | }, |
---|
264 | }, |
---|
265 | ); |
---|
266 | |
---|
267 | # Merging / overriding with common to all object attributes properties |
---|
268 | foreach my $attr (keys %commons) { |
---|
269 | foreach my $var (keys %{ $commons{$attr} }) { |
---|
270 | $info->{$attr}{$var} = $commons{$attr}{$var}; |
---|
271 | } |
---|
272 | } |
---|
273 | |
---|
274 | $info |
---|
275 | } |
---|
276 | |
---|
277 | # Everything managed by the perl code |
---|
278 | |
---|
279 | sub _managed_fields { |
---|
280 | my ($class, $for, $base) = @_; |
---|
281 | return(); |
---|
282 | } |
---|
283 | |
---|
284 | sub new { |
---|
285 | my ($class, $base, $id) = @_; |
---|
286 | |
---|
287 | my $__cache = $base->{__cache}{"_" . $class->type}; |
---|
288 | |
---|
289 | if (!(exists($__cache->{$id}) |
---|
290 | && $__cache->{$id}{__time} >= time - 1)) { |
---|
291 | |
---|
292 | my $sth = $base->db->prepare_cached( |
---|
293 | sprintf(q{ select 1 from %s where %s = ? %s}, |
---|
294 | $base->db->quote_identifier($class->_object_table), |
---|
295 | $base->db->quote_identifier($class->_key_field), |
---|
296 | ($base->{wexported} ? '' : 'and exported = true'), |
---|
297 | ), |
---|
298 | ); |
---|
299 | my $count = $sth->execute($id); |
---|
300 | $sth->finish; |
---|
301 | ($count || 0) == 1 or return; |
---|
302 | } |
---|
303 | $class->SUPER::new($base, $id); |
---|
304 | } |
---|
305 | |
---|
306 | sub Iid { $_[0]->_get_ikey } |
---|
307 | |
---|
308 | =head2 ParentObject |
---|
309 | |
---|
310 | When object is part of another return the parent object, return undef otherwise. |
---|
311 | |
---|
312 | =cut |
---|
313 | |
---|
314 | sub ParentObject { } |
---|
315 | |
---|
316 | sub _get_ikey { |
---|
317 | my ($class, $base, $id) = @_; |
---|
318 | $base ||= $class->base; |
---|
319 | $id ||= $class->id; |
---|
320 | my $sth = $base->db->prepare_cached( |
---|
321 | sprintf( |
---|
322 | q{select ikey from %s where %s = ?}, |
---|
323 | $base->db->quote_identifier($class->_object_table), |
---|
324 | $base->db->quote_identifier($class->_key_field), |
---|
325 | ) |
---|
326 | ); |
---|
327 | $sth->execute($id); |
---|
328 | my $res = $sth->fetchrow_hashref; |
---|
329 | $sth->finish; |
---|
330 | $res->{ikey} |
---|
331 | } |
---|
332 | |
---|
333 | sub _create { |
---|
334 | my ($class, $base, $id, %data) = @_; |
---|
335 | |
---|
336 | # splitting inline from extended |
---|
337 | my (%first, %second); |
---|
338 | # Ensure object is exported if not specified |
---|
339 | $data{exported} = 1 if (!exists($data{exported})); |
---|
340 | if (exists $data{unexported}) { |
---|
341 | $data{exported} = $data{unexported} ? 0 : 1; |
---|
342 | delete $data{unexported} |
---|
343 | } |
---|
344 | foreach (keys %data) { |
---|
345 | my $attr = $base->attribute($class->type, $_) or next; |
---|
346 | $_ =~ /^exported$/ and $data{$_} = $data{$_} ? 1 : 0; |
---|
347 | my $formatted = ref($data{$_}) |
---|
348 | ? [ map { $attr->input($_) } @{ $data{$_} } ] |
---|
349 | : $attr->input($data{$_}); |
---|
350 | if ($attr->{inline} && ! $attr->{delayed}) { |
---|
351 | my $iname = $attr->iname; |
---|
352 | $first{$iname} = $formatted; |
---|
353 | } else { |
---|
354 | # the real internal name will be translate by _set_c_field |
---|
355 | $second{$_} = $formatted if(defined($formatted)); |
---|
356 | } |
---|
357 | } |
---|
358 | $first{$class->_key_field} = $id; |
---|
359 | $first{createdby} = $base->user || '@Console'; |
---|
360 | $first{modifiedby} = $base->user || '@Console'; |
---|
361 | |
---|
362 | my $lastid; |
---|
363 | { |
---|
364 | my $sthnextval = $base->db->prepare_cached("select nextval('ikey_seq') as c"); |
---|
365 | $sthnextval->execute; |
---|
366 | $lastid = $sthnextval->fetchrow_hashref()->{c}; |
---|
367 | $first{ikey} = $lastid; |
---|
368 | $sthnextval->finish; |
---|
369 | } |
---|
370 | |
---|
371 | my $sth = $base->db->prepare( |
---|
372 | sprintf( |
---|
373 | q{insert into %s (%s) values (%s)}, |
---|
374 | $base->db->quote_identifier($class->_object_table), |
---|
375 | join(', ', map { $base->db->quote_identifier($_) } sort keys %first), |
---|
376 | join(',', qw(?) x scalar(keys %first)), |
---|
377 | ) |
---|
378 | ); |
---|
379 | $sth->execute(map { defined($first{$_}) ? $first{$_} : undef } sort keys %first) or return; |
---|
380 | |
---|
381 | my $sthid = $base->db->prepare_cached( |
---|
382 | sprintf(q{select %s as k from %s where ikey = ?}, |
---|
383 | $base->db->quote_identifier($class->_key_field), |
---|
384 | $base->db->quote_identifier($class->_object_table), |
---|
385 | ) |
---|
386 | ); |
---|
387 | $sthid->execute($lastid); |
---|
388 | my $res = $sthid->fetchrow_hashref(); |
---|
389 | $sthid->finish; |
---|
390 | $res or do { |
---|
391 | $base->log(LA_DEBUG, 'Cannot retrieve SQL row from freshly create object %s/%s', $class->type, $id); |
---|
392 | return; |
---|
393 | }; |
---|
394 | |
---|
395 | my $obj = $class->new($base, $res->{k}) or return; |
---|
396 | if (keys %second) { |
---|
397 | $obj->_set_c_fields(%second) or do { |
---|
398 | $base->log(LA_DEBUG, 'Cannot set attributes to freshly create object %s/%s', $class->type, $id); |
---|
399 | return; |
---|
400 | }; |
---|
401 | } |
---|
402 | |
---|
403 | my @parents = $obj->ParentObject; |
---|
404 | |
---|
405 | foreach my $pobj (@parents) { |
---|
406 | $pobj->PostSetAttribute; |
---|
407 | } |
---|
408 | |
---|
409 | return $res->{k}; |
---|
410 | } |
---|
411 | |
---|
412 | =head2 refreshRev |
---|
413 | |
---|
414 | Increase revision of the object to force synchronisation |
---|
415 | |
---|
416 | =cut |
---|
417 | |
---|
418 | sub refreshRev { |
---|
419 | my ($self) = @_; |
---|
420 | |
---|
421 | my $sth = $self->db->prepare_cached( |
---|
422 | sprintf(q{ |
---|
423 | UPDATE %s SET rev = nextval('revisions_rev_seq'::regclass) WHERE %s = ? |
---|
424 | }, |
---|
425 | $self->db->quote_identifier($self->_object_table), |
---|
426 | $self->db->quote_identifier($self->_key_field), |
---|
427 | ) |
---|
428 | ); |
---|
429 | $sth->execute($self->id); |
---|
430 | } |
---|
431 | |
---|
432 | =head2 CreateAlias($base, $name, $for) |
---|
433 | |
---|
434 | Create an alias named C<$name> with pointing to C<$for> |
---|
435 | |
---|
436 | =cut |
---|
437 | |
---|
438 | sub CreateAlias { |
---|
439 | my ($class, $base, $name, $for) = @_; |
---|
440 | |
---|
441 | $base->log(LA_ERR, '%s does not support alias object, alias %s not created', $class->type, $name); |
---|
442 | } |
---|
443 | |
---|
444 | sub _delete { |
---|
445 | my ($class, $base, $id) = @_; |
---|
446 | |
---|
447 | my $__cache = $base->{__cache}{"_" . $class->type}; |
---|
448 | |
---|
449 | my $obj = $base->get_object($class->type, $id) |
---|
450 | or return; |
---|
451 | |
---|
452 | if ($obj->_get_attributes('internobject')) { |
---|
453 | # Cannot happend: internal are not fetchable |
---|
454 | $base->log(LA_ERR,'Cannot delete %s/%s: is an internal object', $class->type, $id); |
---|
455 | return; |
---|
456 | } |
---|
457 | if ($obj->_get_attributes('nodelete')) { |
---|
458 | $base->log(LA_ERR,'Cannot delete %s/%s: is write protected', $class->type, $id); |
---|
459 | return; |
---|
460 | } |
---|
461 | |
---|
462 | my @parents = $obj->ParentObject; |
---|
463 | |
---|
464 | my $sthd = $base->db->prepare_cached( |
---|
465 | sprintf( |
---|
466 | q{delete from %s where %s = ?}, |
---|
467 | $base->db->quote_identifier($class->_object_table), |
---|
468 | $base->db->quote_identifier($class->_key_field), |
---|
469 | ) |
---|
470 | ); |
---|
471 | my $res = $sthd->execute($id); |
---|
472 | if ($res) { |
---|
473 | delete($__cache->{$id}); |
---|
474 | } |
---|
475 | |
---|
476 | foreach my $pobj (@parents) { |
---|
477 | $res += $pobj->PostSetAttribute; |
---|
478 | } |
---|
479 | |
---|
480 | $res |
---|
481 | } |
---|
482 | |
---|
483 | sub _rename { |
---|
484 | my ($class, $base, $id, $newid) = @_; |
---|
485 | |
---|
486 | my $sthr = $base->db->prepare_cached( |
---|
487 | sprintf( |
---|
488 | q{update %s set %s = ? where %s = ?}, |
---|
489 | $base->db->quote_identifier($class->_object_table), |
---|
490 | $base->db->quote_identifier($class->_key_field), |
---|
491 | $base->db->quote_identifier($class->_key_field), |
---|
492 | ) |
---|
493 | ); |
---|
494 | |
---|
495 | if (($sthr->execute($newid, $id) || 0) != 1) { |
---|
496 | $base->log(LA_ERR, "Erreur renaming %s %s to %s", |
---|
497 | $class->type, |
---|
498 | $id, $newid, |
---|
499 | ); |
---|
500 | return; |
---|
501 | } |
---|
502 | |
---|
503 | 1; |
---|
504 | } |
---|
505 | |
---|
506 | =head2 db |
---|
507 | |
---|
508 | Return reference to L<DBI> object. |
---|
509 | |
---|
510 | =cut |
---|
511 | |
---|
512 | sub db { |
---|
513 | return $_[0]->base->db; |
---|
514 | } |
---|
515 | |
---|
516 | sub _quote_object_table { |
---|
517 | my ($self) = @_; |
---|
518 | my $table = $self->_object_table or return; |
---|
519 | $self->db->quote_identifier($table); |
---|
520 | } |
---|
521 | sub _quote_key_field { |
---|
522 | my ($self) = @_; |
---|
523 | my $key_field = $self->_key_field or return; |
---|
524 | $self->db->quote_identifier($key_field); |
---|
525 | } |
---|
526 | |
---|
527 | sub get_field { |
---|
528 | my ($self, $field) = @_; |
---|
529 | if ($field eq 'services') { |
---|
530 | my @services; |
---|
531 | my $sth = $self->db->prepare_cached( |
---|
532 | q{ select name from service join service_attributes |
---|
533 | on okey = ikey |
---|
534 | where service_attributes.attr = 'dependOn' and value = ? |
---|
535 | }); |
---|
536 | $sth->execute($self->type . '.' . $self->id); |
---|
537 | while(my $res = $sth->fetchrow_hashref) { |
---|
538 | push(@services, $res->{name}); |
---|
539 | } |
---|
540 | return \@services; |
---|
541 | } |
---|
542 | my $attr = $self->attribute($field) or return; |
---|
543 | if ($attr->{inline}) { |
---|
544 | my $sth = $self->db->prepare_cached( |
---|
545 | sprintf( |
---|
546 | q{select %s from %s where %s = ?}, |
---|
547 | $self->db->quote_identifier(lc($field)), |
---|
548 | $self->_quote_object_table, |
---|
549 | $self->_quote_key_field, |
---|
550 | ) |
---|
551 | ); |
---|
552 | $sth->execute($self->id); |
---|
553 | my $res = $sth->fetchrow_hashref or $self->db->rollback; |
---|
554 | $sth->finish; |
---|
555 | return $res->{$field}; |
---|
556 | } elsif ($self->_has_extended_attributes) { # else, then we mandatory have extend attr |
---|
557 | $self->base->{__cache}{"_" . $self->type} ||= {}; |
---|
558 | my $__cache = $self->base->{__cache}{"_" . $self->type}; |
---|
559 | if (!(exists($__cache->{$self->id}) |
---|
560 | && $__cache->{$self->id}{__time} >= time - 1)) { |
---|
561 | my $sth = $self->db->prepare_cached( |
---|
562 | sprintf( |
---|
563 | q{ |
---|
564 | select attr, value from %s |
---|
565 | join %s on okey = ikey |
---|
566 | where %s = ? |
---|
567 | }, |
---|
568 | $self->db->quote_identifier($self->_object_table_attributes_read), |
---|
569 | $self->db->quote_identifier($self->_object_table), |
---|
570 | $self->db->quote_identifier($self->_key_field), |
---|
571 | ) |
---|
572 | ); |
---|
573 | $sth->execute($self->id); |
---|
574 | delete($__cache->{$self->id}); |
---|
575 | $__cache->{$self->id}{__time} = time; |
---|
576 | while(my $res = $sth->fetchrow_hashref) { |
---|
577 | push(@{$__cache->{$self->id}{$res->{attr}}}, $res->{value}); |
---|
578 | } |
---|
579 | #return @values > 1 ? \@values : $values[0]; |
---|
580 | } |
---|
581 | my $val = $__cache->{$self->id}{$field}; |
---|
582 | return @{$val || []} > 1 ? $val : $val->[0]; |
---|
583 | } |
---|
584 | } |
---|
585 | |
---|
586 | sub GetAttributeValue { |
---|
587 | my ($self, $cfield) = @_; |
---|
588 | |
---|
589 | my $res = $self->SUPER::GetAttributeValue($cfield) or return; |
---|
590 | |
---|
591 | my $attribute = $self->attribute($cfield) or do { |
---|
592 | $self->base->log(LA_WARN, "Unknow attribute $cfield"); |
---|
593 | return; |
---|
594 | }; |
---|
595 | |
---|
596 | if (my $ref = $attribute->reference) { |
---|
597 | my @deref; |
---|
598 | foreach my $v (ref $res ? @{ $res } : $res) { |
---|
599 | my $derefobj = $self->base->_derefObject($ref, $v); |
---|
600 | push(@deref, $derefobj->id) if ($derefobj); |
---|
601 | } |
---|
602 | return scalar(@deref) > 1 ? \@deref : $deref[0]; |
---|
603 | } else { |
---|
604 | return $res; |
---|
605 | } |
---|
606 | } |
---|
607 | |
---|
608 | sub set_fields { |
---|
609 | my ($self, %data) = @_; |
---|
610 | my @updated_attributes = (); |
---|
611 | my @fields; |
---|
612 | my @vals; |
---|
613 | my %ext; |
---|
614 | if (exists($data{services})) { |
---|
615 | my %old = map { $_ => 0 } $self->get_attributes('services'); |
---|
616 | foreach my $serv (grep { $_ } ref $data{services} ? @{ $data{services} } : $data{services}) { |
---|
617 | if (!exists($old{$serv})) { |
---|
618 | my $oserv = $self->base->get_object('service', $serv) or next; |
---|
619 | $oserv->addAttributeValue('dependOn', $self->type . '.' . $self->id); |
---|
620 | } |
---|
621 | $old{$serv} = 1; |
---|
622 | } |
---|
623 | foreach my $serv (keys %old) { |
---|
624 | if (!$old{$serv}) { |
---|
625 | my $oserv = $self->base->get_object('service', $serv) or next; |
---|
626 | $oserv->delAttributeValue('dependOn', $self->type . '.' . $self->id); |
---|
627 | } |
---|
628 | } |
---|
629 | delete($data{services}); |
---|
630 | } |
---|
631 | foreach my $field (keys %data) { |
---|
632 | my $attr = $self->attribute($field); |
---|
633 | my $oldval = $self->get_field($field); |
---|
634 | next if (($data{$field} || '') eq ($oldval || '')); |
---|
635 | if ($attr->{inline}) { |
---|
636 | # TODO check fields exists ! |
---|
637 | push(@fields, sprintf("%s = ?", $self->db->quote_identifier($field))); |
---|
638 | # undef mean unset, handling null for exported: |
---|
639 | if ($field eq 'exported') { |
---|
640 | push(@vals, $data{$field} ? 1 : 0); |
---|
641 | } else { |
---|
642 | push(@vals, $data{$field} || undef); |
---|
643 | } |
---|
644 | push(@updated_attributes, $field); |
---|
645 | } else { |
---|
646 | $ext{$field} = $data{$field}; |
---|
647 | } |
---|
648 | } |
---|
649 | if (@fields) { |
---|
650 | my $sth = $self->db->prepare_cached( |
---|
651 | sprintf( |
---|
652 | q{update %s set %s where %s = ?}, |
---|
653 | $self->_quote_object_table, |
---|
654 | join(', ', @fields), |
---|
655 | $self->_quote_key_field, |
---|
656 | ) |
---|
657 | ); |
---|
658 | $sth->execute(@vals, $self->id) or do { |
---|
659 | $self->base->log(LA_ERR, |
---|
660 | "Cannot update inline field for object %s, %s: %s", |
---|
661 | $self->type, |
---|
662 | $self->id, |
---|
663 | $self->base->db->errstr); |
---|
664 | return; |
---|
665 | }; |
---|
666 | } |
---|
667 | |
---|
668 | if ($self->_has_extended_attributes) { |
---|
669 | my $sthd = $self->db->prepare_cached( |
---|
670 | sprintf( |
---|
671 | q{delete from %s where okey = ? and attr = ?}, |
---|
672 | $self->db->quote_identifier($self->_object_table_attributes), |
---|
673 | ), |
---|
674 | ); |
---|
675 | my $sthd1 = $self->db->prepare_cached( |
---|
676 | sprintf( |
---|
677 | q{delete from %s where okey = ? and attr = ? and value = ?}, |
---|
678 | $self->db->quote_identifier($self->_object_table_attributes), |
---|
679 | ), |
---|
680 | ); |
---|
681 | my $sthx = $self->db->prepare_cached( |
---|
682 | sprintf( |
---|
683 | q{insert into %s (okey, attr, value) values (?,?,?)}, |
---|
684 | $self->db->quote_identifier($self->_object_table_attributes), |
---|
685 | ) |
---|
686 | ); |
---|
687 | my $sthu = $self->db->prepare_cached( |
---|
688 | sprintf( |
---|
689 | q{update %s set value = ? where okey = ? and attr = ?}, |
---|
690 | $self->db->quote_identifier($self->_object_table_attributes), |
---|
691 | ) |
---|
692 | ); |
---|
693 | |
---|
694 | my $okey = $self->_get_ikey($self->base, $self->id); |
---|
695 | foreach my $uattr (keys %ext) { |
---|
696 | my $attr = $self->attribute($uattr); |
---|
697 | if ($ext{$uattr}) { |
---|
698 | if ($attr->{multiple}) { |
---|
699 | my $updated = 0; |
---|
700 | my $oldvalue = $self->get_field($uattr); |
---|
701 | my %newvalues = map { $_ => 1 } (ref $ext{$uattr} |
---|
702 | ? @{$ext{$uattr}} |
---|
703 | : $ext{$uattr}); |
---|
704 | foreach (grep { $_ } ref $oldvalue ? @{$oldvalue} : $oldvalue) { |
---|
705 | if(exists($newvalues{$_})) { |
---|
706 | $newvalues{$_} = 0; |
---|
707 | } else { |
---|
708 | defined($sthd1->execute($okey, $uattr, $_)) or do { |
---|
709 | $self->base->log(LA_ERR, |
---|
710 | "Error while updating attributes on %s/%s %s: %s", |
---|
711 | $self->type, |
---|
712 | $self->id, |
---|
713 | $uattr, |
---|
714 | $self->base->db->errstr |
---|
715 | ); |
---|
716 | return; |
---|
717 | }; |
---|
718 | $updated++; |
---|
719 | } |
---|
720 | } |
---|
721 | foreach (grep { $newvalues{$_} } keys %newvalues) { |
---|
722 | $sthx->execute($okey, $uattr, $_) or do { |
---|
723 | $self->base->log(LA_ERR, |
---|
724 | "Error while updating attributes: %s/%s %s: %s", |
---|
725 | $self->type, |
---|
726 | $self->id, |
---|
727 | $uattr, |
---|
728 | $self->base->db->errstr |
---|
729 | ); |
---|
730 | return; |
---|
731 | }; |
---|
732 | $updated++; |
---|
733 | } |
---|
734 | push(@updated_attributes, $uattr) if ($updated); |
---|
735 | } else { |
---|
736 | my $res = $sthu->execute($ext{$uattr}, $okey, $uattr); |
---|
737 | defined($res) or do { |
---|
738 | $self->base->log(LA_ERR, |
---|
739 | "Error while udapting attributes: %s/%s %s: %s", |
---|
740 | $self->type, |
---|
741 | $self->id, |
---|
742 | $uattr, |
---|
743 | $self->base->db->errstr |
---|
744 | ); |
---|
745 | return; |
---|
746 | }; |
---|
747 | if ($res == 0) { |
---|
748 | $res = $sthx->execute($okey, $uattr, $ext{$uattr}); |
---|
749 | defined($res) or do { |
---|
750 | $self->base->log(LA_ERR, |
---|
751 | "Error while updating attributes: %s/%s %s: %s", |
---|
752 | $self->type, |
---|
753 | $self->id, |
---|
754 | $uattr, |
---|
755 | $self->base->db->errstr |
---|
756 | ); |
---|
757 | return; |
---|
758 | }; |
---|
759 | } |
---|
760 | push(@updated_attributes, $uattr); |
---|
761 | } |
---|
762 | } else { |
---|
763 | defined($sthd->execute($okey, $uattr)) or do { |
---|
764 | $self->base->log(LA_ERR, |
---|
765 | "Error while deleting attributes: %s/%s %s: %s", |
---|
766 | $self->otype, |
---|
767 | $self->id, |
---|
768 | $uattr, |
---|
769 | $self->base->db->errstr |
---|
770 | ); |
---|
771 | return; |
---|
772 | }; |
---|
773 | push(@updated_attributes, $uattr); |
---|
774 | } |
---|
775 | } |
---|
776 | } |
---|
777 | |
---|
778 | delete($self->base->{__cache}{"_" . $self->type}{$self->id}); |
---|
779 | |
---|
780 | foreach my $attr (@updated_attributes) { |
---|
781 | my $oattr = $self->attribute($attr); |
---|
782 | my $ref = $oattr->reference or next; |
---|
783 | my $refname = sprintf('%s.%s.%s', $self->type, $self->id, $attr); |
---|
784 | my $attrref = "oalias=$refname"; |
---|
785 | |
---|
786 | foreach my $alias ($self->base->search_objects($ref, $attrref)) { |
---|
787 | my $oalias = $self->base->GetAlias($ref, $alias) or next; |
---|
788 | $oalias->_update_aliases_cache; |
---|
789 | $oalias->_update_aliases_ptr; |
---|
790 | } |
---|
791 | } |
---|
792 | |
---|
793 | scalar(@updated_attributes); |
---|
794 | } |
---|
795 | |
---|
796 | sub PostSetAttribute { |
---|
797 | my ($self) = @_; |
---|
798 | |
---|
799 | my %data; |
---|
800 | foreach my $attr ($self->base->list_canonical_fields($self->type, 'r')) { |
---|
801 | my $oattr = $self->base->get_attr_schema($self->type, $attr); |
---|
802 | if ($oattr->{auto}) { |
---|
803 | my $source = "_$attr"; |
---|
804 | $data{$attr} = $self->_get_c_field($source); |
---|
805 | } |
---|
806 | } |
---|
807 | my $res = $self->set_fields(%data); |
---|
808 | |
---|
809 | if ($res) { |
---|
810 | if (my $obj = $self->ParentObject) { |
---|
811 | $res += $obj->PostSetAttribute; |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | return $res ? $res : 'OEO'; |
---|
816 | } |
---|
817 | |
---|
818 | |
---|
819 | =head2 SetNoDelete($value) |
---|
820 | |
---|
821 | Set nodelete attribute to true or false |
---|
822 | |
---|
823 | =cut |
---|
824 | |
---|
825 | sub SetNoDelete { |
---|
826 | my ($self, $value) = @_; |
---|
827 | |
---|
828 | my $sthr = $self->db->prepare_cached( |
---|
829 | sprintf( |
---|
830 | q{update %s set nodelete = ? where %s = ?}, |
---|
831 | $self->db->quote_identifier($self->_object_table), |
---|
832 | $self->db->quote_identifier($self->_key_field), |
---|
833 | ) |
---|
834 | ); |
---|
835 | |
---|
836 | if (($sthr->execute($value ? 'true' : 'false', $self->id) || 0) != 1) { |
---|
837 | $self->log(LA_ERR, "Erreur seting nodelete for %s/%s to %s", |
---|
838 | $self->type, |
---|
839 | $self->id, |
---|
840 | $value, |
---|
841 | ); |
---|
842 | return; |
---|
843 | } |
---|
844 | |
---|
845 | 1; |
---|
846 | } |
---|
847 | |
---|
848 | =head2 find_next_numeric_id($class, $base, $field, $min, $max) |
---|
849 | |
---|
850 | An optimize version to speedup user/group creation |
---|
851 | |
---|
852 | =cut |
---|
853 | |
---|
854 | sub find_next_numeric_id { |
---|
855 | my ($class, $base, $field, $min, $max) = @_; |
---|
856 | $base->attribute($class->type, $field) or return; |
---|
857 | $min ||= |
---|
858 | $field eq 'uidNumber' ? 500 : |
---|
859 | $field eq 'gidNumber' ? 500 : |
---|
860 | 1; |
---|
861 | $max ||= 65635; |
---|
862 | $base->log(LA_DEBUG, "Trying to find %s in range %d - %d", |
---|
863 | $field, $min, $max); |
---|
864 | my %existsid; |
---|
865 | $base->temp_switch_unexported(sub { |
---|
866 | foreach ($class->attributes_summary($base, $field)) { |
---|
867 | $existsid{ $_ } = 1; |
---|
868 | } |
---|
869 | }, 1); |
---|
870 | $min += 0; |
---|
871 | $max += 0; |
---|
872 | for(my $i = $min; $i <= $max; $i++) { |
---|
873 | $existsid{$i + 0} or do { |
---|
874 | $base->log(LA_DEBUG, "Next %s found: %d", $field, $i); |
---|
875 | return $i; |
---|
876 | }; |
---|
877 | } |
---|
878 | return; |
---|
879 | } |
---|
880 | |
---|
881 | sub attributes_summary { |
---|
882 | my ($class, $base, $dotAttribute) = @_; |
---|
883 | |
---|
884 | my ($attribute, $recursiveAttr) = $dotAttribute =~ /(\w+)(?:\.(.*))?/; |
---|
885 | |
---|
886 | my $attr = $base->attribute($class->type, $attribute) or do { |
---|
887 | $base->log(LA_ERR, "Cannot instantiate %s attribute", $attribute); |
---|
888 | return; |
---|
889 | }; |
---|
890 | if (!$attr->readable) { |
---|
891 | $base->log(LA_WARN, l('Attribute %s is not readable', $attribute)); |
---|
892 | return; |
---|
893 | } |
---|
894 | if (!$base->check_acl($class->type, $attribute, 'r')) { |
---|
895 | $base->log(LA_WARN, l('Permission denied to read attribute %s', $attribute)); |
---|
896 | return; |
---|
897 | } |
---|
898 | if ($attr->{managed}) { |
---|
899 | return $class->SUPER::attributes_summary($base, $attribute); |
---|
900 | } |
---|
901 | my $sth = $base->db->prepare_cached( |
---|
902 | $attr->{inline} |
---|
903 | ? sprintf( |
---|
904 | q{select %s as value from %s where internobject = false} . ($base->{wexported} ? '' : ' and "exported" = true'), |
---|
905 | $base->db->quote_identifier($attr->iname), |
---|
906 | $base->db->quote_identifier($class->_object_table), |
---|
907 | ) |
---|
908 | : sprintf( |
---|
909 | q{select value from %s join |
---|
910 | %s on %s.ikey = %s.okey where attr = ? and internobject = false group by value} . |
---|
911 | ($base->{wexported} ? '' : ' and "exported" = true'), |
---|
912 | $base->db->quote_identifier($class->_object_table), |
---|
913 | $base->db->quote_identifier($class->_object_table_attributes_read), |
---|
914 | $base->db->quote_identifier($class->_object_table), |
---|
915 | $base->db->quote_identifier($class->_object_table_attributes_read), |
---|
916 | ) |
---|
917 | ); |
---|
918 | $sth->execute($attr->{inline} ? () : ($attr->iname)); |
---|
919 | |
---|
920 | my %values; |
---|
921 | if ($recursiveAttr) { |
---|
922 | my $otype = $attr->reference or do { |
---|
923 | $base->log(LA_ERR, "Cannot do recursive search, no ref for attribute %s", $attribute); |
---|
924 | return; |
---|
925 | }; |
---|
926 | my %parentRes = $base->attributes_summary_by_object($otype, $recursiveAttr) or return; |
---|
927 | |
---|
928 | while (my $res = $sth->fetchrow_hashref) { |
---|
929 | defined($res->{value}) or next; |
---|
930 | $values{ $parentRes{ $res->{value} } } = 1; |
---|
931 | } |
---|
932 | } else { |
---|
933 | while (my $res = $sth->fetchrow_hashref) { |
---|
934 | $values{$res->{value}} = 1 if ($res->{value}); |
---|
935 | } |
---|
936 | } |
---|
937 | sort keys %values |
---|
938 | } |
---|
939 | |
---|
940 | sub _filterToUser { |
---|
941 | my ($self) = @_; |
---|
942 | |
---|
943 | if ( |
---|
944 | !$self->get_field('autoMemberFilter') |
---|
945 | && !$self->get_field('autoFromSutype')) { |
---|
946 | return 0; |
---|
947 | } |
---|
948 | $self->base->log(LA_DEBUG, |
---|
949 | "Populating group %s from autoMemberFilter/autoFromSutype attribute", |
---|
950 | $self->id |
---|
951 | ); |
---|
952 | |
---|
953 | my $autoin = $self->get_field('autoInclude'); |
---|
954 | my %include = map { $_ => 1 } grep { $_ } (ref $autoin ? @$autoin : $autoin); |
---|
955 | |
---|
956 | my $autoex = $self->get_field('autoExclude'); |
---|
957 | my %exclude = map { $_ => 1 } grep { $_ } (ref $autoex ? @$autoex : $autoex); |
---|
958 | |
---|
959 | my %users = %include; |
---|
960 | |
---|
961 | if (my $filter = $self->get_field('autoFromSutype')) { |
---|
962 | my @suTypeFilter = map { "sutype=$_" } (ref $filter ? @{ $filter } : $filter); |
---|
963 | foreach my $group ($self->base->search_objects('group', @suTypeFilter)) { |
---|
964 | foreach ($self->base->search_objects('user', "memberOf=$group")) { |
---|
965 | $users{$_} = 1; |
---|
966 | } |
---|
967 | } |
---|
968 | } |
---|
969 | |
---|
970 | if (my $filter = $self->get_field('autoMemberFilter')) { |
---|
971 | foreach ($self->base->search_objects( |
---|
972 | 'user', |
---|
973 | 'oalias=NULL', |
---|
974 | ref $filter ? @{ $filter } : $filter)) { |
---|
975 | $users{$_} = 1; |
---|
976 | } |
---|
977 | } |
---|
978 | |
---|
979 | my @Userlist = grep { ! $exclude{ $_ } } keys %users; |
---|
980 | |
---|
981 | @Userlist |
---|
982 | } |
---|
983 | |
---|
984 | |
---|
985 | =head2 attributes_summary_by_object($base, $attribute) |
---|
986 | |
---|
987 | Return a hash containing object/value peer for C<$attribute>. |
---|
988 | |
---|
989 | =cut |
---|
990 | |
---|
991 | sub attributes_summary_by_object { |
---|
992 | my ($class, $base, $dotAttribute) = @_; |
---|
993 | |
---|
994 | my ($attribute, $recursiveAttr) = $dotAttribute =~ /(\w+)(?:\.(.*))?/; |
---|
995 | |
---|
996 | my $attr = $base->attribute($class->type, $attribute) or do { |
---|
997 | $base->log(LA_ERR, "Cannot instantiate %s attribute", $attribute); |
---|
998 | return; |
---|
999 | }; |
---|
1000 | if (!$attr->readable) { |
---|
1001 | $base->log(LA_WARN, l('Attribute %s is not readable', $attribute)); |
---|
1002 | return; |
---|
1003 | } |
---|
1004 | if (!$base->check_acl($class->type, $attribute, 'r')) { |
---|
1005 | $base->log(LA_WARN, l('Permission denied to read attribute %s', $attribute)); |
---|
1006 | return; |
---|
1007 | } |
---|
1008 | |
---|
1009 | if ($attr->{managed}) { |
---|
1010 | return $class->SUPER::attributes_summary_by_object($base, $attribute); |
---|
1011 | } |
---|
1012 | my $sth = $base->db->prepare_cached( |
---|
1013 | $attr->{inline} |
---|
1014 | ? sprintf( |
---|
1015 | q{ |
---|
1016 | select name, %s as value from %s} . ($base->{wexported} ? '' : ' where "exported" = true'), |
---|
1017 | $base->db->quote_identifier($attr->iname), |
---|
1018 | $base->db->quote_identifier($class->_object_table), |
---|
1019 | ) |
---|
1020 | : sprintf( |
---|
1021 | q{select name, value from %s left join %s on %s.ikey = %s.okey and attr = ?} . ($base->{wexported} ? '' : ' and "exported" = true'), |
---|
1022 | $base->db->quote_identifier($class->_object_table), |
---|
1023 | $base->db->quote_identifier($class->_object_table_attributes_read), |
---|
1024 | $base->db->quote_identifier($class->_object_table), |
---|
1025 | $base->db->quote_identifier($class->_object_table_attributes_read), |
---|
1026 | ) |
---|
1027 | ); |
---|
1028 | $sth->execute($attr->{inline} ? () : ($attr->iname)); |
---|
1029 | |
---|
1030 | my %values; |
---|
1031 | if ($recursiveAttr) { |
---|
1032 | my $otype = $attr->reference or do { |
---|
1033 | $base->log(LA_ERR, "Cannot do recursive search, no ref for attribute %s", $attribute); |
---|
1034 | return; |
---|
1035 | }; |
---|
1036 | my %parentRes = $base->attributes_summary_by_object($otype, $recursiveAttr) or return; |
---|
1037 | |
---|
1038 | while (my $res = $sth->fetchrow_hashref) { |
---|
1039 | defined($res->{value}) or next; |
---|
1040 | push(@{ $values{ $res->{name} } }, @{ $parentRes{ $res->{value} } || []}); |
---|
1041 | } |
---|
1042 | } else { |
---|
1043 | while (my $res = $sth->fetchrow_hashref) { |
---|
1044 | defined($res->{value}) or next; |
---|
1045 | push(@{ $values{ $res->{name} } }, $res->{value}); |
---|
1046 | } |
---|
1047 | } |
---|
1048 | %values |
---|
1049 | } |
---|
1050 | |
---|
1051 | sub search { |
---|
1052 | my ($class, $base, @filter) = @_; |
---|
1053 | |
---|
1054 | # Results groups by attr (OR filter) |
---|
1055 | # foo=1 foo=1 => foo = 1 or foo = 2 |
---|
1056 | # foo=1 bar=1 => foo =1 and bar = 2 |
---|
1057 | my $results = {}; |
---|
1058 | my $noalias = 0; |
---|
1059 | |
---|
1060 | @filter = grep { defined($_) && $_ ne '' } @filter; |
---|
1061 | if (!@filter) { |
---|
1062 | my ($package, $filename, $line) = caller; |
---|
1063 | $base->log(LA_DEBUG, "search() call w/o filter at %s:%d", $filename, $line); |
---|
1064 | return $base->list_objects($class->type); |
---|
1065 | } |
---|
1066 | |
---|
1067 | while (my $item = shift(@filter)) { |
---|
1068 | # attr=foo => no extra white space ! |
---|
1069 | # \W is false, it is possible to have two char |
---|
1070 | my ($attr, $attrref, $operator, $val) = $item =~ /^(\w+)(?:\.([\.\w]+))?(?:([^\w*]+)(.+))?$/ or next; |
---|
1071 | if (!$operator) { |
---|
1072 | $operator = '~'; |
---|
1073 | $val = shift(@filter); |
---|
1074 | } |
---|
1075 | |
---|
1076 | if (my ($func, $args) = $val =~ /^(\w+)\((.*)\)$/) { |
---|
1077 | $val = $base->QFunc( $func, $args ); |
---|
1078 | } |
---|
1079 | |
---|
1080 | my $attribute = $base->attribute($class->type, $attr) or do { |
---|
1081 | $base->log(LA_ERR, "Unknown attribute $attr"); |
---|
1082 | return; |
---|
1083 | }; |
---|
1084 | $attribute->name eq 'oalias' and $noalias = 1; |
---|
1085 | defined($val) or $val = ''; |
---|
1086 | |
---|
1087 | $base->log(LA_DEBUG, "Search for %s %s (ref %s) %s %s", $class->type, $attr, $attrref || '(none)', $operator || '', $val); |
---|
1088 | |
---|
1089 | # Invalid filter due to impossible value: |
---|
1090 | if ($operator ne '~' && !($operator eq '=' && ($val eq 'NULL' || $val eq '*'))) { |
---|
1091 | if (!$attribute->checkinputformat($val)) { |
---|
1092 | $base->log(LA_ERR, "Invalid format value $val for attribute $attr"); |
---|
1093 | return; |
---|
1094 | } |
---|
1095 | } |
---|
1096 | my $attrKey = $attr; |
---|
1097 | |
---|
1098 | if ($attrref) { |
---|
1099 | my $otype = $attribute->reference or do { |
---|
1100 | $base->log(LA_ERR, "Attribute $attr do not refer to another object"); |
---|
1101 | return; |
---|
1102 | }; |
---|
1103 | |
---|
1104 | $attrKey .= '.' . $attrref; |
---|
1105 | my @results = $base->search_objects($otype, "$attrref$operator$val"); |
---|
1106 | $base->log(LA_DEBUG, "Sub search %s res: %s", $otype, join(', ', @results)); |
---|
1107 | |
---|
1108 | if (!@results) { |
---|
1109 | $results->{$attrKey} ||= {}; |
---|
1110 | next; |
---|
1111 | } |
---|
1112 | |
---|
1113 | ($operator, $val) = ('=', join('||', @results)); |
---|
1114 | } |
---|
1115 | |
---|
1116 | my @results = $class->_search_uniq_filter($base, $attr, $operator, $val); |
---|
1117 | |
---|
1118 | $base->log(LA_DEBUG, "Search result: %s: %s", $attr, join(' ', @results)); |
---|
1119 | $results->{$attrKey} ||= {}; |
---|
1120 | $results->{$attrKey}{$_} = 1 foreach (@results); |
---|
1121 | } |
---|
1122 | |
---|
1123 | # Merging filter result |
---|
1124 | |
---|
1125 | my %mresults = map { $_ => 1 } $base->list_objects($class->type); |
---|
1126 | |
---|
1127 | foreach my $attr (keys %{ $results }) { |
---|
1128 | my @values = keys %mresults; |
---|
1129 | foreach my $value (sort(@values)) { |
---|
1130 | if (!$results->{$attr}{$value}) { |
---|
1131 | delete($mresults{$value}); |
---|
1132 | } |
---|
1133 | } |
---|
1134 | } |
---|
1135 | |
---|
1136 | # We add to result aliases pointing to these object: |
---|
1137 | if(scalar(keys %mresults) && !$noalias) { |
---|
1138 | my @alias = $class->_search_uniq_filter($base, 'oalias', '=', join('||', keys %mresults)); |
---|
1139 | foreach(@alias) { |
---|
1140 | $mresults{$_} = 1; |
---|
1141 | } |
---|
1142 | } |
---|
1143 | |
---|
1144 | return(sort keys %mresults); |
---|
1145 | } |
---|
1146 | |
---|
1147 | sub _search_uniq_filter { |
---|
1148 | my ($class, $base, $attr, $operator, $value) = @_; |
---|
1149 | |
---|
1150 | my @attrsql; |
---|
1151 | my @attrbind; |
---|
1152 | |
---|
1153 | my $attribute = $base->attribute($class->type, $attr) or do { |
---|
1154 | $base->log(LA_ERR, "Unknown attribute $attr"); |
---|
1155 | return; |
---|
1156 | }; |
---|
1157 | |
---|
1158 | my @values = split(/([\|\&]+)/, $value); |
---|
1159 | |
---|
1160 | $base->log(LA_DEBUG, "Uniq search for " . $class->type . "->$attr $operator (%s)", join(' ', @values)); |
---|
1161 | |
---|
1162 | return unless(@values); |
---|
1163 | |
---|
1164 | # We detect if we can do a very quick search: |
---|
1165 | my $forRef = $operator eq '=' && scalar(@values) > 1 && ! grep { $_ eq '*' or $_ eq '&&' or $_ eq 'NULL' } @values; |
---|
1166 | if ($forRef) { |
---|
1167 | # Improv perf |
---|
1168 | |
---|
1169 | my @oaliases = (); |
---|
1170 | if ($attr ne 'oaliascache' && $attribute->reference) { |
---|
1171 | @oaliases = $base->search_objects($attribute->reference, "oaliascache=$value"); |
---|
1172 | $base->log(LA_DEBUG, "Uniq search will match oaliases: %s", join(' ', @oaliases) || '(none)'); |
---|
1173 | } |
---|
1174 | |
---|
1175 | if ($attribute->{inline}) { |
---|
1176 | my $sql = sprintf( |
---|
1177 | q{select ikey from %s where %s = ANY (?)}, |
---|
1178 | $base->db->quote_identifier($class->_object_table), |
---|
1179 | $base->db->quote_identifier($attribute->iname), |
---|
1180 | ); |
---|
1181 | push(@attrsql, $sql); |
---|
1182 | } else { |
---|
1183 | my $sql = sprintf( |
---|
1184 | q{select okey from %s where attr = ? and "value" = ANY (?) }, |
---|
1185 | $base->db->quote_identifier($class->_object_table_attributes_read), |
---|
1186 | ); |
---|
1187 | push(@attrbind, $attribute->iname); |
---|
1188 | push(@attrsql, $sql); |
---|
1189 | } |
---|
1190 | push(@attrbind, [ @oaliases, grep { $_ ne '||' } @values ]); |
---|
1191 | } else { |
---|
1192 | |
---|
1193 | # No optimisation possible: |
---|
1194 | while (defined(my $val = shift(@values))) { |
---|
1195 | |
---|
1196 | if ($val eq '&&') { |
---|
1197 | push(@attrsql, 'intersect'); |
---|
1198 | next; |
---|
1199 | } |
---|
1200 | if ($val eq '||') { |
---|
1201 | push(@attrsql, 'union'); |
---|
1202 | next; |
---|
1203 | } |
---|
1204 | |
---|
1205 | $val = $attribute->input($val) unless($operator eq '=' && $val eq 'NULL'); |
---|
1206 | |
---|
1207 | my $sql; |
---|
1208 | |
---|
1209 | # Specific case for unexported attribute, comming from exported value |
---|
1210 | if ($attribute->iname eq 'unexported') { |
---|
1211 | $sql = sprintf( |
---|
1212 | q{select ikey from %s where %s}, |
---|
1213 | $base->db->quote_identifier($class->_object_table), |
---|
1214 | $val ? q{exported='f'} : q{exported='t'} |
---|
1215 | ) |
---|
1216 | } elsif ($attribute->iname eq 'exported') { |
---|
1217 | $sql = sprintf( |
---|
1218 | q{select ikey from %s where %s}, |
---|
1219 | $base->db->quote_identifier($class->_object_table), |
---|
1220 | $val ? q{exported='t'} : q{exported='f'} |
---|
1221 | ) |
---|
1222 | } elsif ($attribute->{inline}) { |
---|
1223 | $sql = sprintf( |
---|
1224 | q{select ikey from %s where %s%s %s}, |
---|
1225 | $base->db->quote_identifier($class->_object_table), |
---|
1226 | $base->db->quote_identifier($attribute->iname), |
---|
1227 | ($operator eq '~' ? '::text' : ''), |
---|
1228 | $operator eq '=' && $val eq '*' |
---|
1229 | ? 'is not NULL' |
---|
1230 | : $operator eq '=' && $val eq 'NULL' |
---|
1231 | ? 'is NULL' |
---|
1232 | : $operator eq '~' |
---|
1233 | ? 'ILIKE ?' |
---|
1234 | : "$operator ?" |
---|
1235 | ); |
---|
1236 | push(@attrbind, $operator eq '~' ? '%' . $val . '%' : $val) unless($operator eq '=' && ($val eq '*' || $val eq 'NULL')); |
---|
1237 | } else { |
---|
1238 | if ($operator eq '=' && $val eq 'NULL') { |
---|
1239 | $sql = sprintf(q{ select ikey from %s where ikey |
---|
1240 | not in (select okey from %s where attr = ? and ("value" is NOT NULL and value != '')) }, |
---|
1241 | $base->db->quote_identifier($class->_object_table), |
---|
1242 | $base->db->quote_identifier( |
---|
1243 | $class->_object_table_attributes_read |
---|
1244 | ), |
---|
1245 | ); |
---|
1246 | push(@attrbind, $attribute->iname); |
---|
1247 | } else { |
---|
1248 | my @oaliases = (); |
---|
1249 | if ($attr ne 'oaliascache' && $attribute->reference) { |
---|
1250 | @oaliases = $base->search_objects($attribute->reference, "oaliascache=$val"); |
---|
1251 | $base->log(LA_DEBUG, "Uniq search will match oaliases for %s: %s", $val, join(' ', @oaliases) || '(none)'); |
---|
1252 | } |
---|
1253 | |
---|
1254 | $sql = sprintf( |
---|
1255 | q{select okey from %s where attr = ? %s}, |
---|
1256 | $base->db->quote_identifier( |
---|
1257 | $class->_object_table_attributes_read |
---|
1258 | ), |
---|
1259 | $val eq '*' |
---|
1260 | ? '' |
---|
1261 | : $operator eq '~' |
---|
1262 | ? q{and value::text ILIKE ANY(?)} |
---|
1263 | : qq{and value $operator ANY (?)} |
---|
1264 | |
---|
1265 | ); |
---|
1266 | push(@attrbind, $attribute->iname); |
---|
1267 | my @vals = ( $val, @oaliases ); |
---|
1268 | push(@attrbind, $operator eq '~' |
---|
1269 | ? [ map { '%' . $_ . '%' } @vals ] |
---|
1270 | : [ @vals ]) |
---|
1271 | unless($val eq '*'); |
---|
1272 | } |
---|
1273 | } |
---|
1274 | |
---|
1275 | push(@attrsql, $sql); |
---|
1276 | } |
---|
1277 | |
---|
1278 | } # Perf |
---|
1279 | |
---|
1280 | # building the query |
---|
1281 | if (!$base->{wexported}) { |
---|
1282 | push(@attrsql, 'intersect') if (@attrsql); |
---|
1283 | push(@attrsql, sprintf( |
---|
1284 | q{select ikey from %s where exported = true and internobject = false}, |
---|
1285 | $base->db->quote_identifier($class->_object_table) |
---|
1286 | ) |
---|
1287 | ); |
---|
1288 | } |
---|
1289 | my $sth = $base->db->prepare( |
---|
1290 | sprintf(q{ |
---|
1291 | select name from %s |
---|
1292 | where internobject = false |
---|
1293 | %s |
---|
1294 | order by name |
---|
1295 | }, |
---|
1296 | $base->db->quote_identifier($class->_object_table), |
---|
1297 | @attrsql |
---|
1298 | ? "and ikey in (\n" . join(" ", @attrsql) . ")\n" |
---|
1299 | : '', |
---|
1300 | ) |
---|
1301 | ); |
---|
1302 | $sth->execute(@attrbind); |
---|
1303 | my @results; |
---|
1304 | while (my $res = $sth->fetchrow_hashref) { |
---|
1305 | push(@results, $res->{name}); |
---|
1306 | } |
---|
1307 | return(@results); |
---|
1308 | } |
---|
1309 | |
---|
1310 | sub _update_aliases_ptr { |
---|
1311 | my ($self) = @_; |
---|
1312 | |
---|
1313 | my $atype = $self->type; |
---|
1314 | my $name = $self->id; |
---|
1315 | my $base = $self->base; |
---|
1316 | |
---|
1317 | foreach my $otype ($base->list_supported_objects) { |
---|
1318 | foreach my $attr ($base->list_canonical_fields($otype, 'r')) { |
---|
1319 | $attr =~ /^(oalias|modifiedby|createdby)$/ and next; |
---|
1320 | my $attribute = $base->attribute($otype, $attr); |
---|
1321 | my $ref = $attribute->reference or next; |
---|
1322 | |
---|
1323 | if ($ref eq $atype) { |
---|
1324 | $base->log(LA_DEBUG, "Searching object referencing alias %s/%s in %s->%s", |
---|
1325 | $atype, $name, $otype, $attr); |
---|
1326 | foreach my $target ($base->search_objects($otype, "$attr\=$name", 'oalias=NULL')) { |
---|
1327 | $base->log(LA_DEBUG, "Update ref for object %s/%s", $otype, $target); |
---|
1328 | my $otarget = $base->get_object($otype, $target) or next; |
---|
1329 | $otarget->refreshRev; |
---|
1330 | if ($otype eq 'group' && grep { $_ eq 'dpmt' } $otarget->_get_attributes('sutype')) { |
---|
1331 | $otarget->_update_employment_manager; |
---|
1332 | } |
---|
1333 | } |
---|
1334 | } |
---|
1335 | } |
---|
1336 | } |
---|
1337 | } |
---|
1338 | |
---|
1339 | sub _update_aliases_cache { |
---|
1340 | my ($self) = @_; |
---|
1341 | |
---|
1342 | my $getoalias = $self->base->db->prepare( |
---|
1343 | sprintf( |
---|
1344 | q{ select * from %s where %s = ? }, |
---|
1345 | $self->base->db->quote_identifier($self->_object_table), |
---|
1346 | $self->base->db->quote_identifier($self->_key_field), |
---|
1347 | ) |
---|
1348 | ); |
---|
1349 | $getoalias->execute($self->id); |
---|
1350 | my $res = $getoalias->fetchrow_hashref or do { |
---|
1351 | $self->base->log(LA_ERR, "Cannot find alias %s / %s", $self->type, $self->id); |
---|
1352 | return; |
---|
1353 | }; |
---|
1354 | $getoalias->finish; |
---|
1355 | |
---|
1356 | my $ref = $self->base->_derefObject($self->type, $res->{oalias}); |
---|
1357 | my $id = $ref ? $ref->id : undef; |
---|
1358 | |
---|
1359 | if (($id || '') ne ($res->{oaliascache} || '')) { |
---|
1360 | my $upd = $self->base->db->prepare( |
---|
1361 | sprintf( |
---|
1362 | q{update %s set oaliascache = ? where %s = ?}, |
---|
1363 | $self->base->db->quote_identifier($self->_object_table), |
---|
1364 | $self->base->db->quote_identifier($self->_key_field), |
---|
1365 | ) |
---|
1366 | ); |
---|
1367 | $self->base->log(LA_DEBUG, "Updating Cache for alias %s => %s", $self->id, $id || '(none)'); |
---|
1368 | return $upd->execute($id, $self->id); |
---|
1369 | } |
---|
1370 | } |
---|
1371 | |
---|
1372 | sub ReportChange { |
---|
1373 | my ($self, $changetype, $message, @args) = @_; |
---|
1374 | |
---|
1375 | $self->base->ReportChange( |
---|
1376 | $self->type, |
---|
1377 | $self->id, |
---|
1378 | $self->Iid, |
---|
1379 | $changetype, $message, @args |
---|
1380 | ) |
---|
1381 | } |
---|
1382 | |
---|
1383 | |
---|
1384 | 1; |
---|
1385 | |
---|
1386 | __END__ |
---|
1387 | |
---|
1388 | =head1 SEE ALSO |
---|
1389 | |
---|
1390 | L<LATMOS::Accounts::Bases::Sql> |
---|
1391 | |
---|
1392 | L<LATMOS::Accounts::Bases::Objects> |
---|
1393 | |
---|
1394 | =head1 AUTHOR |
---|
1395 | |
---|
1396 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
1397 | |
---|
1398 | =head1 COPYRIGHT AND LICENSE |
---|
1399 | |
---|
1400 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
1401 | |
---|
1402 | This library is free software; you can redistribute it and/or modify |
---|
1403 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
1404 | at your option, any later version of Perl 5 you may have available. |
---|
1405 | |
---|
1406 | |
---|
1407 | =cut |
---|