source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ad/User.pm @ 92

Last change on this file since 92 was 92, checked in by nanardon, 15 years ago
  • allow to set memberUID everywhere
  • Property svn:keywords set to Id Rev
File size: 4.2 KB
Line 
1package LATMOS::Accounts::Bases::Ad::User;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::Ad::objects);
8use Net::LDAP;
9use Net::LDAP::Entry;
10use Net::LDAP::Control::Paged;
11use Net::LDAP::Constant qw( LDAP_CONTROL_PAGED ); 
12use Net::LDAP::Util     qw( escape_filter_value );
13
14our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
15
16=head1 NAME
17
18LATMOS::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
27Stub documentation for LATMOS::Ad, created by h2xs. It looks like the
28author of the extension was negligent enough to leave the stub
29unedited.
30
31Blah blah blah.
32
33=head1 FUNCTIONS
34
35=cut
36
37sub _class_filter { '(&(ObjectClass=user) (!(ObjectClass=computer)))' }
38
39sub _key_attr { 'cn' } 
40
41sub _delayed_fields {
42    my ($self)= @_;
43    return qw(memberOf);
44}
45
46sub _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
71sub _populate_entry {
72    my ($self, $entry, $field, $value) = @_;
73    for ($field) {
74    }
75    $entry->replace($field, $value);
76}
77
78
79sub _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
100sub 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
123sub 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
1511;
152
153__END__
154
155=head1 SEE ALSO
156
157=head1 AUTHOR
158
159Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
160
161=head1 COPYRIGHT AND LICENSE
162
163Copyright (C) 2008 CNRS SA/CETP/LATMOS
164
165This library is free software; you can redistribute it and/or modify
166it under the same terms as Perl itself, either Perl version 5.10.0 or,
167at your option, any later version of Perl 5 you may have available.
168
169
170=cut
Note: See TracBrowser for help on using the repository browser.