1 | package LATMOS::Accounts::Bases::Sql::User; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
8 | |
---|
9 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
10 | |
---|
11 | =head1 NAME |
---|
12 | |
---|
13 | LATMOS::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 | |
---|
23 | Account base access over standard unix file format. |
---|
24 | |
---|
25 | =head1 FUNCTIONS |
---|
26 | |
---|
27 | =cut |
---|
28 | |
---|
29 | =head2 new(%options) |
---|
30 | |
---|
31 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
32 | |
---|
33 | domain / server: either the Ad domain or directly the server |
---|
34 | |
---|
35 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
36 | |
---|
37 | =cut |
---|
38 | |
---|
39 | sub object_table { 'user' } |
---|
40 | |
---|
41 | sub key_field { 'uid' } |
---|
42 | |
---|
43 | sub has_extended_attributes { 1 } |
---|
44 | |
---|
45 | sub _delayed_fields { |
---|
46 | my ($self)= @_; |
---|
47 | return qw(memberOf manager); |
---|
48 | } |
---|
49 | |
---|
50 | sub _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 | |
---|
67 | sub 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 | |
---|
121 | sub 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 | |
---|
160 | 1; |
---|
161 | |
---|
162 | __END__ |
---|
163 | |
---|
164 | =head1 SEE ALSO |
---|
165 | |
---|
166 | =head1 AUTHOR |
---|
167 | |
---|
168 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
169 | |
---|
170 | =head1 COPYRIGHT AND LICENSE |
---|
171 | |
---|
172 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
173 | |
---|
174 | This library is free software; you can redistribute it and/or modify |
---|
175 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
176 | at your option, any later version of Perl 5 you may have available. |
---|
177 | |
---|
178 | |
---|
179 | =cut |
---|