[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 | }, |
---|
[861] | 190 | } |
---|
| 191 | ) |
---|
[369] | 192 | } |
---|
| 193 | |
---|
[499] | 194 | sub new { |
---|
| 195 | my ($class, $base, $id) = @_; |
---|
| 196 | my $sth = $base->db->prepare_cached(q{ |
---|
| 197 | select name from aliases where lower(name) = lower(?) |
---|
| 198 | }); |
---|
| 199 | $sth->execute($id); |
---|
| 200 | my $res = $sth->fetchrow_hashref; |
---|
| 201 | $sth->finish; |
---|
| 202 | if ($res) { |
---|
| 203 | $class->SUPER::new($base, $res->{name}); |
---|
| 204 | } else { |
---|
| 205 | return; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
[369] | 209 | sub _create { |
---|
| 210 | my ($class, $base, $id, %data) = @_; |
---|
[493] | 211 | $data{'forward'} = [ $data{'forward'} ] unless(ref $data{'forward'}); |
---|
[1727] | 212 | @{$data{'forward'}} = grep { $_ } @{$data{'forward'}}; |
---|
[369] | 213 | my $sth = $base->db->prepare_cached(q{ |
---|
| 214 | select name from aliases where lower(name) = lower(?) |
---|
| 215 | limit 1 |
---|
| 216 | }); |
---|
| 217 | $sth->execute($id); |
---|
| 218 | my $res = $sth->fetchrow_hashref; |
---|
[371] | 219 | $sth->finish; |
---|
[369] | 220 | if ($res) { |
---|
| 221 | $base->log(LA_ERR, "Aliases %s already exists as %s", |
---|
| 222 | $id, $res->{name}); |
---|
| 223 | return; |
---|
| 224 | } else { |
---|
[370] | 225 | return $class->SUPER::_create($base, $id, %data); |
---|
[369] | 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
[1385] | 229 | sub set_fields { |
---|
| 230 | my ($self, %data) = @_; |
---|
[369] | 231 | |
---|
[1385] | 232 | if ($data{forward}) { |
---|
| 233 | $data{forward} = ref $data{forward} |
---|
| 234 | ? $data{forward} |
---|
| 235 | : [ $data{forward} ]; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | $self->SUPER::set_fields(%data); |
---|
| 239 | } |
---|
| 240 | |
---|
[1490] | 241 | =head2 populate_dyn_aliases |
---|
[1385] | 242 | |
---|
[1490] | 243 | Synchronise aliase's forward according filter set into C<autoMemberFilter> attribute. |
---|
| 244 | |
---|
| 245 | =cut |
---|
| 246 | |
---|
| 247 | sub populate_dyn_aliases { |
---|
| 248 | my ($self) = @_; |
---|
| 249 | |
---|
| 250 | |
---|
[1782] | 251 | if (!$self->get_field('autoMemberFilter') |
---|
| 252 | && !$self->get_field('autoFromSutype') ) { |
---|
[1490] | 253 | return 0; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | $self->base->log(LA_DEBUG, |
---|
[1627] | 257 | "Populating aliases %s from autoMemberFilter attribute", |
---|
[1490] | 258 | $self->id |
---|
| 259 | ); |
---|
[1729] | 260 | |
---|
| 261 | my $listh = $self->base->db->prepare_cached(q{ |
---|
| 262 | SELECT forward FROM aliases WHERE ikey = ? |
---|
| 263 | }); |
---|
| 264 | |
---|
| 265 | $listh->execute($self->Iid); |
---|
| 266 | my $res = $listh->fetchrow_hashref; |
---|
| 267 | $listh->finish; |
---|
| 268 | my $currentForward = $res->{forward} || []; |
---|
| 269 | |
---|
| 270 | my %email = $self->base->attributes_summary_by_object('user', 'mail'); |
---|
[1782] | 271 | my %users; |
---|
[1729] | 272 | |
---|
[1490] | 273 | my $autoex = $self->get_field('autoExclude'); |
---|
| 274 | my %exclude = map { $_ => 1 } grep { $_ } (ref $autoex ? @$autoex : $autoex); |
---|
| 275 | |
---|
[1782] | 276 | if (my $filter = $self->get_field('autoMemberFilter')) { |
---|
[1904] | 277 | $users{$_} = 1 foreach( $self->base->search_objects( |
---|
| 278 | 'user', |
---|
| 279 | 'oalias=NULL', |
---|
| 280 | ref $filter ? @{ $filter } : $filter) |
---|
| 281 | ); |
---|
[1782] | 282 | } |
---|
[1490] | 283 | |
---|
[1782] | 284 | if (my $filter = $self->get_field('autoFromSutype')) { |
---|
| 285 | my @suTypeFilter = map { "sutype=$_" } (ref $filter ? @{ $filter } : $filter); |
---|
| 286 | foreach my $group ($self->base->search_objects('group', @suTypeFilter)) { |
---|
[1904] | 287 | foreach ($self->base->search_objects('user', 'oalias=NULL', "memberOf=$group")) { |
---|
[1782] | 288 | $users{$_} = 1; |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | } |
---|
| 292 | |
---|
[1497] | 293 | my %forward = (); |
---|
[1490] | 294 | |
---|
[1782] | 295 | foreach my $user (keys %users) { |
---|
[1490] | 296 | $exclude{$user} and next; |
---|
| 297 | $email{$user} or next; |
---|
| 298 | my $mail = $email{$user}->[0] or next; |
---|
[1501] | 299 | $forward{$mail} = 1; |
---|
[1490] | 300 | } |
---|
| 301 | |
---|
[1729] | 302 | if (join(',', sort @$currentForward) eq join(',', sort keys %forward)) { |
---|
| 303 | $self->base->log( |
---|
| 304 | LA_DEBUG, |
---|
| 305 | "No need to update alias %s for autoMemberFilter settings", |
---|
| 306 | $self->id, |
---|
| 307 | ); |
---|
| 308 | return 0; |
---|
| 309 | } |
---|
| 310 | |
---|
[1490] | 311 | my $sth = $self->base->db->prepare_cached( |
---|
| 312 | 'UPDATE aliases SET forward = ? WHERE ikey = ?' |
---|
| 313 | ); |
---|
[1497] | 314 | if (!$sth->execute([ sort keys %forward ], $self->Iid)) { |
---|
[1490] | 315 | $self->base->log(LA_ERR, "Cannot update forward for aliases %s: %s", $self->id, $sth->errstr()); |
---|
| 316 | return; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | return 1; |
---|
| 320 | } |
---|
| 321 | |
---|
[300] | 322 | 1; |
---|
| 323 | |
---|
| 324 | __END__ |
---|
| 325 | |
---|
| 326 | =head1 SEE ALSO |
---|
| 327 | |
---|
[1023] | 328 | L<LATMOS::Accounts::Bases::Sql> |
---|
| 329 | |
---|
| 330 | L<LATMOS::Accounts::Bases::Sql::Revaliases> |
---|
| 331 | |
---|
[300] | 332 | =head1 AUTHOR |
---|
| 333 | |
---|
| 334 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
| 335 | |
---|
| 336 | =head1 COPYRIGHT AND LICENSE |
---|
| 337 | |
---|
| 338 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
| 339 | |
---|
| 340 | This library is free software; you can redistribute it and/or modify |
---|
| 341 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 342 | at your option, any later version of Perl 5 you may have available. |
---|
| 343 | |
---|
| 344 | |
---|
| 345 | =cut |
---|