source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/Aliases.pm @ 462

Last change on this file since 462 was 462, checked in by nanardon, 15 years ago
  • manage several destinations in aliases
File size: 3.8 KB
Line 
1package LATMOS::Accounts::Bases::Sql::Aliases;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Sql::objects);
8use LATMOS::Accounts::Log;
9
10our $VERSION = (q$Rev: 341 $ =~ /^Rev: (\d+) /)[0];
11
12=head1 NAME
13
14LATMOS::Ad - Perl extension for blah blah blah
15
16=head1 SYNOPSIS
17
18  use LATMOS::Accounts::Bases;
19  my $base = LATMOS::Accounts::Bases->new('sql');
20  ...
21
22=head1 DESCRIPTION
23
24Account base access over standard unix file format.
25
26=head1 FUNCTIONS
27
28=cut
29
30=head2 new(%options)
31
32Create a new LATMOS::Ad object for windows AD $domain.
33
34domain / server: either the Ad domain or directly the server
35
36ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.
37
38=cut
39
40sub object_table { 'aliases' }
41
42sub key_field { 'name' }
43
44sub has_extended_attributes { 0 }
45
46sub _managed_fields {
47    my ($class, $for, $base) = @_;
48    return(
49        $for !~ /w/
50        ? (
51            finalpoint => 'finalpoint',
52            parents => 'parents',
53            anyparents => 'anyparents',
54            sameforward => 'sameforward',
55            samedestination => 'samedestination',
56        )
57        : ()
58    );
59}
60
61sub _create {
62    my ($class, $base, $id, %data) = @_;
63    my $sth = $base->db->prepare_cached(q{
64        select name from aliases where lower(name) = lower(?)
65        limit 1
66        });
67    $sth->execute($id);
68    my $res = $sth->fetchrow_hashref;
69    $sth->finish;
70    if ($res) {
71        $base->log(LA_ERR, "Aliases %s already exists as %s",
72            $id, $res->{name});
73        return;
74    } else {
75        return $class->SUPER::_create($base, $id, %data);
76    }
77}
78
79sub get_field {
80    my ($self, $field) = @_; 
81    if ($field eq 'finalpoint') {
82        my $sth = $self->db->prepare_cached(q{
83        select forward from aliases where lower(name) = lower(?)
84        limit 1
85        });
86        my $next = $self->get_field('forward');
87        while (1) {
88            $sth->execute($next);
89            if (my $res = $sth->fetchrow_hashref) {
90                $next = $res->{forward};
91                $next = ref $next
92                    ? join(',', @$next)
93                    : $next;
94                next;
95            } else {
96                last;
97            }
98        }
99        $sth->finish;
100        return $next;
101    }
102    if ($field =~ /^(parents|anyparents|sameforward|samedestination)$/) {
103        my $sth = $self->db->prepare_cached(q{
104            select name from aliases where array[lower($1)] <@
105                string_to_array(lower(array_to_string("forward", ',')), ',')
106            });
107        my @tocheck = ($field =~ /^sameforward$/
108            ? $self->get_field('forward')
109            : ($field =~ /^samedestination$/ 
110                ? $self->get_field('finalpoint')
111                : $self->id));
112        my @results;
113        my @already;
114        while (my $next = shift(@tocheck)) {
115            # endless loop detection
116            grep { $next eq $_ } @already and do {
117                push(@results, 'LOOP');
118                next;
119            };
120            push(@already, $next);
121            $sth->execute($next);
122            while (my $res = $sth->fetchrow_hashref) {
123                push(@tocheck, $res->{name});
124                $res->{name} eq $self->id and next;
125                push(@results, $res->{name});
126            }
127            $field =~ /^(parents|sameforward)$/ and last; # No recursive search
128        }
129        return scalar(@results) <= 1 ? $results[0] : \@results;
130    }
131    $self->SUPER::get_field($field);
132}
133
1341;
135
136__END__
137
138=head1 SEE ALSO
139
140=head1 AUTHOR
141
142Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
143
144=head1 COPYRIGHT AND LICENSE
145
146Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
147
148This library is free software; you can redistribute it and/or modify
149it under the same terms as Perl itself, either Perl version 5.10.0 or,
150at your option, any later version of Perl 5 you may have available.
151
152
153=cut
Note: See TracBrowser for help on using the repository browser.