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

Last change on this file since 107 was 107, checked in by nanardon, 15 years ago
  • objects::set_fields is called also to terminate work, so always try update, but detect 'no update' error
  • Property svn:keywords set to Id Rev
File size: 4.1 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        return [ sort(@res) ];
118    };
119    $self->SUPER::get_field($field);
120}
121
122sub set_fields {
123    my ($self, %data) = @_;
124    my %ndata;
125    while (my ($f, $val) = each(%data)) {
126        $f eq 'memberOf' and do {
127            my %users;
128            $users{$_}{e} = 1 foreach (@{ $self->get_field('memberOf') || []});
129            $users{$_}{n} = 1 foreach (@{ $val || []});
130            foreach (keys %users) {
131                $users{$_}{e} && $users{$_}{n} and next;
132                my $user = $self->base->get_object('group', $_) or next;
133                if ($users{$_}{e}) {
134                    $self->{entry}->del(memberOf => $user->get_field('dn'));
135                } elsif ($users{$_}{n}) {
136                    $self->{entry}->add(memberOf => $user->get_field('dn'));
137                } # else {} # can't happen
138                my $mesg = $self->{entry}->update($self->base->ldap);
139            }
140            next;
141        };
142        $ndata{$f} = $val;
143    }
144    $self->SUPER::set_fields(%ndata);
145}
146
1471;
148
149__END__
150
151=head1 SEE ALSO
152
153=head1 AUTHOR
154
155Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
156
157=head1 COPYRIGHT AND LICENSE
158
159Copyright (C) 2008 CNRS SA/CETP/LATMOS
160
161This library is free software; you can redistribute it and/or modify
162it under the same terms as Perl itself, either Perl version 5.10.0 or,
163at your option, any later version of Perl 5 you may have available.
164
165
166=cut
Note: See TracBrowser for help on using the repository browser.