[300] | 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); |
---|
[369] | 8 | use LATMOS::Accounts::Log; |
---|
[1551] | 9 | use LATMOS::Accounts::I18N; |
---|
[300] | 10 | |
---|
| 11 | our $VERSION = (q$Rev: 341 $ =~ /^Rev: (\d+) /)[0]; |
---|
| 12 | |
---|
| 13 | =head1 NAME |
---|
| 14 | |
---|
[1023] | 15 | LATMOS::Accounts::Bases::Sql::Aliases - Mail alias object |
---|
[300] | 16 | |
---|
| 17 | =cut |
---|
| 18 | |
---|
[1014] | 19 | sub _object_table { 'aliases' } |
---|
[300] | 20 | |
---|
[1014] | 21 | sub _key_field { 'name' } |
---|
[300] | 22 | |
---|
[1014] | 23 | sub _has_extended_attributes { 1 } |
---|
[300] | 24 | |
---|
[861] | 25 | sub _get_attr_schema { |
---|
| 26 | my ($class, $base) = @_; |
---|
| 27 | |
---|
[1315] | 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$/ |
---|
[1472] | 39 | ? $self->get_attributes('forward') |
---|
[1315] | 40 | : ($field =~ /^samedestination$/ |
---|
[1472] | 41 | ? $self->get_attributes('finalpoint') |
---|
[1315] | 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 { |
---|
[1424] | 48 | push(@results, $next); |
---|
[1315] | 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 | |
---|
[861] | 63 | $class->SUPER::_get_attr_schema($base, |
---|
| 64 | { |
---|
| 65 | name => { |
---|
| 66 | ro => 1, |
---|
| 67 | }, |
---|
| 68 | forward => { |
---|
[1550] | 69 | label => l('Forward'), |
---|
[1820] | 70 | delayed => 1, |
---|
[861] | 71 | mandatory => 1, |
---|
| 72 | multiple => 1, |
---|
[1297] | 73 | monitored => 1, |
---|
[1315] | 74 | set => sub { |
---|
| 75 | my ($self, $data) = @_; |
---|
[1844] | 76 | my @datas = $data |
---|
| 77 | ? (ref $data |
---|
| 78 | ? @$data |
---|
| 79 | : ($data)) |
---|
| 80 | : (); |
---|
| 81 | my $sql = @datas |
---|
| 82 | ? '{' . join(',', map { $_ =~ s/\\/\\\\/g; '"' .$_.'"' } @datas) . '}' |
---|
[1818] | 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) |
---|
[1315] | 88 | ); |
---|
[1821] | 89 | return 1; |
---|
[1315] | 90 | }, |
---|
[1490] | 91 | ro => sub { |
---|
| 92 | $_[0] && $_[0]->_get_c_field('autoMemberFilter') ? 1 : 0 |
---|
| 93 | }, |
---|
[861] | 94 | }, |
---|
[1493] | 95 | rfc822MailMember => { |
---|
| 96 | iname => 'forward', |
---|
| 97 | hidden => 1, |
---|
[1817] | 98 | ro => 1, |
---|
| 99 | multiple => 1, |
---|
[1493] | 100 | }, |
---|
[1315] | 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') || [] }; |
---|
[1424] | 112 | my %seen; |
---|
[1315] | 113 | while (my $next = shift(@next)) { |
---|
[1424] | 114 | # Avoiding loop: |
---|
| 115 | $seen{$next} and do { |
---|
| 116 | push(@res, $next); |
---|
| 117 | next; |
---|
| 118 | }; |
---|
| 119 | $seen{$next} = 1; |
---|
[1315] | 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 | }, |
---|
[1550] | 130 | label => l('Final recipient') |
---|
[1315] | 131 | }, |
---|
| 132 | parents => { |
---|
| 133 | ro => 1, |
---|
| 134 | get => $recurs, |
---|
[1550] | 135 | label => l('Parents') |
---|
[1315] | 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 | }, |
---|
[861] | 151 | user => { |
---|
| 152 | ro => 1, |
---|
| 153 | reference => 'user', |
---|
[1471] | 154 | multiple => 1, |
---|
[1550] | 155 | label => l('User'), |
---|
[861] | 156 | }, |
---|
[939] | 157 | expire => { |
---|
| 158 | inline => 1, |
---|
| 159 | formtype => 'DATE', |
---|
[1550] | 160 | label => l('Expire'), |
---|
[939] | 161 | }, |
---|
[1550] | 162 | comment => { |
---|
| 163 | label => l('Comment'), |
---|
| 164 | }, |
---|
[1490] | 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; |
---|
[1550] | 172 | }, |
---|
[1551] | 173 | label => l('Automatics filters'), |
---|
[1490] | 174 | }, |
---|
[1782] | 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 | }, |
---|
[1490] | 185 | autoExclude => { |
---|
| 186 | multiple => 1, |
---|
| 187 | reference => 'user', |
---|
[1550] | 188 | label => l('User excluded'), |
---|
[1490] | 189 | }, |
---|
[2256] | 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 | }, |
---|
[861] | 199 | } |
---|
| 200 | ) |
---|
[369] | 201 | } |
---|
| 202 | |
---|
[499] | 203 | sub new { |
---|
| 204 | my ($class, $base, $id) = @_; |
---|
| 205 | my $sth = $base->db->prepare_cached(q{ |
---|
| 206 | select name from aliases where lower(name) = lower(?) |
---|
| 207 | }); |
---|
| 208 | $sth->execute($id); |
---|
| 209 | my $res = $sth->fetchrow_hashref; |
---|
| 210 | $sth->finish; |
---|
| 211 | if ($res) { |
---|
| 212 | $class->SUPER::new($base, $res->{name}); |
---|
| 213 | } else { |
---|
| 214 | return; |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
[369] | 218 | sub _create { |
---|
| 219 | my ($class, $base, $id, %data) = @_; |
---|
[493] | 220 | $data{'forward'} = [ $data{'forward'} ] unless(ref $data{'forward'}); |
---|
[1727] | 221 | @{$data{'forward'}} = grep { $_ } @{$data{'forward'}}; |
---|
[369] | 222 | my $sth = $base->db->prepare_cached(q{ |
---|
| 223 | select name from aliases where lower(name) = lower(?) |
---|
| 224 | limit 1 |
---|
| 225 | }); |
---|
| 226 | $sth->execute($id); |
---|
| 227 | my $res = $sth->fetchrow_hashref; |
---|
[371] | 228 | $sth->finish; |
---|
[369] | 229 | if ($res) { |
---|
| 230 | $base->log(LA_ERR, "Aliases %s already exists as %s", |
---|
| 231 | $id, $res->{name}); |
---|
| 232 | return; |
---|
| 233 | } else { |
---|
[370] | 234 | return $class->SUPER::_create($base, $id, %data); |
---|
[369] | 235 | } |
---|
| 236 | } |
---|
| 237 | |
---|
[1385] | 238 | sub set_fields { |
---|
| 239 | my ($self, %data) = @_; |
---|
[369] | 240 | |
---|
[1385] | 241 | if ($data{forward}) { |
---|
| 242 | $data{forward} = ref $data{forward} |
---|
| 243 | ? $data{forward} |
---|
| 244 | : [ $data{forward} ]; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | $self->SUPER::set_fields(%data); |
---|
| 248 | } |
---|
| 249 | |
---|
[1490] | 250 | =head2 populate_dyn_aliases |
---|
[1385] | 251 | |
---|
[1490] | 252 | Synchronise aliase's forward according filter set into C<autoMemberFilter> attribute. |
---|
| 253 | |
---|
| 254 | =cut |
---|
| 255 | |
---|
| 256 | sub populate_dyn_aliases { |
---|
| 257 | my ($self) = @_; |
---|
| 258 | |
---|
| 259 | |
---|
[1782] | 260 | if (!$self->get_field('autoMemberFilter') |
---|
| 261 | && !$self->get_field('autoFromSutype') ) { |
---|
[1490] | 262 | return 0; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | $self->base->log(LA_DEBUG, |
---|
[1627] | 266 | "Populating aliases %s from autoMemberFilter attribute", |
---|
[1490] | 267 | $self->id |
---|
| 268 | ); |
---|
[1729] | 269 | |
---|
| 270 | my $listh = $self->base->db->prepare_cached(q{ |
---|
| 271 | SELECT forward FROM aliases WHERE ikey = ? |
---|
| 272 | }); |
---|
| 273 | |
---|
| 274 | $listh->execute($self->Iid); |
---|
| 275 | my $res = $listh->fetchrow_hashref; |
---|
| 276 | $listh->finish; |
---|
| 277 | my $currentForward = $res->{forward} || []; |
---|
| 278 | |
---|
| 279 | my %email = $self->base->attributes_summary_by_object('user', 'mail'); |
---|
| 280 | |
---|
[2256] | 281 | my @UserList = $self->_filterToUser(); |
---|
[1490] | 282 | |
---|
[1497] | 283 | my %forward = (); |
---|
[1490] | 284 | |
---|
[2256] | 285 | foreach my $user (@UserList) { |
---|
[1490] | 286 | $email{$user} or next; |
---|
| 287 | my $mail = $email{$user}->[0] or next; |
---|
[2256] | 288 | $forward{ lc($mail) } = 1; |
---|
[1490] | 289 | } |
---|
| 290 | |
---|
[2256] | 291 | my $autoAddEmail = $self->get_field('autoAddEmail'); |
---|
| 292 | |
---|
| 293 | foreach my $mail (grep { $_ } (ref $autoAddEmail ? @$autoAddEmail : $autoAddEmail)) { |
---|
| 294 | $forward{ lc($mail) } = 1; |
---|
| 295 | } |
---|
| 296 | |
---|
[1729] | 297 | if (join(',', sort @$currentForward) eq join(',', sort keys %forward)) { |
---|
| 298 | $self->base->log( |
---|
| 299 | LA_DEBUG, |
---|
| 300 | "No need to update alias %s for autoMemberFilter settings", |
---|
| 301 | $self->id, |
---|
| 302 | ); |
---|
| 303 | return 0; |
---|
| 304 | } |
---|
| 305 | |
---|
[1490] | 306 | my $sth = $self->base->db->prepare_cached( |
---|
| 307 | 'UPDATE aliases SET forward = ? WHERE ikey = ?' |
---|
| 308 | ); |
---|
[1497] | 309 | if (!$sth->execute([ sort keys %forward ], $self->Iid)) { |
---|
[1490] | 310 | $self->base->log(LA_ERR, "Cannot update forward for aliases %s: %s", $self->id, $sth->errstr()); |
---|
| 311 | return; |
---|
| 312 | } |
---|
| 313 | |
---|
| 314 | return 1; |
---|
| 315 | } |
---|
| 316 | |
---|
[300] | 317 | 1; |
---|
| 318 | |
---|
| 319 | __END__ |
---|
| 320 | |
---|
| 321 | =head1 SEE ALSO |
---|
| 322 | |
---|
[1023] | 323 | L<LATMOS::Accounts::Bases::Sql> |
---|
| 324 | |
---|
| 325 | L<LATMOS::Accounts::Bases::Sql::Revaliases> |
---|
| 326 | |
---|
[300] | 327 | =head1 AUTHOR |
---|
| 328 | |
---|
| 329 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 330 | |
---|
| 331 | =head1 COPYRIGHT AND LICENSE |
---|
| 332 | |
---|
| 333 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
| 334 | |
---|
| 335 | This library is free software; you can redistribute it and/or modify |
---|
| 336 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 337 | at your option, any later version of Perl 5 you may have available. |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | =cut |
---|