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

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