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

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