1 | package LATMOS::Accounts::Bases::Ad::User; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::Ad::objects); |
---|
8 | use Net::LDAP; |
---|
9 | use Net::LDAP::Entry; |
---|
10 | use Net::LDAP::Control::Paged; |
---|
11 | use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); |
---|
12 | use Net::LDAP::Util qw( escape_filter_value ); |
---|
13 | |
---|
14 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
15 | |
---|
16 | =head1 NAME |
---|
17 | |
---|
18 | LATMOS::Ad - Perl extension for blah blah blah |
---|
19 | |
---|
20 | =head1 SYNOPSIS |
---|
21 | |
---|
22 | use LATMOS::Ad; |
---|
23 | blah blah blah |
---|
24 | |
---|
25 | =head1 DESCRIPTION |
---|
26 | |
---|
27 | Stub documentation for LATMOS::Ad, created by h2xs. It looks like the |
---|
28 | author of the extension was negligent enough to leave the stub |
---|
29 | unedited. |
---|
30 | |
---|
31 | Blah blah blah. |
---|
32 | |
---|
33 | =head1 FUNCTIONS |
---|
34 | |
---|
35 | =cut |
---|
36 | |
---|
37 | sub _class_filter { '(&(ObjectClass=user) (!(ObjectClass=computer)))' } |
---|
38 | |
---|
39 | sub _key_attr { 'cn' } |
---|
40 | |
---|
41 | sub _delayed_fields { |
---|
42 | my ($self)= @_; |
---|
43 | return qw(memberOf); |
---|
44 | } |
---|
45 | |
---|
46 | sub _canonical_fields { |
---|
47 | my ($self, $base, $mode) = @_; |
---|
48 | ( |
---|
49 | qw( |
---|
50 | sn name givenName |
---|
51 | sAMAccountName uid gecos |
---|
52 | homeDirectory loginShell |
---|
53 | uidNumber gidNumber gecos |
---|
54 | userPassword |
---|
55 | shadowLastChange shadowMin shadowMax |
---|
56 | shadowWarning shadowInactive shadowExpire |
---|
57 | shadowFlag |
---|
58 | description |
---|
59 | mail |
---|
60 | ipPhone otherTelephone department |
---|
61 | title modbile homePhone |
---|
62 | memberOf |
---|
63 | ), |
---|
64 | ($mode !~ /w/ |
---|
65 | ? qw(cn dn uSNCreated uSNChanged) |
---|
66 | : () |
---|
67 | ) |
---|
68 | ) |
---|
69 | } |
---|
70 | |
---|
71 | sub _populate_entry { |
---|
72 | my ($self, $entry, $field, $value) = @_; |
---|
73 | for ($field) { |
---|
74 | } |
---|
75 | $entry->replace($field, $value); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | sub _create { |
---|
80 | my ($class, $base, $id, %data) = @_; |
---|
81 | |
---|
82 | my $entry = Net::LDAP::Entry->new(); |
---|
83 | |
---|
84 | $entry->dn(join(',', |
---|
85 | sprintf('cn=%s', escape_filter_value($id)), |
---|
86 | $base->object_base_dn($class->type), |
---|
87 | )); |
---|
88 | $entry->replace('sAMAccountName', $id); |
---|
89 | $entry->replace(objectClass => [ qw(top person organizationalPerson user)],); |
---|
90 | $entry->replace(userAccountControl => 544); |
---|
91 | $entry->replace(accountExpires => '9223372036854775807'); # TODO hardcoded, burk |
---|
92 | $entry->replace(userPrincipalName => "$id\@" . $base->ad_domain); |
---|
93 | foreach (keys %data) { |
---|
94 | $class->_populate_entry($entry, $_, $data{$_}); |
---|
95 | } |
---|
96 | my $msg = $base->ldap->add($entry); |
---|
97 | return $msg->code ? 0 : 1; |
---|
98 | } |
---|
99 | |
---|
100 | sub get_field { |
---|
101 | my ($self, $field) = @_; |
---|
102 | |
---|
103 | $field eq 'memberOf' and do { |
---|
104 | my @res; |
---|
105 | $self->base->_unlimited_search( |
---|
106 | base => $self->base->object_base_dn('group'), |
---|
107 | filter => sprintf( |
---|
108 | '(&(objectClass=group)(member=%s))', |
---|
109 | escape_filter_value($self->{entry}->dn), |
---|
110 | ), |
---|
111 | callback => sub { |
---|
112 | my ($mesg, $entry) = @_; |
---|
113 | ref $entry eq 'Net::LDAP::Entry' or return; |
---|
114 | push(@res, $entry->get_value('cn')); |
---|
115 | }, |
---|
116 | ); |
---|
117 | sort(@res); |
---|
118 | return \@res |
---|
119 | }; |
---|
120 | $self->SUPER::get_field($field); |
---|
121 | } |
---|
122 | |
---|
123 | sub set_fields { |
---|
124 | my ($self, %data) = @_; |
---|
125 | my %ndata; |
---|
126 | while (my ($f, $val) = each(%data)) { |
---|
127 | $f eq 'memberOf' and do { |
---|
128 | my %users; |
---|
129 | $users{$_}{e} = 1 foreach (@{ $self->get_field('memberOf') || []}); |
---|
130 | $users{$_}{n} = 1 foreach (@{ $val || []}); |
---|
131 | foreach (keys %users) { |
---|
132 | $users{$_}{e} && $users{$_}{n} and next; |
---|
133 | my $user = $self->base->get_object('group', $_) or next; |
---|
134 | if ($users{$_}{e}) { |
---|
135 | $self->{entry}->del(memberOf => $user->get_field('dn')); |
---|
136 | } elsif ($users{$_}{n}) { |
---|
137 | $self->{entry}->add(memberOf => $user->get_field('dn')); |
---|
138 | } # else {} # can't happen |
---|
139 | my $mesg = $self->{entry}->update($self->base->ldap); |
---|
140 | if ($mesg->code) { |
---|
141 | warn $mesg->error; |
---|
142 | } |
---|
143 | } |
---|
144 | next; |
---|
145 | }; |
---|
146 | $ndata{$f} = $val; |
---|
147 | } |
---|
148 | $self->SUPER::set_fields(%ndata); |
---|
149 | } |
---|
150 | |
---|
151 | 1; |
---|
152 | |
---|
153 | __END__ |
---|
154 | |
---|
155 | =head1 SEE ALSO |
---|
156 | |
---|
157 | =head1 AUTHOR |
---|
158 | |
---|
159 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
160 | |
---|
161 | =head1 COPYRIGHT AND LICENSE |
---|
162 | |
---|
163 | Copyright (C) 2008 CNRS SA/CETP/LATMOS |
---|
164 | |
---|
165 | This library is free software; you can redistribute it and/or modify |
---|
166 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
167 | at your option, any later version of Perl 5 you may have available. |
---|
168 | |
---|
169 | |
---|
170 | =cut |
---|