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

Last change on this file since 2359 was 2352, checked in by nanardon, 4 years ago

Add basic template features

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