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

Last change on this file since 158 was 158, checked in by nanardon, 15 years ago
  • change db schema about group management
  • Property svn:keywords set to Id Rev
File size: 5.0 KB
Line 
1package LATMOS::Accounts::Bases::Sql::User;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Sql::objects);
8
9our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
10
11=head1 NAME
12
13LATMOS::Ad - Perl extension for blah blah blah
14
15=head1 SYNOPSIS
16
17  use LATMOS::Accounts::Bases;
18  my $base = LATMOS::Accounts::Bases->new('sql');
19  ...
20
21=head1 DESCRIPTION
22
23Account base access over standard unix file format.
24
25=head1 FUNCTIONS
26
27=cut
28
29=head2 new(%options)
30
31Create a new LATMOS::Ad object for windows AD $domain.
32
33domain / server: either the Ad domain or directly the server
34
35ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>.
36
37=cut
38
39sub object_table { 'user' }
40
41sub key_field { 'uid' }
42
43sub has_extended_attributes { 1 }
44
45sub _delayed_fields {
46    my ($self)= @_;
47    return qw(memberOf manager);
48}
49
50sub _inline_fields {
51    my ($self, $for, $base) = @_;
52    return {
53        %{ $self->_initial_fields($for, $base) },
54        memberOf        => 'memberOf',
55        uidNumber => 'uidnumber',
56        gidNumber => 'gidnumber',
57        (($for !~ /w/) ? (
58        gecos        => 'gecos',
59        displayName  => 'displayName',
60        sAMAccountName  => 'sAMAccountName',
61        accountExpires => 'accountExpires',
62        shadowExpire => 'shadowExpire',
63        ) : ()),
64    }
65}
66
67sub get_field {
68    my ($self, $field) = @_;
69    if ($field eq 'gecos' || $field eq 'displayName') {
70        return join(' ', grep { $_ } ($self->get_c_field('givenName'), ($self->get_c_field('sn'))));
71    } elsif ($field =~ /^(sAMAccountName)$/) {
72        return $self->id,
73    } elsif ($field eq 'memberOf') {
74        my $sth = $self->db->prepare_cached(
75            q{
76            select value from user_attributes_groups
77            join user_attributes_list on
78            user_attributes_groups.attr = user_attributes_list.ikey
79            where id = ? and canonical = ?
80            }
81        );
82        $sth->execute($self->id, 'memberOf');
83        my @res;
84        while (my $res = $sth->fetchrow_hashref) {
85            push(@res, $res->{value});
86        }
87        return \@res;
88    } elsif ($field eq 'accountExpires') {
89        my $sth = $self->db->prepare_cached(
90            sprintf(
91                q{select extract(epoch from expire) + 11644474161 as expire
92                from %s where %s = ?},
93                $self->db->quote_identifier($self->object_table),
94                $self->db->quote_identifier($self->key_field),
95            )
96        );
97        $sth->execute($self->id);
98        my $res = $sth->fetchrow_hashref;
99        $sth->finish;
100        return $res->{expire} ? sprintf("%.f", $res->{expire} * 1E7) : '9223372036854775807';
101    } elsif ($field eq 'shadowExpire') {
102        my $sth = $self->db->prepare_cached(
103            sprintf(
104                q{select justify_hours(expire - '1/1/1970'::timestamp) as expire
105                from %s where %s = ?},
106                $self->db->quote_identifier($self->object_table),
107                $self->db->quote_identifier($self->key_field),
108            )
109        );
110        $sth->execute($self->id);
111        my $res = $sth->fetchrow_hashref;
112        $sth->finish;
113        return -1 unless($res->{expire});
114        $res->{expire} =~ /(\d+) days\s*(\w)?/;
115        return $1 + ($2 ? 1 : 0);
116    } else {
117        return $self->SUPER::get_field($field);
118    }
119}
120
121sub set_fields {
122    my ($self, %data) = @_;
123    my %fdata;
124    foreach my $attr (keys %data) {
125        $attr =~ /^memberOf$/ and do {
126            my $attrid = $self->_get_field_name_db('memberOf', $self->base) or next;
127            my %member;
128            foreach (@{ $self->get_field('memberOf') }) {
129                $member{$_}{c} = 1;
130            }
131            foreach (@{ $data{$attr} || []}) {
132                $member{$_}{n} = 1;
133            }
134
135            foreach (keys %member) {
136                $member{$_}{c} && $member{$_}{n} and next; # no change !
137                my $group = $self->base->get_object('group', $_) or next;
138                if ($member{$_}{n}) {
139                    my $sth = $self->db->prepare_cached(
140                        q{insert into user_attributes_groups (id, attr, value) values (?,?,?)}
141                    );
142                    $sth->execute($self->id, $attrid, $_);
143                } elsif ($member{$_}{c}) {
144                    my $sth = $self->db->prepare_cached(
145                        q{delete from user_attributes_groups where id = ? and attr = ? and value = ?}
146                    );
147                    $sth->execute($self->id, $attrid, $_);
148                } # else {} # can't happend
149            }
150            next;
151        };
152        $fdata{$attr} = $data{$attr};
153    }
154    if (keys %fdata) {
155        $self->SUPER::set_fields(%fdata);
156    }
157}
158
159
1601;
161
162__END__
163
164=head1 SEE ALSO
165
166=head1 AUTHOR
167
168Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
169
170=head1 COPYRIGHT AND LICENSE
171
172Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS
173
174This library is free software; you can redistribute it and/or modify
175it under the same terms as Perl itself, either Perl version 5.10.0 or,
176at your option, any later version of Perl 5 you may have available.
177
178
179=cut
Note: See TracBrowser for help on using the repository browser.