source: branches/4.0/LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Aliases.pm @ 1299

Last change on this file since 1299 was 1299, checked in by nanardon, 9 years ago

backport fix

File size: 4.6 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Aliases;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Sql::objects);
8use LATMOS::Accounts::Log;
9
10our $VERSION = (q$Rev: 341 $ =~ /^Rev: (\d+) /)[0];
11
12=head1 NAME
13
14LATMOS::Accounts::Bases::Sql::Aliases - Mail alias object
15
16=cut
17
18sub _object_table { 'aliases' }
19
20sub _key_field { 'name' }
21
22sub _has_extended_attributes { 1 }
23
24sub _get_attr_schema {
25    my ($class, $base) = @_;
26
27    $class->SUPER::_get_attr_schema($base,
28        {
29            name            => {
30                ro => 1,
31            },
32            forward         => {
33                mandatory => 1,
34                multiple => 1,
35                monitored => 1,
36            },
37            finalpoint      => { ro => 1, multiple => 1 },
38            parents         => { ro => 1, },
39            anyparents      => { ro => 1, },
40            sameforward     => { ro => 1, multiple => 1 },
41            samedestination => { ro => 1, multiple => 1 },
42            user            => {
43                ro => 1,
44                reference => 'user',
45            },
46            expire    => {
47                inline => 1,
48                formtype => 'DATE',
49            },
50        }
51    )
52}
53
54sub new {
55    my ($class, $base, $id) = @_;
56    my $sth = $base->db->prepare_cached(q{
57        select name from aliases where lower(name) = lower(?)
58    });
59    $sth->execute($id);
60    my $res = $sth->fetchrow_hashref;
61    $sth->finish;
62    if ($res) {
63        $class->SUPER::new($base, $res->{name});
64    } else {
65        return;
66    }
67}
68
69sub _create {
70    my ($class, $base, $id, %data) = @_;
71    $data{'forward'} = [ $data{'forward'} ] unless(ref $data{'forward'});
72    my $sth = $base->db->prepare_cached(q{
73        select name from aliases where lower(name) = lower(?)
74        limit 1
75        });
76    $sth->execute($id);
77    my $res = $sth->fetchrow_hashref;
78    $sth->finish;
79    if ($res) {
80        $base->log(LA_ERR, "Aliases %s already exists as %s",
81            $id, $res->{name});
82        return;
83    } else {
84        return $class->SUPER::_create($base, $id, %data);
85    }
86}
87
88sub get_field {
89    my ($self, $field) = @_; 
90    if ($field eq 'finalpoint') {
91        my $sth = $self->db->prepare_cached(q{
92        select forward from aliases where lower(name) = lower(?)
93        limit 1
94        });
95        my @res = ();
96        my @next = @{ $self->get_field('forward') || [] };
97        while (my $next = shift(@next)) {
98            $sth->execute($next);
99            if (my $res = $sth->fetchrow_hashref) {
100                push(@next, @{ $res->{forward} });
101            } else {
102                push(@res, $next);
103            }
104        }
105        $sth->finish;
106        return @res ? \@res : undef;
107    }
108    if ($field =~ /^(parents|anyparents|sameforward|samedestination)$/) {
109        my $sth = $self->db->prepare_cached(q{
110            select name from aliases where array[lower($1)] <@
111                string_to_array(lower(array_to_string("forward", ',')), ',')
112            });
113        my @tocheck = ($field =~ /^sameforward$/
114            ? $self->get_field('forward')
115            : ($field =~ /^samedestination$/ 
116                ? $self->get_field('finalpoint')
117                : $self->id));
118        my @results;
119        my @already;
120        while (my $next = shift(@tocheck)) {
121            # endless loop detection
122            grep { $next eq $_ } @already and do {
123                push(@results, 'LOOP');
124                next;
125            };
126            push(@already, $next);
127            $sth->execute($next);
128            while (my $res = $sth->fetchrow_hashref) {
129                push(@tocheck, $res->{name});
130                $res->{name} eq $self->id and next;
131                push(@results, $res->{name});
132            }
133            $field =~ /^(parents|sameforward)$/ and last; # No recursive search
134        }
135        return scalar(@results) <= 1 ? $results[0] : \@results;
136    }
137    $self->SUPER::get_field($field);
138}
139
140sub set_fields {
141    my ($self, %attrs) = @_;
142    foreach (keys %attrs) {
143        /^forward$/ and do {
144            $attrs{$_} = [ $attrs{$_} ] unless(ref $attrs{$_});
145        };
146    }
147    $self->SUPER::set_fields(%attrs);
148}
149
1501;
151
152__END__
153
154=head1 SEE ALSO
155
156L<LATMOS::Accounts::Bases::Sql>
157
158L<LATMOS::Accounts::Bases::Sql::Revaliases>
159
160=head1 AUTHOR
161
162Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
163
164=head1 COPYRIGHT AND LICENSE
165
166Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
167
168This library is free software; you can redistribute it and/or modify
169it under the same terms as Perl itself, either Perl version 5.10.0 or,
170at your option, any later version of Perl 5 you may have available.
171
172
173=cut
Note: See TracBrowser for help on using the repository browser.