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

Last change on this file since 213 was 213, checked in by nanardon, 15 years ago

rename field in db, making it coherent

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