[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'), |
---|
[861] | 70 | mandatory => 1, |
---|
| 71 | multiple => 1, |
---|
[1297] | 72 | monitored => 1, |
---|
[1315] | 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 | }, |
---|
[1490] | 81 | ro => sub { |
---|
| 82 | $_[0] && $_[0]->_get_c_field('autoMemberFilter') ? 1 : 0 |
---|
| 83 | }, |
---|
[861] | 84 | }, |
---|
[1493] | 85 | rfc822MailMember => { |
---|
| 86 | iname => 'forward', |
---|
| 87 | hidden => 1, |
---|
| 88 | }, |
---|
[1315] | 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') || [] }; |
---|
[1424] | 100 | my %seen; |
---|
[1315] | 101 | while (my $next = shift(@next)) { |
---|
[1424] | 102 | # Avoiding loop: |
---|
| 103 | $seen{$next} and do { |
---|
| 104 | push(@res, $next); |
---|
| 105 | next; |
---|
| 106 | }; |
---|
| 107 | $seen{$next} = 1; |
---|
[1315] | 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 | }, |
---|
[1550] | 118 | label => l('Final recipient') |
---|
[1315] | 119 | }, |
---|
| 120 | parents => { |
---|
| 121 | ro => 1, |
---|
| 122 | get => $recurs, |
---|
[1550] | 123 | label => l('Parents') |
---|
[1315] | 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 | }, |
---|
[861] | 139 | user => { |
---|
| 140 | ro => 1, |
---|
| 141 | reference => 'user', |
---|
[1471] | 142 | multiple => 1, |
---|
[1550] | 143 | label => l('User'), |
---|
[861] | 144 | }, |
---|
[939] | 145 | expire => { |
---|
| 146 | inline => 1, |
---|
| 147 | formtype => 'DATE', |
---|
[1550] | 148 | label => l('Expire'), |
---|
[939] | 149 | }, |
---|
[1550] | 150 | comment => { |
---|
| 151 | label => l('Comment'), |
---|
| 152 | }, |
---|
[1490] | 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; |
---|
[1550] | 160 | }, |
---|
[1551] | 161 | label => l('Automatics filters'), |
---|
[1490] | 162 | }, |
---|
| 163 | autoExclude => { |
---|
| 164 | multiple => 1, |
---|
| 165 | reference => 'user', |
---|
[1550] | 166 | label => l('User excluded'), |
---|
[1490] | 167 | }, |
---|
[861] | 168 | } |
---|
| 169 | ) |
---|
[369] | 170 | } |
---|
| 171 | |
---|
[499] | 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 | |
---|
[369] | 187 | sub _create { |
---|
| 188 | my ($class, $base, $id, %data) = @_; |
---|
[493] | 189 | $data{'forward'} = [ $data{'forward'} ] unless(ref $data{'forward'}); |
---|
[369] | 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; |
---|
[371] | 196 | $sth->finish; |
---|
[369] | 197 | if ($res) { |
---|
| 198 | $base->log(LA_ERR, "Aliases %s already exists as %s", |
---|
| 199 | $id, $res->{name}); |
---|
| 200 | return; |
---|
| 201 | } else { |
---|
[370] | 202 | return $class->SUPER::_create($base, $id, %data); |
---|
[369] | 203 | } |
---|
| 204 | } |
---|
| 205 | |
---|
[1385] | 206 | sub set_fields { |
---|
| 207 | my ($self, %data) = @_; |
---|
[369] | 208 | |
---|
[1385] | 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 | |
---|
[1490] | 218 | =head2 populate_dyn_aliases |
---|
[1385] | 219 | |
---|
[1490] | 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 | |
---|
[1497] | 244 | my %forward = (); |
---|
[1490] | 245 | |
---|
| 246 | foreach my $user (@users) { |
---|
| 247 | $exclude{$user} and next; |
---|
| 248 | $email{$user} or next; |
---|
| 249 | my $mail = $email{$user}->[0] or next; |
---|
[1501] | 250 | $forward{$mail} = 1; |
---|
[1490] | 251 | } |
---|
| 252 | |
---|
| 253 | my $sth = $self->base->db->prepare_cached( |
---|
| 254 | 'UPDATE aliases SET forward = ? WHERE ikey = ?' |
---|
| 255 | ); |
---|
[1497] | 256 | if (!$sth->execute([ sort keys %forward ], $self->Iid)) { |
---|
[1490] | 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 | |
---|
[300] | 264 | 1; |
---|
| 265 | |
---|
| 266 | __END__ |
---|
| 267 | |
---|
| 268 | =head1 SEE ALSO |
---|
| 269 | |
---|
[1023] | 270 | L<LATMOS::Accounts::Bases::Sql> |
---|
| 271 | |
---|
| 272 | L<LATMOS::Accounts::Bases::Sql::Revaliases> |
---|
| 273 | |
---|
[300] | 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 |
---|