1 | package LATMOS::Accounts::Bases::Sql::User; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use LATMOS::Accounts::Utils; |
---|
8 | use base qw(LATMOS::Accounts::Bases::Sql::objects); |
---|
9 | |
---|
10 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
11 | |
---|
12 | =head1 NAME |
---|
13 | |
---|
14 | LATMOS::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 | |
---|
24 | Account base access over standard unix file format. |
---|
25 | |
---|
26 | =head1 FUNCTIONS |
---|
27 | |
---|
28 | =cut |
---|
29 | |
---|
30 | =head2 new(%options) |
---|
31 | |
---|
32 | Create a new LATMOS::Ad object for windows AD $domain. |
---|
33 | |
---|
34 | domain / server: either the Ad domain or directly the server |
---|
35 | |
---|
36 | ldap_args is an optionnal list of arguments to pass to L<Net::LDAP>. |
---|
37 | |
---|
38 | =cut |
---|
39 | |
---|
40 | sub object_table { 'user' } |
---|
41 | |
---|
42 | sub key_field { 'name' } |
---|
43 | |
---|
44 | sub has_extended_attributes { 1 } |
---|
45 | |
---|
46 | sub _delayed_fields { |
---|
47 | my ($self)= @_; |
---|
48 | return qw(memberOf manager directReports); |
---|
49 | } |
---|
50 | |
---|
51 | sub _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 | |
---|
71 | sub 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 | |
---|
163 | sub 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 | |
---|
203 | 1; |
---|
204 | |
---|
205 | __END__ |
---|
206 | |
---|
207 | =head1 SEE ALSO |
---|
208 | |
---|
209 | =head1 AUTHOR |
---|
210 | |
---|
211 | Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt> |
---|
212 | |
---|
213 | =head1 COPYRIGHT AND LICENSE |
---|
214 | |
---|
215 | Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS |
---|
216 | |
---|
217 | This library is free software; you can redistribute it and/or modify |
---|
218 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
219 | at your option, any later version of Perl 5 you may have available. |
---|
220 | |
---|
221 | |
---|
222 | =cut |
---|