source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases/Ad/objects.pm @ 105

Last change on this file since 105 was 105, checked in by nanardon, 15 years ago
  • ensure error are returned
  • Property svn:keywords set to Id Rev
File size: 3.2 KB
Line 
1package LATMOS::Accounts::Bases::Ad::objects;
2
3use 5.010000;
4use strict;
5use warnings;
6
7use base qw(LATMOS::Accounts::Bases::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 list {
38    my ($class, $base) = @_;
39   
40    my @uids;
41    eval {
42    my $xx = $base->_unlimited_search(
43        attrs => [ $class->_key_attr ],
44        base => $base->object_base_dn($class->type),
45        filter => $class->_class_filter,
46        callback => sub {
47            my ($mesg, $entry) = @_;
48            #$mesg->code and die $mesg->error;
49            $entry or return;
50            ref $entry eq 'Net::LDAP::Entry' or return;
51            push(@uids, $entry->get_value( $class->_key_attr ));
52        },
53    );
54    };
55
56    return @uids;
57
58}
59
60sub _get_field_name {
61    my ($self, $field, $base, $for) = @_;
62
63    my %fields = map { $_ => 1 } $self->_canonical_fields($base, $for);
64
65    return $fields{$field} ? $field : undef;
66}
67
68sub new {
69    my ($class, $base, $uid) = @_;
70   
71    my $mesg = $base->ldap->search(
72        filter => sprintf(
73            '(&%s (%s=%s))',
74            $class->_class_filter,
75            $class->_key_attr,
76            escape_filter_value($uid),
77        ),
78        base => $base->object_base_dn($class->type),
79    );
80
81    $mesg->code and return;
82
83    my ($entry, @others) = $mesg->entries;
84
85    return if(@others); # we cannot have multiple entries...
86    return if (!$entry);
87    bless({ entry => $entry, _base => $base, _id => $uid }, $class);
88}
89
90sub _delete {
91    my ($class, $base, $uid) = @_;
92    my $obj = $class->new($base, $uid) or return;
93
94    my $mesg = $base->ldap->delete($obj->{entry}->dn);
95
96    if ($mesg->code) {
97        warn $mesg->error;
98        return;
99    } else { return 1 }
100}
101
102sub ldap {
103    return $_[0]->base->{_ldap};
104}
105
106sub get_field {
107    my ($self, $field) = @_;
108
109    $field eq 'dn' and return $self->{entry}->dn;
110    my ($first, @others) = $self->{entry}->get_value($field);
111    return @others ? [ sort($first, @others) ] : $first;
112}
113
114sub _populate_entry {
115    my ($self, $entry, $field, $value) = @_;
116    $entry->replace($field, $value);
117}
118
119sub set_fields {
120    my ($self, %fields) = @_;
121
122    foreach (keys %fields) {
123        $self->get_field_name($_, 'w') or next;
124        $self->_populate_entry($self->{entry}, $_, $fields{$_});
125    }
126   
127    my $mesg = $self->{entry}->update($self->base->ldap);
128
129    if ($mesg->code) {
130        warn $mesg->error;
131        return;
132    } else { return 1 }
133}
134
1351;
136
137__END__
138
139=head1 SEE ALSO
140
141=head1 AUTHOR
142
143Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt>
144
145=head1 COPYRIGHT AND LICENSE
146
147Copyright (C) 2008 CNRS SA/CETP/LATMOS
148
149This library is free software; you can redistribute it and/or modify
150it under the same terms as Perl itself, either Perl version 5.10.0 or,
151at your option, any later version of Perl 5 you may have available.
152
153
154=cut
Note: See TracBrowser for help on using the repository browser.