source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Sql/User.pm @ 251

Last change on this file since 251 was 251, checked in by nanardon, 15 years ago
  • try to improve fields discovery in DB
  • Property svn:keywords set to Id Rev
File size: 6.9 KB
Line 
1package LATMOS::Accounts::Bases::Sql::User;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use LATMOS::Accounts::Utils;
8use base qw(LATMOS::Accounts::Bases::Sql::objects);
9
10our $VERSION = (q$Rev$ =~ /^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 { 'user' }
41
42sub key_field { 'name' }
43
44sub has_extended_attributes { 1 }
45
46sub _delayed_fields {
47    my ($self)= @_;
48    return qw(memberOf manager directReports);
49}
50
51sub _initials_fields {
52    return {
53        uidNumber => 'uidnumber',
54        gidNumber => 'gidnumber',
55    }
56}
57
58sub _inline_fields {
59    my ($self, $for, $base) = @_;
60    return {
61        %{ $self->SUPER::_inline_fields($for, $base) },
62        memberOf        => 'memberOf',
63        # uidNumber => 'uidnumber',
64        # gidNumber => 'gidnumber',
65        (($for !~ /w/) ? (
66        uid => 'name',
67        gecos        => 'gecos',
68        displayName  => 'displayName',
69        sAMAccountName  => 'sAMAccountName',
70        accountExpires => 'accountExpires',
71        shadowExpire => 'shadowExpire',
72        directReports => 'directReports',
73        managedObjects => 'managedObjects',
74        department => 'department',
75        ) : ()),
76    }
77}
78
79sub get_field {
80    my ($self, $field) = @_;
81    if ($field eq 'gecos') {
82        return to_ascii(
83            join(' ', grep { $_ } ($self->get_c_field('givenName'), ($self->get_c_field('sn'))))
84        ) || to_ascii($self->get_c_field('description'));
85    } elsif ($field eq 'displayName') {
86        return join(' ', grep { $_ } ($self->get_c_field('givenName'), ($self->get_c_field('sn'))))
87            || $self->id;
88    } elsif ($field eq 'sAMAccountName') {
89        return $self->id;
90    } elsif ($field eq 'memberOf') {
91        my $sth = $self->db->prepare_cached(
92            q{
93            select name from "group" join
94            group_attributes_users on group_attributes_users.okey = "group".ikey
95            where value = ? and attr = ?
96            }
97        );
98        $sth->execute($self->id, 'memberUID');
99        my @res;
100        while (my $res = $sth->fetchrow_hashref) {
101            push(@res, $res->{name});
102        }
103        return \@res;
104    } elsif ($field eq 'directReports') {
105        my $sth = $self->db->prepare_cached(
106            q{
107            select name from "user" join
108            user_attributes on user_attributes.okey = "user".ikey
109            where value = ? and attr = ?
110            }
111        );
112        $sth->execute($self->id, 'manager');
113        my @res;
114        while (my $res = $sth->fetchrow_hashref) {
115            push(@res, $res->{name});
116        }
117        return \@res;
118    } elsif ($field eq 'department') {
119        my $sth = $self->db->prepare_cached(
120            q{
121            select name from "group" join
122            group_attributes on "group".ikey = group_attributes.okey
123            where okey in (select okey from group_attributes_users where value = ?)
124            and attr = ? and value = ?
125            }
126        );
127        $sth->execute($self->id, 'sutype', 'dpmt');
128        my @res;
129        while (my $res = $sth->fetchrow_hashref) {
130            push(@res, $res->{name});
131        }
132        return join(',', @res);
133    } elsif ($field eq 'managedObjects') {
134        my $sth = $self->db->prepare_cached(
135            q{
136            select name from "group" join
137            group_attributes on group_attributes.okey = "group".ikey
138            where value = ? and attr = ?
139            }
140        );
141        $sth->execute($self->id, 'managedBy');
142        my @res;
143        while (my $res = $sth->fetchrow_hashref) {
144            push(@res, $res->{name});
145        }
146        return \@res;
147    } elsif ($field eq 'accountExpires') {
148        my $sth = $self->db->prepare_cached(
149            sprintf(
150                q{select extract(epoch from expire) + 11644474161 as expire
151                from %s where %s = ?},
152                $self->db->quote_identifier($self->object_table),
153                $self->db->quote_identifier($self->key_field),
154            )
155        );
156        $sth->execute($self->id);
157        my $res = $sth->fetchrow_hashref;
158        $sth->finish;
159        return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807';
160    } elsif ($field eq 'shadowExpire') {
161        my $sth = $self->db->prepare_cached(
162            sprintf(
163                q{select justify_hours(expire - '1/1/1970'::timestamp) as expire
164                from %s where %s = ?},
165                $self->db->quote_identifier($self->object_table),
166                $self->db->quote_identifier($self->key_field),
167            )
168        );
169        $sth->execute($self->id);
170        my $res = $sth->fetchrow_hashref;
171        $sth->finish;
172        return -1 unless($res->{expire});
173        $res->{expire} =~ /(\d+) days\s*(\w)?/;
174        return $1 + ($2 ? 1 : 0);
175    } else {
176        return $self->SUPER::get_field($field);
177    }
178}
179
180sub set_fields {
181    my ($self, %data) = @_;
182    my %fdata;
183    foreach my $attr (keys %data) {
184        $attr =~ /^memberOf$/ and do {
185            my %member;
186            foreach (@{ $self->get_field('memberOf') }) {
187                $member{$_}{c} = 1;
188            }
189            foreach (@{ $data{$attr} || []}) {
190                $member{$_}{n} = 1;
191            }
192
193            foreach (keys %member) {
194                $member{$_}{c} && $member{$_}{n} and next; # no change !
195                my $group = $self->base->get_object('group', $_) or next;
196                if ($member{$_}{n}) {
197                    my $sth = $self->db->prepare_cached(
198                        q{insert into group_attributes_users (value, attr, okey) values (?,?,?)}
199                    );
200                    $sth->execute($self->id, 'memberUID', $group->_get_ikey);
201                } elsif ($member{$_}{c}) {
202                    my $sth = $self->db->prepare_cached(
203                        q{delete from group_attributes_users where value = ? and attr = ? and okey = ?}
204                    );
205                    $sth->execute($self->id, 'memberUID', $group->_get_ikey);
206                } # else {} # can't happend
207            }
208            next;
209        };
210        $fdata{$attr} = $data{$attr} || undef;
211    }
212    if (keys %fdata) {
213        $self->SUPER::set_fields(%fdata);
214    }
215}
216
217
2181;
219
220__END__
221
222=head1 SEE ALSO
223
224=head1 AUTHOR
225
226Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
227
228=head1 COPYRIGHT AND LICENSE
229
230Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
231
232This library is free software; you can redistribute it and/or modify
233it under the same terms as Perl itself, either Perl version 5.10.0 or,
234at your option, any later version of Perl 5 you may have available.
235
236
237=cut
Note: See TracBrowser for help on using the repository browser.