1 | package LATMOS::Accounts::Bases::Sql::Aliases; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
8 | use LATMOS::Accounts::Log; |
---|
9 | use LATMOS::Accounts::I18N; |
---|
10 | |
---|
11 | our $VERSION = (q$Rev: 341 $ =~ /^Rev: (\d+) /)[0]; |
---|
12 | |
---|
13 | =head1 NAME |
---|
14 | |
---|
15 | LATMOS::Accounts::Bases::Sql::Aliases - Mail alias object |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | sub _object_table { 'aliases' } |
---|
20 | |
---|
21 | sub _key_field { 'name' } |
---|
22 | |
---|
23 | sub _has_extended_attributes { 1 } |
---|
24 | |
---|
25 | sub _get_attr_schema { |
---|
26 | my ($class, $base) = @_; |
---|
27 | |
---|
28 | |
---|
29 | my $recurs = sub { |
---|
30 | my ($attr) = @_; |
---|
31 | my ($self) = $attr->object; |
---|
32 | my $field = $attr->name; |
---|
33 | |
---|
34 | my $sth = $self->db->prepare_cached(q{ |
---|
35 | select name from aliases where array[lower($1)] <@ |
---|
36 | string_to_array(lower(array_to_string("forward", ',')), ',') |
---|
37 | }); |
---|
38 | my @tocheck = ($field =~ /^sameforward$/ |
---|
39 | ? $self->get_attributes('forward') |
---|
40 | : ($field =~ /^samedestination$/ |
---|
41 | ? $self->get_attributes('finalpoint') |
---|
42 | : $self->id)); |
---|
43 | my @results; |
---|
44 | my @already; |
---|
45 | while (my $next = shift(@tocheck)) { |
---|
46 | # endless loop detection |
---|
47 | grep { $next eq $_ } @already and do { |
---|
48 | push(@results, $next); |
---|
49 | next; |
---|
50 | }; |
---|
51 | push(@already, $next); |
---|
52 | $sth->execute($next); |
---|
53 | while (my $res = $sth->fetchrow_hashref) { |
---|
54 | push(@tocheck, $res->{name}); |
---|
55 | $res->{name} eq $self->id and next; |
---|
56 | push(@results, $res->{name}); |
---|
57 | } |
---|
58 | $field =~ /^(parents|sameforward)$/ and last; # No recursive search |
---|
59 | } |
---|
60 | return scalar(@results) <= 1 ? $results[0] : \@results; |
---|
61 | }; |
---|
62 | |
---|
63 | $class->SUPER::_get_attr_schema($base, |
---|
64 | { |
---|
65 | name => { |
---|
66 | ro => 1, |
---|
67 | }, |
---|
68 | forward => { |
---|
69 | label => l('Forward'), |
---|
70 | mandatory => 1, |
---|
71 | multiple => 1, |
---|
72 | monitored => 1, |
---|
73 | set => sub { |
---|
74 | my ($self, $data) = @_; |
---|
75 | return $self->object->set_fields($self->iname, |
---|
76 | $data |
---|
77 | ? ref $data ? $data : [ $data ] |
---|
78 | : undef |
---|
79 | ); |
---|
80 | }, |
---|
81 | ro => sub { |
---|
82 | $_[0] && $_[0]->_get_c_field('autoMemberFilter') ? 1 : 0 |
---|
83 | }, |
---|
84 | }, |
---|
85 | rfc822MailMember => { |
---|
86 | iname => 'forward', |
---|
87 | hidden => 1, |
---|
88 | }, |
---|
89 | finalpoint => { |
---|
90 | ro => 1, |
---|
91 | multiple => 1, |
---|
92 | get => sub { |
---|
93 | my ($self) = @_; |
---|
94 | my $sth = $self->base->db->prepare_cached(q{ |
---|
95 | select forward from aliases where lower(name) = lower(?) |
---|
96 | limit 1 |
---|
97 | }); |
---|
98 | my @res = (); |
---|
99 | my @next = @{ $self->object->get_field('forward') || [] }; |
---|
100 | my %seen; |
---|
101 | while (my $next = shift(@next)) { |
---|
102 | # Avoiding loop: |
---|
103 | $seen{$next} and do { |
---|
104 | push(@res, $next); |
---|
105 | next; |
---|
106 | }; |
---|
107 | $seen{$next} = 1; |
---|
108 | $sth->execute($next); |
---|
109 | if (my $res = $sth->fetchrow_hashref) { |
---|
110 | push(@next, @{ $res->{forward} }); |
---|
111 | } else { |
---|
112 | push(@res, $next); |
---|
113 | } |
---|
114 | } |
---|
115 | $sth->finish; |
---|
116 | return @res ? \@res : undef; |
---|
117 | }, |
---|
118 | label => l('Final recipient') |
---|
119 | }, |
---|
120 | parents => { |
---|
121 | ro => 1, |
---|
122 | get => $recurs, |
---|
123 | label => l('Parents') |
---|
124 | }, |
---|
125 | anyparents => { |
---|
126 | ro => 1, |
---|
127 | get => $recurs, |
---|
128 | }, |
---|
129 | sameforward => { |
---|
130 | ro => 1, |
---|
131 | multiple => 1, |
---|
132 | get => $recurs, |
---|
133 | }, |
---|
134 | samedestination => { |
---|
135 | ro => 1, |
---|
136 | multiple => 1, |
---|
137 | get => $recurs, |
---|
138 | }, |
---|
139 | user => { |
---|
140 | ro => 1, |
---|
141 | reference => 'user', |
---|
142 | multiple => 1, |
---|
143 | label => l('User'), |
---|
144 | }, |
---|
145 | expire => { |
---|
146 | inline => 1, |
---|
147 | formtype => 'DATE', |
---|
148 | label => l('Expire'), |
---|
149 | }, |
---|
150 | comment => { |
---|
151 | label => l('Comment'), |
---|
152 | }, |
---|
153 | autoMemberFilter => { |
---|
154 | multiple => 1, |
---|
155 | set => sub { |
---|
156 | my ($self, $data) = @_; |
---|
157 | $self->object->set_fields($self->name, $data) or return; |
---|
158 | $self->object->populate_dyn_aliases; |
---|
159 | return 1; |
---|
160 | }, |
---|
161 | label => l('Automatics filters'), |
---|
162 | }, |
---|
163 | autoExclude => { |
---|
164 | multiple => 1, |
---|
165 | reference => 'user', |
---|
166 | label => l('User excluded'), |
---|
167 | }, |
---|
168 | } |
---|
169 | ) |
---|
170 | } |
---|
171 | |
---|
172 | sub new { |
---|
173 | my ($class, $base, $id) = @_; |
---|
174 | my $sth = $base->db->prepare_cached(q{ |
---|
175 | select name from aliases where lower(name) = lower(?) |
---|
176 | }); |
---|
177 | $sth->execute($id); |
---|
178 | my $res = $sth->fetchrow_hashref; |
---|
179 | $sth->finish; |
---|
180 | if ($res) { |
---|
181 | $class->SUPER::new($base, $res->{name}); |
---|
182 | } else { |
---|
183 | return; |
---|
184 | } |
---|
185 | } |
---|
186 | |
---|
187 | sub _create { |
---|
188 | my ($class, $base, $id, %data) = @_; |
---|
189 | $data{'forward'} = [ $data{'forward'} ] unless(ref $data{'forward'}); |
---|
190 | my $sth = $base->db->prepare_cached(q{ |
---|
191 | select name from aliases where lower(name) = lower(?) |
---|
192 | limit 1 |
---|
193 | }); |
---|
194 | $sth->execute($id); |
---|
195 | my $res = $sth->fetchrow_hashref; |
---|
196 | $sth->finish; |
---|
197 | if ($res) { |
---|
198 | $base->log(LA_ERR, "Aliases %s already exists as %s", |
---|
199 | $id, $res->{name}); |
---|
200 | return; |
---|
201 | } else { |
---|
202 | return $class->SUPER::_create($base, $id, %data); |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | sub set_fields { |
---|
207 | my ($self, %data) = @_; |
---|
208 | |
---|
209 | if ($data{forward}) { |
---|
210 | $data{forward} = ref $data{forward} |
---|
211 | ? $data{forward} |
---|
212 | : [ $data{forward} ]; |
---|
213 | } |
---|
214 | |
---|
215 | $self->SUPER::set_fields(%data); |
---|
216 | } |
---|
217 | |
---|
218 | =head2 populate_dyn_aliases |
---|
219 | |
---|
220 | Synchronise aliase's forward according filter set into C<autoMemberFilter> attribute. |
---|
221 | |
---|
222 | =cut |
---|
223 | |
---|
224 | sub populate_dyn_aliases { |
---|
225 | my ($self) = @_; |
---|
226 | |
---|
227 | |
---|
228 | if (!$self->get_field('autoMemberFilter')) { |
---|
229 | return 0; |
---|
230 | } |
---|
231 | |
---|
232 | my %email = $self->base->attributes_summary_by_object('user', 'mail'); |
---|
233 | |
---|
234 | $self->base->log(LA_DEBUG, |
---|
235 | "Populating alaises %s from autoMemberFilter attribute", |
---|
236 | $self->id |
---|
237 | ); |
---|
238 | my $filter = $self->get_field('autoMemberFilter'); |
---|
239 | my $autoex = $self->get_field('autoExclude'); |
---|
240 | my %exclude = map { $_ => 1 } grep { $_ } (ref $autoex ? @$autoex : $autoex); |
---|
241 | |
---|
242 | my @users = $self->base->search_objects('user', ref $filter ? @{ $filter } : $filter); |
---|
243 | |
---|
244 | my %forward = (); |
---|
245 | |
---|
246 | foreach my $user (@users) { |
---|
247 | $exclude{$user} and next; |
---|
248 | $email{$user} or next; |
---|
249 | my $mail = $email{$user}->[0] or next; |
---|
250 | $forward{$mail} = 1; |
---|
251 | } |
---|
252 | |
---|
253 | my $sth = $self->base->db->prepare_cached( |
---|
254 | 'UPDATE aliases SET forward = ? WHERE ikey = ?' |
---|
255 | ); |
---|
256 | if (!$sth->execute([ sort keys %forward ], $self->Iid)) { |
---|
257 | $self->base->log(LA_ERR, "Cannot update forward for aliases %s: %s", $self->id, $sth->errstr()); |
---|
258 | return; |
---|
259 | } |
---|
260 | |
---|
261 | return 1; |
---|
262 | } |
---|
263 | |
---|
264 | 1; |
---|
265 | |
---|
266 | __END__ |
---|
267 | |
---|
268 | =head1 SEE ALSO |
---|
269 | |
---|
270 | L<LATMOS::Accounts::Bases::Sql> |
---|
271 | |
---|
272 | L<LATMOS::Accounts::Bases::Sql::Revaliases> |
---|
273 | |
---|
274 | =head1 AUTHOR |
---|
275 | |
---|
276 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
277 | |
---|
278 | =head1 COPYRIGHT AND LICENSE |
---|
279 | |
---|
280 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
281 | |
---|
282 | This library is free software; you can redistribute it and/or modify |
---|
283 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
284 | at your option, any later version of Perl 5 you may have available. |
---|
285 | |
---|
286 | |
---|
287 | =cut |
---|