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 | delayed => 1, |
---|
71 | mandatory => 1, |
---|
72 | multiple => 1, |
---|
73 | monitored => 1, |
---|
74 | set => sub { |
---|
75 | my ($self, $data) = @_; |
---|
76 | my @datas = $data |
---|
77 | ? (ref $data |
---|
78 | ? @$data |
---|
79 | : ($data)) |
---|
80 | : (); |
---|
81 | my $sql = @datas |
---|
82 | ? '{' . join(',', map { $_ =~ s/\\/\\\\/g; '"' .$_.'"' } @datas) . '}' |
---|
83 | : undef; |
---|
84 | $base->db->do( |
---|
85 | q{UPDATE aliases SET forward = } |
---|
86 | . $base->db->quote($sql) . q{ where name = } . |
---|
87 | $base->db->quote($self->object->id) |
---|
88 | ); |
---|
89 | return 1; |
---|
90 | }, |
---|
91 | ro => sub { |
---|
92 | $_[0] && $_[0]->_get_c_field('autoMemberFilter') ? 1 : 0 |
---|
93 | }, |
---|
94 | }, |
---|
95 | rfc822MailMember => { |
---|
96 | iname => 'forward', |
---|
97 | hidden => 1, |
---|
98 | ro => 1, |
---|
99 | multiple => 1, |
---|
100 | }, |
---|
101 | finalpoint => { |
---|
102 | ro => 1, |
---|
103 | multiple => 1, |
---|
104 | get => sub { |
---|
105 | my ($self) = @_; |
---|
106 | my $sth = $self->base->db->prepare_cached(q{ |
---|
107 | select forward from aliases where lower(name) = lower(?) |
---|
108 | limit 1 |
---|
109 | }); |
---|
110 | my @res = (); |
---|
111 | my @next = @{ $self->object->get_field('forward') || [] }; |
---|
112 | my %seen; |
---|
113 | while (my $next = shift(@next)) { |
---|
114 | # Avoiding loop: |
---|
115 | $seen{$next} and do { |
---|
116 | push(@res, $next); |
---|
117 | next; |
---|
118 | }; |
---|
119 | $seen{$next} = 1; |
---|
120 | $sth->execute($next); |
---|
121 | if (my $res = $sth->fetchrow_hashref) { |
---|
122 | push(@next, @{ $res->{forward} }); |
---|
123 | } else { |
---|
124 | push(@res, $next); |
---|
125 | } |
---|
126 | } |
---|
127 | $sth->finish; |
---|
128 | return @res ? \@res : undef; |
---|
129 | }, |
---|
130 | label => l('Final recipient') |
---|
131 | }, |
---|
132 | parents => { |
---|
133 | ro => 1, |
---|
134 | get => $recurs, |
---|
135 | label => l('Parents') |
---|
136 | }, |
---|
137 | anyparents => { |
---|
138 | ro => 1, |
---|
139 | get => $recurs, |
---|
140 | }, |
---|
141 | sameforward => { |
---|
142 | ro => 1, |
---|
143 | multiple => 1, |
---|
144 | get => $recurs, |
---|
145 | }, |
---|
146 | samedestination => { |
---|
147 | ro => 1, |
---|
148 | multiple => 1, |
---|
149 | get => $recurs, |
---|
150 | }, |
---|
151 | user => { |
---|
152 | ro => 1, |
---|
153 | reference => 'user', |
---|
154 | multiple => 1, |
---|
155 | label => l('User'), |
---|
156 | }, |
---|
157 | expire => { |
---|
158 | inline => 1, |
---|
159 | formtype => 'DATE', |
---|
160 | label => l('Expire'), |
---|
161 | }, |
---|
162 | comment => { |
---|
163 | label => l('Comment'), |
---|
164 | }, |
---|
165 | autoMemberFilter => { |
---|
166 | multiple => 1, |
---|
167 | set => sub { |
---|
168 | my ($self, $data) = @_; |
---|
169 | $self->object->set_fields($self->name, $data) or return; |
---|
170 | $self->object->populate_dyn_aliases; |
---|
171 | return 1; |
---|
172 | }, |
---|
173 | label => l('Automatics filters'), |
---|
174 | }, |
---|
175 | autoFromSutype => { |
---|
176 | multiple => 1, |
---|
177 | set => sub { |
---|
178 | my ($self, $data) = @_; |
---|
179 | $self->object->set_fields($self->name, $data) or return; |
---|
180 | $self->object->populate_dyn_aliases; |
---|
181 | return 1; |
---|
182 | }, |
---|
183 | label => l('From group type'), |
---|
184 | }, |
---|
185 | autoExclude => { |
---|
186 | multiple => 1, |
---|
187 | reference => 'user', |
---|
188 | label => l('User excluded'), |
---|
189 | }, |
---|
190 | autoInclude => { |
---|
191 | multiple => 1, |
---|
192 | reference => 'user', |
---|
193 | label => l('User included'), |
---|
194 | }, |
---|
195 | autoAddEmail => { |
---|
196 | multiple => 1, |
---|
197 | label => l('Extra email'), |
---|
198 | }, |
---|
199 | } |
---|
200 | ) |
---|
201 | } |
---|
202 | |
---|
203 | sub ParentObject { |
---|
204 | my ($self) = @_; |
---|
205 | |
---|
206 | if (my $user = $self->_get_attributes('user')) { |
---|
207 | return $self->base->get_object('user', $user); |
---|
208 | } else { |
---|
209 | return; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | sub new { |
---|
214 | my ($class, $base, $id) = @_; |
---|
215 | my $sth = $base->db->prepare_cached(q{ |
---|
216 | select name from aliases where lower(name) = lower(?) |
---|
217 | }); |
---|
218 | $sth->execute($id); |
---|
219 | my $res = $sth->fetchrow_hashref; |
---|
220 | $sth->finish; |
---|
221 | if ($res) { |
---|
222 | $class->SUPER::new($base, $res->{name}); |
---|
223 | } else { |
---|
224 | return; |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | sub _create { |
---|
229 | my ($class, $base, $id, %data) = @_; |
---|
230 | $data{'forward'} = [ $data{'forward'} ] unless(ref $data{'forward'}); |
---|
231 | @{$data{'forward'}} = grep { $_ } @{$data{'forward'}}; |
---|
232 | my $sth = $base->db->prepare_cached(q{ |
---|
233 | select name from aliases where lower(name) = lower(?) |
---|
234 | limit 1 |
---|
235 | }); |
---|
236 | $sth->execute($id); |
---|
237 | my $res = $sth->fetchrow_hashref; |
---|
238 | $sth->finish; |
---|
239 | if ($res) { |
---|
240 | $base->log(LA_ERR, "Aliases %s already exists as %s", |
---|
241 | $id, $res->{name}); |
---|
242 | return; |
---|
243 | } else { |
---|
244 | return $class->SUPER::_create($base, $id, %data); |
---|
245 | } |
---|
246 | } |
---|
247 | |
---|
248 | sub set_fields { |
---|
249 | my ($self, %data) = @_; |
---|
250 | |
---|
251 | if ($data{forward}) { |
---|
252 | $data{forward} = ref $data{forward} |
---|
253 | ? $data{forward} |
---|
254 | : [ $data{forward} ]; |
---|
255 | } |
---|
256 | |
---|
257 | $self->SUPER::set_fields(%data); |
---|
258 | } |
---|
259 | |
---|
260 | =head2 populate_dyn_aliases |
---|
261 | |
---|
262 | Synchronise aliase's forward according filter set into C<autoMemberFilter> attribute. |
---|
263 | |
---|
264 | =cut |
---|
265 | |
---|
266 | sub populate_dyn_aliases { |
---|
267 | my ($self) = @_; |
---|
268 | |
---|
269 | |
---|
270 | if (!$self->get_field('autoMemberFilter') |
---|
271 | && !$self->get_field('autoFromSutype') ) { |
---|
272 | return 0; |
---|
273 | } |
---|
274 | |
---|
275 | $self->base->log(LA_DEBUG, |
---|
276 | "Populating aliases %s from autoMemberFilter attribute", |
---|
277 | $self->id |
---|
278 | ); |
---|
279 | |
---|
280 | my $listh = $self->base->db->prepare_cached(q{ |
---|
281 | SELECT forward FROM aliases WHERE ikey = ? |
---|
282 | }); |
---|
283 | |
---|
284 | $listh->execute($self->Iid); |
---|
285 | my $res = $listh->fetchrow_hashref; |
---|
286 | $listh->finish; |
---|
287 | my $currentForward = $res->{forward} || []; |
---|
288 | |
---|
289 | my %email = $self->base->attributes_summary_by_object('user', 'mail'); |
---|
290 | |
---|
291 | my @UserList = $self->_filterToUser(); |
---|
292 | |
---|
293 | my %forward = (); |
---|
294 | |
---|
295 | foreach my $user (@UserList) { |
---|
296 | $email{$user} or next; |
---|
297 | my $mail = $email{$user}->[0] or next; |
---|
298 | $forward{ lc($mail) } = 1; |
---|
299 | } |
---|
300 | |
---|
301 | my $autoAddEmail = $self->get_field('autoAddEmail'); |
---|
302 | |
---|
303 | foreach my $mail (grep { $_ } (ref $autoAddEmail ? @$autoAddEmail : $autoAddEmail)) { |
---|
304 | $forward{ lc($mail) } = 1; |
---|
305 | } |
---|
306 | |
---|
307 | if (join(',', sort @$currentForward) eq join(',', sort keys %forward)) { |
---|
308 | $self->base->log( |
---|
309 | LA_DEBUG, |
---|
310 | "No need to update alias %s for autoMemberFilter settings", |
---|
311 | $self->id, |
---|
312 | ); |
---|
313 | return 0; |
---|
314 | } |
---|
315 | |
---|
316 | my $sth = $self->base->db->prepare_cached( |
---|
317 | 'UPDATE aliases SET forward = ? WHERE ikey = ?' |
---|
318 | ); |
---|
319 | if (!$sth->execute([ sort keys %forward ], $self->Iid)) { |
---|
320 | $self->base->log(LA_ERR, "Cannot update forward for aliases %s: %s", $self->id, $sth->errstr()); |
---|
321 | return; |
---|
322 | } |
---|
323 | |
---|
324 | return 1; |
---|
325 | } |
---|
326 | |
---|
327 | 1; |
---|
328 | |
---|
329 | __END__ |
---|
330 | |
---|
331 | =head1 SEE ALSO |
---|
332 | |
---|
333 | L<LATMOS::Accounts::Bases::Sql> |
---|
334 | |
---|
335 | L<LATMOS::Accounts::Bases::Sql::Revaliases> |
---|
336 | |
---|
337 | =head1 AUTHOR |
---|
338 | |
---|
339 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
340 | |
---|
341 | =head1 COPYRIGHT AND LICENSE |
---|
342 | |
---|
343 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
344 | |
---|
345 | This library is free software; you can redistribute it and/or modify |
---|
346 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
347 | at your option, any later version of Perl 5 you may have available. |
---|
348 | |
---|
349 | |
---|
350 | =cut |
---|