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

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

Add documentation for ParentObject?()

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