source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/objects.pm @ 2264

Last change on this file since 2264 was 2258, checked in by nanardon, 5 years ago

Add log attribute, allow to fetch object log

  • Property svn:keywords set to Id Rev
File size: 44.9 KB
Line 
1package LATMOS::Accounts::Bases::Sql::objects;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Objects);
8use LATMOS::Accounts::Log;
9use Crypt::RSA;
10use Crypt::RSA::Key::Public::SSH;
11use DateTime;
12use LATMOS::Accounts::I18N;
13
14our $VERSION = (q$Rev: 2105 $ =~ /^Rev: (\d+) /)[0];
15
16=head1 NAME
17
18LATMOS::Accounts::Bases::Sql::objects - Parent class for SQL object
19
20=cut
21
22# Write extend attribute table
23sub _object_table_attributes      { $_[0]->_object_table . '_attributes' }
24# Read search table (with compute attribute)
25sub _object_table_attributes_read { $_[0]->_object_table . '_attributes' }
26
27sub 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
48sub 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
68sub 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
88sub _has_extended_attributes { 0 }
89
90sub _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
279sub _managed_fields {
280    my ($class, $for, $base) = @_;
281    return();
282}
283
284sub 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
306sub Iid { $_[0]->_get_ikey }
307
308=head2 ParentObject
309
310When object is part of another return the parent object, return undef otherwise.
311
312=cut
313
314sub ParentObject { }
315
316sub _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
333sub _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    return $res->{k};
404}
405
406=head2 refreshRev
407
408Increase revision of the object to force synchronisation
409
410=cut
411
412sub refreshRev {
413    my ($self) = @_;
414
415    my $sth = $self->db->prepare_cached(
416        sprintf(q{
417                UPDATE %s SET rev = nextval('revisions_rev_seq'::regclass)  WHERE %s = ?
418            },
419            $self->db->quote_identifier($self->_object_table),
420            $self->db->quote_identifier($self->_key_field),
421        )
422    );
423    $sth->execute($self->id);
424}
425
426=head2 CreateAlias($base, $name, $for)
427
428Create an alias named C<$name> with pointing to C<$for>
429
430=cut
431
432sub CreateAlias {
433    my ($class, $base, $name, $for) = @_;
434
435    $base->log(LA_ERR, '%s does not support alias object, alias %s not created', $class->type, $name);
436}
437
438sub _delete {
439    my ($class, $base, $id) = @_;
440
441    my $__cache = $base->{__cache}{"_" . $class->type};
442
443    my $obj = $base->get_object($class->type, $id)
444        or return;
445
446    if ($obj->_get_attributes('internobject')) {
447        # Cannot happend: internal are not fetchable
448        $base->log(LA_ERR,'Cannot delete %s/%s: is an internal object', $class->type, $id);
449        return;
450    }
451    if ($obj->_get_attributes('nodelete')) {
452        $base->log(LA_ERR,'Cannot delete %s/%s: is write protected', $class->type, $id);
453        return;
454    }
455
456    my $sthd = $base->db->prepare_cached(
457        sprintf(
458            q{delete from %s where %s = ?},
459            $base->db->quote_identifier($class->_object_table),
460            $base->db->quote_identifier($class->_key_field),
461        )
462    );
463    my $res = $sthd->execute($id);
464    if ($res) {
465        delete($__cache->{$id});
466    }
467
468    $res
469}
470
471sub _rename {
472    my ($class, $base, $id, $newid) = @_;
473
474    my $sthr = $base->db->prepare_cached(
475        sprintf(
476            q{update %s set %s = ? where %s = ?},
477            $base->db->quote_identifier($class->_object_table),
478            $base->db->quote_identifier($class->_key_field),
479            $base->db->quote_identifier($class->_key_field),
480        )
481    );
482
483    if (($sthr->execute($newid, $id) || 0) != 1) {
484        $base->log(LA_ERR, "Erreur renaming %s %s to %s",
485            $class->type,
486            $id, $newid,
487        );
488        return;
489    }
490
491    1;
492}
493
494=head2 db
495
496Return reference to L<DBI> object.
497
498=cut
499
500sub db {
501    return $_[0]->base->db;
502}
503
504sub _quote_object_table {
505    my ($self) = @_;
506    my $table = $self->_object_table or return;
507    $self->db->quote_identifier($table);
508}
509sub _quote_key_field {
510    my ($self) = @_;
511    my $key_field = $self->_key_field or return;
512    $self->db->quote_identifier($key_field);
513}
514
515sub get_field {
516    my ($self, $field) = @_;
517    if ($field eq 'services') {
518        my @services;
519        my $sth = $self->db->prepare_cached(
520            q{ select name from service join service_attributes
521               on okey = ikey
522               where service_attributes.attr = 'dependOn' and value = ?
523               });
524        $sth->execute($self->type . '.' . $self->id);
525        while(my $res = $sth->fetchrow_hashref) {
526            push(@services, $res->{name});
527        }
528        return \@services; 
529    }
530    my $attr = $self->attribute($field) or return;
531    if ($attr->{inline}) {
532    my $sth = $self->db->prepare_cached(
533        sprintf(
534            q{select %s from %s where %s = ?},
535            $self->db->quote_identifier(lc($field)),
536            $self->_quote_object_table,
537            $self->_quote_key_field,
538        )
539    );
540    $sth->execute($self->id);
541    my $res = $sth->fetchrow_hashref or $self->db->rollback;
542    $sth->finish;
543    return $res->{$field};
544    } elsif ($self->_has_extended_attributes) { # else, then we mandatory have extend attr
545        $self->base->{__cache}{"_" . $self->type} ||= {};
546        my $__cache = $self->base->{__cache}{"_" . $self->type};
547        if (!(exists($__cache->{$self->id})
548            && $__cache->{$self->id}{__time} >= time - 1)) {
549        my $sth = $self->db->prepare_cached(
550            sprintf(
551                q{
552                select attr, value from %s
553                join %s on okey = ikey
554                where %s = ?
555                },
556                $self->db->quote_identifier($self->_object_table_attributes_read),
557                $self->db->quote_identifier($self->_object_table),
558                $self->db->quote_identifier($self->_key_field),
559            )
560        );
561        $sth->execute($self->id);
562        delete($__cache->{$self->id});
563        $__cache->{$self->id}{__time} = time;
564        while(my $res = $sth->fetchrow_hashref) {
565            push(@{$__cache->{$self->id}{$res->{attr}}}, $res->{value});
566        }
567        #return @values > 1 ? \@values : $values[0];
568        }
569        my $val = $__cache->{$self->id}{$field};
570        return @{$val || []} > 1 ? $val : $val->[0];
571    }
572}
573
574sub GetAttributeValue {
575    my ($self, $cfield) = @_;
576
577    my $res = $self->SUPER::GetAttributeValue($cfield) or return;
578
579    my $attribute = $self->attribute($cfield) or do {
580        $self->base->log(LA_WARN, "Unknow attribute $cfield");
581        return;
582    };
583
584    if (my $ref = $attribute->reference) {
585        my @deref;
586        foreach my $v (ref $res ? @{ $res } : $res) {
587            my $derefobj = $self->base->_derefObject($ref, $v);
588            push(@deref, $derefobj->id) if ($derefobj);
589        }
590        return scalar(@deref) > 1 ? \@deref : $deref[0];
591    } else {
592        return $res;
593    }
594}
595
596sub set_fields {
597    my ($self, %data) = @_;
598    my @updated_attributes = ();
599    my @fields;
600    my @vals;
601    my %ext;
602    if (exists($data{services})) {
603        my %old = map { $_ => 0 } $self->get_attributes('services');
604        foreach my $serv (grep { $_ } ref $data{services} ? @{ $data{services} } : $data{services}) {
605            if (!exists($old{$serv})) {
606                my $oserv = $self->base->get_object('service', $serv) or next;
607                $oserv->addAttributeValue('dependOn', $self->type . '.' . $self->id);
608            }
609            $old{$serv} = 1;
610        }
611        foreach my $serv (keys %old) {
612            if (!$old{$serv}) {
613                my $oserv = $self->base->get_object('service', $serv) or next;
614                $oserv->delAttributeValue('dependOn', $self->type . '.' . $self->id);
615            }
616        }
617        delete($data{services});
618    }
619    foreach my $field (keys %data) {
620        my $attr = $self->attribute($field);
621        my $oldval = $self->get_field($field);
622        next if (($data{$field} || '') eq ($oldval || ''));
623        if ($attr->{inline}) {
624        # TODO check fields exists !
625            push(@fields, sprintf("%s = ?", $self->db->quote_identifier($field)));
626            # undef mean unset, handling null for exported:
627            if ($field eq 'exported') {
628                push(@vals, $data{$field} ? 1 : 0);
629            } else {
630                push(@vals, $data{$field} || undef);
631            }
632            push(@updated_attributes, $field);
633        } else {
634            $ext{$field} = $data{$field};
635        }
636    }
637    if (@fields) {
638        my $sth = $self->db->prepare_cached(
639            sprintf(
640                q{update %s set %s where %s = ?},
641                $self->_quote_object_table,
642                join(', ', @fields),
643                $self->_quote_key_field,
644            )
645        );
646        $sth->execute(@vals, $self->id) or do {
647            $self->base->log(LA_ERR,
648                "Cannot update inline field for object %s, %s: %s",
649                $self->type,
650                $self->id,
651                $self->base->db->errstr);
652            return;
653        };
654    }
655   
656    if ($self->_has_extended_attributes) {
657        my $sthd = $self->db->prepare_cached(
658            sprintf(
659                q{delete from %s where okey = ? and attr = ?},
660                $self->db->quote_identifier($self->_object_table_attributes),
661            ),
662        );
663        my $sthd1 = $self->db->prepare_cached(
664            sprintf(
665                q{delete from %s where okey = ? and attr = ? and value = ?},
666                $self->db->quote_identifier($self->_object_table_attributes),
667            ),
668        );
669        my $sthx = $self->db->prepare_cached(
670            sprintf(
671                q{insert into %s (okey, attr, value) values (?,?,?)},
672                $self->db->quote_identifier($self->_object_table_attributes),
673            )
674        );
675        my $sthu = $self->db->prepare_cached(
676            sprintf(
677                q{update %s set value = ? where okey = ? and attr = ?},
678                $self->db->quote_identifier($self->_object_table_attributes),
679            )
680        );
681
682        my $okey = $self->_get_ikey($self->base, $self->id);
683        foreach my $uattr (keys %ext) {
684            my $attr = $self->attribute($uattr);
685            if ($ext{$uattr}) {
686                if ($attr->{multiple}) {
687                    my $updated = 0;
688                    my $oldvalue = $self->get_field($uattr);
689                    my %newvalues = map { $_ => 1 } (ref $ext{$uattr}
690                        ? @{$ext{$uattr}}
691                        : $ext{$uattr});
692                    foreach (grep { $_ } ref $oldvalue ? @{$oldvalue} : $oldvalue) {
693                        if(exists($newvalues{$_})) {
694                            $newvalues{$_} = 0;
695                        } else {
696                            defined($sthd1->execute($okey, $uattr, $_)) or do {
697                                $self->base->log(LA_ERR,
698                                    "Error while updating attributes on %s/%s %s: %s",
699                                    $self->type,
700                                    $self->id,
701                                    $uattr,
702                                    $self->base->db->errstr
703                                );
704                                return;
705                            };
706                            $updated++;
707                        }
708                    }
709                    foreach (grep { $newvalues{$_} } keys %newvalues) {
710                        $sthx->execute($okey, $uattr, $_) or do {
711                            $self->base->log(LA_ERR,
712                                "Error while updating attributes: %s/%s %s: %s",
713                                $self->type,
714                                $self->id,
715                                $uattr,
716                                $self->base->db->errstr
717                            );
718                            return;
719                        };
720                        $updated++;
721                    }
722                    push(@updated_attributes, $uattr) if ($updated);
723                } else {
724                    my $res = $sthu->execute($ext{$uattr}, $okey, $uattr);
725                    defined($res) or do {
726                        $self->base->log(LA_ERR,
727                            "Error while udapting attributes: %s/%s %s: %s",
728                            $self->type,
729                            $self->id,
730                            $uattr,
731                            $self->base->db->errstr
732                        );
733                        return;
734                    };
735                    if ($res == 0) {
736                        $res = $sthx->execute($okey, $uattr, $ext{$uattr});
737                        defined($res) or do {
738                            $self->base->log(LA_ERR,
739                                "Error while updating attributes: %s/%s %s: %s",
740                                $self->type,
741                                $self->id,
742                                $uattr,
743                                $self->base->db->errstr
744                            );
745                            return;
746                        };
747                    }
748                    push(@updated_attributes, $uattr);
749                }
750            } else {
751                defined($sthd->execute($okey, $uattr)) or do {
752                    $self->base->log(LA_ERR,
753                        "Error while deleting attributes: %s/%s %s: %s",
754                        $self->otype,
755                        $self->id,
756                        $uattr,
757                        $self->base->db->errstr
758                    );
759                    return;
760                };
761                push(@updated_attributes, $uattr);
762            }
763        }
764    }
765
766    delete($self->base->{__cache}{"_" . $self->type}{$self->id});
767
768    foreach my $attr (@updated_attributes) {
769        my $oattr = $self->attribute($attr);
770        my $ref = $oattr->reference or next;
771        my $refname = sprintf('%s.%s.%s', $self->type, $self->id, $attr);
772        my $attrref = "oalias=$refname";
773
774        foreach my $alias ($self->base->search_objects($ref, $attrref)) {
775            my $oalias = $self->base->GetAlias($ref, $alias) or next;
776            $oalias->_update_aliases_cache;
777            $oalias->_update_aliases_ptr;
778        }
779    }
780
781    scalar(@updated_attributes);
782}
783
784sub PostSetAttribute {
785    my ($self) = @_;
786
787    my %data;
788    foreach my $attr ($self->base->list_canonical_fields($self->type, 'r')) {
789        my $oattr = $self->base->get_attr_schema($self->type, $attr);
790        if ($oattr->{auto}) {
791            my $source = "_$attr";
792            $data{$attr} = $self->_get_c_field($source);
793        }
794    }
795    my $res =  $self->set_fields(%data);
796
797    return $res ? $res : 'OEO';
798}
799
800
801=head2 SetNoDelete($value)
802
803Set nodelete attribute to true or false
804
805=cut
806
807sub SetNoDelete {
808    my ($self, $value) = @_;
809
810    my $sthr = $self->db->prepare_cached(
811        sprintf(
812            q{update %s set nodelete = ? where %s = ?},
813            $self->db->quote_identifier($self->_object_table),
814            $self->db->quote_identifier($self->_key_field),
815        )
816    );
817
818    if (($sthr->execute($value ? 'true' : 'false', $self->id) || 0) != 1) {
819        $self->log(LA_ERR, "Erreur seting nodelete for %s/%s to %s",
820            $self->type,
821            $self->id,
822            $value,
823        );
824        return;
825    }
826
827    1;
828}
829
830=head2 find_next_numeric_id($class, $base, $field, $min, $max)
831
832An optimize version to speedup user/group creation
833
834=cut
835
836sub find_next_numeric_id {
837    my ($class, $base, $field, $min, $max) = @_;
838    $base->attribute($class->type, $field) or return;
839    $min ||=
840        $field eq 'uidNumber' ? 500 :
841        $field eq 'gidNumber' ? 500 :
842        1;
843    $max ||= 65635;
844    $base->log(LA_DEBUG, "Trying to find %s in range %d - %d",
845        $field, $min, $max);
846    my %existsid;
847    $base->temp_switch_unexported(sub {
848        foreach ($class->attributes_summary($base, $field)) {
849            $existsid{ $_ } = 1;
850        }
851    }, 1);
852    $min += 0;
853    $max += 0;
854    for(my $i = $min; $i <= $max; $i++) {
855        $existsid{$i + 0} or do {
856            $base->log(LA_DEBUG, "Next %s found: %d", $field, $i);
857            return $i;
858        };
859    }
860    return;
861}
862
863sub attributes_summary {
864    my ($class, $base, $dotAttribute) = @_;
865
866    my ($attribute, $recursiveAttr) = $dotAttribute =~ /(\w+)(?:\.(.*))?/;
867   
868    my $attr = $base->attribute($class->type, $attribute) or do {
869        $base->log(LA_ERR, "Cannot instantiate %s attribute", $attribute);
870        return;
871    };
872    if (!$attr->readable) {
873        $base->log(LA_WARN, l('Attribute %s is not readable', $attribute));
874        return;
875    }
876    if (!$base->check_acl($class->type, $attribute, 'r')) {
877        $base->log(LA_WARN, l('Permission denied to read attribute %s', $attribute));
878        return;
879    }
880    if ($attr->{managed}) { 
881        return $class->SUPER::attributes_summary($base, $attribute);
882    }
883    my $sth = $base->db->prepare_cached(
884        $attr->{inline}
885            ? sprintf(
886                q{select %s as value from %s where internobject = false} . ($base->{wexported} ? '' : ' and "exported" = true'),
887                $base->db->quote_identifier($attr->iname),
888                $base->db->quote_identifier($class->_object_table),
889            )
890            : sprintf(
891                q{select value from %s join
892                %s on %s.ikey = %s.okey where attr = ? and internobject = false group by value} . 
893                    ($base->{wexported} ? '' : ' and "exported" = true'),
894                $base->db->quote_identifier($class->_object_table),
895                $base->db->quote_identifier($class->_object_table_attributes_read),
896                $base->db->quote_identifier($class->_object_table),
897                $base->db->quote_identifier($class->_object_table_attributes_read),
898            )
899    );
900    $sth->execute($attr->{inline} ? () : ($attr->iname));
901
902    my %values;
903    if ($recursiveAttr) {
904        my $otype = $attr->reference or do {
905            $base->log(LA_ERR, "Cannot do recursive search, no ref for attribute %s", $attribute);
906            return;
907        };
908        my %parentRes = $base->attributes_summary_by_object($otype, $recursiveAttr) or return;
909
910        while (my $res = $sth->fetchrow_hashref) {
911            defined($res->{value}) or next;
912            $values{ $parentRes{ $res->{value} } } = 1;
913        }
914    } else {
915        while (my $res = $sth->fetchrow_hashref) {
916            $values{$res->{value}} = 1 if ($res->{value});
917        }
918    }
919    sort keys %values
920}
921
922sub _filterToUser {
923    my ($self) = @_;
924
925    if (
926           !$self->get_field('autoMemberFilter')
927        && !$self->get_field('autoFromSutype')) {
928        return 0;
929    }
930    $self->base->log(LA_DEBUG,
931        "Populating group %s from autoMemberFilter/autoFromSutype attribute",
932        $self->id
933    );
934
935    my $autoin = $self->get_field('autoInclude');
936    my %include = map { $_ => 1 } grep { $_  } (ref $autoin ? @$autoin : $autoin);
937
938    my $autoex = $self->get_field('autoExclude');
939    my %exclude = map { $_ => 1 } grep { $_  } (ref $autoex ? @$autoex : $autoex);
940
941    my %users = %include;
942
943    if (my $filter = $self->get_field('autoFromSutype')) {
944        my @suTypeFilter = map { "sutype=$_" } (ref $filter ? @{ $filter } : $filter);
945        foreach my $group ($self->base->search_objects('group', @suTypeFilter)) {
946            foreach ($self->base->search_objects('user', "memberOf=$group")) {
947                $users{$_} = 1;
948            }
949        }
950    }
951
952    if (my $filter = $self->get_field('autoMemberFilter')) {
953        foreach ($self->base->search_objects(
954                'user',
955                'oalias=NULL',
956                ref $filter ? @{ $filter } : $filter)) {
957            $users{$_} = 1;
958        }
959    }
960
961    my @Userlist = grep { ! $exclude{ $_ } } keys %users;
962
963    @Userlist
964}
965 
966
967=head2 attributes_summary_by_object($base, $attribute)
968
969Return a hash containing object/value peer for C<$attribute>.
970
971=cut
972
973sub attributes_summary_by_object {
974    my ($class, $base, $dotAttribute) = @_;
975
976    my ($attribute, $recursiveAttr) = $dotAttribute =~ /(\w+)(?:\.(.*))?/;
977
978    my $attr = $base->attribute($class->type, $attribute) or do {
979        $base->log(LA_ERR, "Cannot instantiate %s attribute", $attribute);
980        return;
981    };
982    if (!$attr->readable) {
983        $base->log(LA_WARN, l('Attribute %s is not readable', $attribute));
984        return;
985    }
986    if (!$base->check_acl($class->type, $attribute, 'r')) {
987        $base->log(LA_WARN, l('Permission denied to read attribute %s', $attribute));
988        return;
989    }
990
991    if ($attr->{managed}) {
992        return $class->SUPER::attributes_summary_by_object($base, $attribute);
993    }
994    my $sth = $base->db->prepare_cached(
995        $attr->{inline}
996            ? sprintf(
997                q{
998                select name, %s as value from %s} . ($base->{wexported} ? '' : ' where "exported" = true'),
999                $base->db->quote_identifier($attr->iname),
1000                $base->db->quote_identifier($class->_object_table),
1001            )
1002            : sprintf(
1003                q{select name, value from %s left join %s on %s.ikey = %s.okey and attr = ?} . ($base->{wexported} ? '' : ' and "exported" = true'),
1004                $base->db->quote_identifier($class->_object_table),
1005                $base->db->quote_identifier($class->_object_table_attributes_read),
1006                $base->db->quote_identifier($class->_object_table),
1007                $base->db->quote_identifier($class->_object_table_attributes_read),
1008            )
1009    );
1010    $sth->execute($attr->{inline} ? () : ($attr->iname));
1011
1012    my %values;
1013    if ($recursiveAttr) {
1014        my $otype = $attr->reference or do {
1015            $base->log(LA_ERR, "Cannot do recursive search, no ref for attribute %s", $attribute);
1016            return;
1017        };
1018        my %parentRes = $base->attributes_summary_by_object($otype, $recursiveAttr) or return;
1019
1020        while (my $res = $sth->fetchrow_hashref) {
1021            defined($res->{value}) or next;
1022            push(@{ $values{ $res->{name} } }, @{ $parentRes{ $res->{value} } || []});
1023        }
1024    } else {
1025        while (my $res = $sth->fetchrow_hashref) {
1026            defined($res->{value}) or next;
1027            push(@{ $values{ $res->{name} } }, $res->{value});
1028        }
1029    }
1030    %values
1031}
1032
1033sub search {
1034    my ($class, $base, @filter) = @_;
1035
1036    # Results groups by attr (OR filter)
1037    # foo=1 foo=1 => foo = 1 or foo = 2
1038    # foo=1 bar=1 => foo =1 and bar = 2
1039    my $results = {};
1040    my $noalias = 0;
1041
1042    @filter = grep { defined($_) && $_ ne '' } @filter;
1043    if (!@filter) {
1044        my ($package, $filename, $line) = caller;
1045        $base->log(LA_DEBUG, "search() call w/o filter at %s:%d", $filename, $line);
1046        return $base->list_objects($class->type);
1047    }
1048
1049    while (my $item = shift(@filter)) {
1050        # attr=foo => no extra white space !
1051        # \W is false, it is possible to have two char
1052        my ($attr, $attrref, $operator, $val) = $item =~ /^(\w+)(?:\.([\.\w]+))?(?:([^\w*]+)(.+))?$/ or next;
1053        if (!$operator) {
1054            $operator = '~';
1055            $val = shift(@filter);
1056        }
1057
1058        if (my ($func, $args) = $val =~ /^(\w+)\((.*)\)$/) {
1059            $val = $base->QFunc( $func, $args );
1060        }
1061
1062        my $attribute = $base->attribute($class->type, $attr) or do {
1063            $base->log(LA_ERR, "Unknown attribute $attr");
1064            return;
1065        };
1066        $attribute->name eq 'oalias' and $noalias = 1;
1067        defined($val) or $val =  '';
1068
1069        $base->log(LA_DEBUG, "Search for %s %s (ref %s) %s %s", $class->type, $attr, $attrref || '(none)', $operator || '', $val);
1070
1071        # Invalid filter due to impossible value:
1072        if ($operator ne '~' && !($operator eq '=' && ($val eq 'NULL' || $val eq '*'))) {
1073            if (!$attribute->checkinputformat($val)) {
1074                $base->log(LA_ERR, "Invalid format value $val for attribute $attr");
1075                return;
1076            }
1077        }
1078        my $attrKey = $attr;
1079
1080        if ($attrref) {
1081            my $otype = $attribute->reference or do {
1082                $base->log(LA_ERR, "Attribute $attr do not refer to another object");
1083                return;
1084            };
1085
1086            $attrKey .= '.' . $attrref;
1087            my @results = $base->search_objects($otype, "$attrref$operator$val");
1088            $base->log(LA_DEBUG, "Sub search %s res: %s", $otype, join(', ', @results));
1089
1090            if (!@results) {
1091                $results->{$attrKey} ||= {};
1092                next;
1093            }
1094
1095            ($operator, $val) = ('=', join('||', @results));
1096        }
1097
1098        my @results = $class->_search_uniq_filter($base, $attr, $operator, $val);
1099
1100        $base->log(LA_DEBUG, "Search result: %s: %s", $attr, join(' ', @results));
1101        $results->{$attrKey} ||= {};
1102        $results->{$attrKey}{$_} = 1 foreach (@results);
1103    }
1104
1105    # Merging filter result
1106
1107    my %mresults = map { $_ => 1 } $base->list_objects($class->type);
1108
1109    foreach my $attr (keys %{ $results }) {
1110        my @values = keys %mresults;
1111        foreach my $value (sort(@values)) {
1112            if (!$results->{$attr}{$value}) {
1113                delete($mresults{$value});
1114            }
1115        }
1116    }
1117
1118    # We add to result aliases pointing to these object:
1119    if(scalar(keys %mresults) && !$noalias) {
1120        my @alias = $class->_search_uniq_filter($base, 'oalias', '=', join('||', keys %mresults));
1121        foreach(@alias) {
1122            $mresults{$_} = 1;
1123        }
1124    }
1125
1126    return(sort keys %mresults);
1127}
1128
1129sub _search_uniq_filter {
1130    my ($class, $base, $attr, $operator, $value) = @_;
1131
1132    my @attrsql;
1133    my @attrbind;
1134
1135    my $attribute = $base->attribute($class->type, $attr) or do {
1136        $base->log(LA_ERR, "Unknown attribute $attr");
1137        return;
1138    };
1139
1140    my @values = split(/([\|\&]+)/, $value);
1141
1142    $base->log(LA_DEBUG, "Uniq search for " . $class->type . "->$attr $operator (%s)", join(' ', @values));
1143
1144    return unless(@values);
1145   
1146    # We detect if we can do a very quick search:
1147    my $forRef = $operator eq '=' && scalar(@values) > 1 && ! grep { $_ eq '*' or $_ eq  '&&' or $_ eq 'NULL' } @values;
1148    if ($forRef) {
1149        # Improv perf
1150
1151        my @oaliases = ();
1152        if ($attr ne 'oaliascache' && $attribute->reference) {
1153            @oaliases = $base->search_objects($attribute->reference, "oaliascache=$value");
1154            $base->log(LA_DEBUG, "Uniq search will match oaliases: %s", join(' ', @oaliases) || '(none)');
1155        }
1156
1157        if ($attribute->{inline}) {
1158            my $sql = sprintf(
1159                q{select ikey from %s where %s = ANY (?)},
1160                $base->db->quote_identifier($class->_object_table),
1161                $base->db->quote_identifier($attribute->iname),
1162            );
1163            push(@attrsql, $sql);
1164        } else {
1165            my $sql = sprintf(
1166                q{select okey from %s where attr = ? and "value" = ANY (?) },
1167                    $base->db->quote_identifier($class->_object_table_attributes_read),
1168            );
1169            push(@attrbind, $attribute->iname);
1170            push(@attrsql, $sql);
1171        }
1172        push(@attrbind, [ @oaliases, grep { $_ ne '||' } @values ]);
1173    } else {
1174
1175    # No optimisation possible:
1176    while (defined(my $val = shift(@values))) {
1177
1178        if ($val eq '&&') {
1179            push(@attrsql, 'intersect');
1180            next;
1181        }
1182        if ($val eq '||') {
1183            push(@attrsql, 'union');
1184            next;
1185        }
1186
1187        $val = $attribute->input($val) unless($operator eq '=' && $val eq 'NULL');
1188
1189        my $sql;
1190
1191        # Specific case for unexported attribute, comming from exported value
1192        if ($attribute->iname eq 'unexported') {
1193            $sql = sprintf(
1194                q{select ikey from %s where %s},
1195                $base->db->quote_identifier($class->_object_table),
1196                $val ? q{exported='f'} : q{exported='t'}
1197            )
1198        } elsif ($attribute->iname eq 'exported') {
1199            $sql = sprintf(
1200                q{select ikey from %s where %s},
1201                $base->db->quote_identifier($class->_object_table),
1202                $val ? q{exported='t'} : q{exported='f'}
1203            )
1204        } elsif ($attribute->{inline}) {
1205            $sql = sprintf(
1206                q{select ikey from %s where %s%s %s},
1207                $base->db->quote_identifier($class->_object_table),
1208                $base->db->quote_identifier($attribute->iname),
1209                ($operator eq '~' ? '::text' : ''),
1210                $operator eq '=' && $val eq '*'
1211                    ? 'is not NULL'
1212                    : $operator eq '=' && $val eq 'NULL'
1213                    ? 'is NULL'
1214                    : $operator eq '~'
1215                        ? 'ILIKE ?'
1216                        : "$operator ?"
1217            );
1218            push(@attrbind, $operator eq '~' ? '%' . $val . '%' : $val) unless($operator eq '=' && ($val eq '*' || $val eq 'NULL'));
1219        } else {
1220            if ($operator eq '=' && $val eq 'NULL') {
1221                $sql = sprintf(q{ select ikey from %s where ikey
1222                          not in (select okey from %s where attr = ? and ("value" is NOT NULL and value != '')) },
1223                          $base->db->quote_identifier($class->_object_table),
1224                          $base->db->quote_identifier(
1225                              $class->_object_table_attributes_read
1226                          ),
1227                );
1228                push(@attrbind, $attribute->iname);
1229            } else {
1230                my @oaliases = ();
1231                if ($attr ne 'oaliascache' && $attribute->reference) {
1232                    @oaliases = $base->search_objects($attribute->reference, "oaliascache=$val");
1233                    $base->log(LA_DEBUG, "Uniq search will match oaliases for %s: %s", $val, join(' ', @oaliases) || '(none)');
1234                }
1235
1236                $sql = sprintf(
1237                    q{select okey from %s where attr = ? %s},
1238                    $base->db->quote_identifier(
1239                        $class->_object_table_attributes_read
1240                    ),
1241                    $val eq '*'
1242                        ? ''
1243                        : $operator eq '~'
1244                            ? q{and value::text ILIKE ANY(?)}
1245                            : qq{and value $operator ANY (?)}
1246
1247                );
1248                push(@attrbind, $attribute->iname);
1249                my @vals = ( $val, @oaliases );
1250                push(@attrbind, $operator eq '~'
1251                    ? [ map { '%' . $_ . '%' } @vals ]
1252                    : [ @vals ])
1253                    unless($val eq '*');
1254            }
1255        }
1256
1257        push(@attrsql, $sql);
1258    }
1259
1260    } # Perf
1261
1262    # building the query
1263    if (!$base->{wexported}) {
1264        push(@attrsql, 'intersect') if (@attrsql);
1265        push(@attrsql, sprintf(
1266                q{select ikey from %s where exported = true and internobject = false},
1267                $base->db->quote_identifier($class->_object_table)
1268            )
1269        );
1270    }
1271    my $sth = $base->db->prepare(
1272        sprintf(q{
1273            select name from %s
1274            where internobject = false
1275            %s
1276            order by name
1277            },
1278            $base->db->quote_identifier($class->_object_table),
1279            @attrsql
1280            ? "and ikey in (\n" . join(" ", @attrsql) . ")\n"
1281            : '',
1282        )
1283    );
1284    $sth->execute(@attrbind);
1285    my @results;
1286    while (my $res = $sth->fetchrow_hashref) {
1287        push(@results, $res->{name});
1288    }
1289    return(@results);
1290}
1291
1292sub _update_aliases_ptr {
1293    my ($self) = @_;
1294
1295    my $atype = $self->type;
1296    my $name  = $self->id;
1297    my $base  = $self->base;
1298
1299    foreach my $otype ($base->list_supported_objects) {
1300        foreach my $attr ($base->list_canonical_fields($otype, 'r')) {
1301            $attr =~ /^(oalias|modifiedby|createdby)$/ and next;
1302            my $attribute = $base->attribute($otype, $attr);
1303            my $ref = $attribute->reference or next;
1304
1305            if ($ref eq $atype) {
1306                $base->log(LA_DEBUG, "Searching object referencing alias %s/%s in %s->%s",
1307                    $atype, $name, $otype, $attr);
1308                foreach my $target ($base->search_objects($otype, "$attr\=$name", 'oalias=NULL')) {
1309                    $base->log(LA_DEBUG, "Update ref for object %s/%s", $otype, $target);
1310                    my $otarget = $base->get_object($otype, $target) or next;
1311                    $otarget->refreshRev;
1312                    if ($otype eq 'group' && grep { $_ eq 'dpmt' } $otarget->_get_attributes('sutype')) {
1313                        $otarget->_update_employment_manager;
1314                    }
1315                }
1316            }
1317        }
1318    }
1319}
1320
1321sub _update_aliases_cache {
1322    my ($self) = @_;
1323
1324    my $getoalias = $self->base->db->prepare(
1325        sprintf(
1326            q{ select * from %s where %s = ? },
1327            $self->base->db->quote_identifier($self->_object_table),
1328            $self->base->db->quote_identifier($self->_key_field),
1329        )
1330    );
1331    $getoalias->execute($self->id);
1332    my $res = $getoalias->fetchrow_hashref or do {
1333        $self->base->log(LA_ERR, "Cannot find alias %s / %s", $self->type, $self->id);
1334        return;
1335    };
1336    $getoalias->finish;
1337
1338    my $ref = $self->base->_derefObject($self->type, $res->{oalias});
1339    my $id = $ref ? $ref->id : undef;
1340
1341    if (($id || '') ne ($res->{oaliascache} || '')) {
1342        my $upd = $self->base->db->prepare(
1343            sprintf(
1344                q{update %s set oaliascache = ? where %s = ?},
1345                $self->base->db->quote_identifier($self->_object_table),
1346                $self->base->db->quote_identifier($self->_key_field),
1347            )
1348        );
1349        $self->base->log(LA_DEBUG, "Updating Cache for alias %s => %s", $self->id, $id || '(none)');
1350        return $upd->execute($id, $self->id);
1351    }
1352}
1353
1354sub ReportChange {
1355    my ($self, $changetype, $message, @args) = @_;
1356
1357    $self->base->ReportChange(
1358        $self->type,
1359        $self->id,
1360        $self->Iid,
1361        $changetype, $message, @args
1362    )
1363}
1364
1365
13661;
1367
1368__END__
1369
1370=head1 SEE ALSO
1371
1372L<LATMOS::Accounts::Bases::Sql>
1373
1374L<LATMOS::Accounts::Bases::Objects>
1375
1376=head1 AUTHOR
1377
1378Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
1379
1380=head1 COPYRIGHT AND LICENSE
1381
1382Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
1383
1384This library is free software; you can redistribute it and/or modify
1385it under the same terms as Perl itself, either Perl version 5.10.0 or,
1386at your option, any later version of Perl 5 you may have available.
1387
1388
1389=cut
Note: See TracBrowser for help on using the repository browser.