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

Last change on this file since 114 was 114, checked in by nanardon, 15 years ago
  • Manage account expire date/time
  • 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        accountExpires
64        ),
65        ($mode !~ /w/
66            ? qw(cn dn uSNCreated uSNChanged)
67            : ()
68        )
69    )
70}
71
72sub _populate_entry {
73    my ($self, $entry, $field, $value) = @_;
74    for ($field) {
75    }
76    $entry->replace($field, $value);
77}
78
79
80sub _create {
81    my ($class, $base, $id, %data) = @_;
82
83    my $entry = Net::LDAP::Entry->new();
84
85    $entry->dn(join(',',
86        sprintf('cn=%s', escape_filter_value($id)),
87        $base->object_base_dn($class->type),
88    ));
89    $entry->replace('sAMAccountName', $id);
90    $entry->replace(objectClass => [ qw(top person organizationalPerson user)],);
91    $entry->replace(userAccountControl => 544);
92    $entry->replace(accountExpires => '9223372036854775807'); # TODO hardcoded, burk
93    $entry->replace(userPrincipalName => "$id\@" . $base->ad_domain);
94    foreach (keys %data) {
95        $class->_populate_entry($entry, $_, $data{$_});
96    }
97    my $msg = $base->ldap->add($entry);
98    return $msg->code ? 0 : 1;
99}
100
101sub get_field {
102    my ($self, $field) = @_;
103
104    $field eq 'memberOf' and do {
105        my @res;
106        $self->base->_unlimited_search(
107            base => $self->base->object_base_dn('group'),
108            filter => sprintf(
109                '(&(objectClass=group)(member=%s))',
110                escape_filter_value($self->{entry}->dn),
111            ),
112            callback => sub {
113                my ($mesg, $entry) = @_;
114                ref $entry eq 'Net::LDAP::Entry' or return;
115                push(@res, $entry->get_value('cn'));
116            },
117        );
118        return [ sort(@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            }
141            next;
142        };
143        $ndata{$f} = $val;
144    }
145    $self->SUPER::set_fields(%ndata);
146}
147
1481;
149
150__END__
151
152=head1 SEE ALSO
153
154=head1 AUTHOR
155
156Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
157
158=head1 COPYRIGHT AND LICENSE
159
160Copyright (C) 2008 CNRS SA/CETP/LATMOS
161
162This library is free software; you can redistribute it and/or modify
163it under the same terms as Perl itself, either Perl version 5.10.0 or,
164at your option, any later version of Perl 5 you may have available.
165
166
167=cut
Note: See TracBrowser for help on using the repository browser.