1 | package LATMOS::Accounts::Bases::Ad::objects; |
---|
2 | |
---|
3 | use 5.010000; |
---|
4 | use strict; |
---|
5 | use warnings; |
---|
6 | |
---|
7 | use base qw(LATMOS::Accounts::Bases::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 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 | |
---|
60 | sub _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 | |
---|
68 | sub 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 | |
---|
90 | sub _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 | |
---|
102 | sub ldap { |
---|
103 | return $_[0]->base->{_ldap}; |
---|
104 | } |
---|
105 | |
---|
106 | sub 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 | |
---|
114 | sub _populate_entry { |
---|
115 | my ($self, $entry, $field, $value) = @_; |
---|
116 | $entry->replace($field, $value); |
---|
117 | } |
---|
118 | |
---|
119 | sub 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 | |
---|
135 | 1; |
---|
136 | |
---|
137 | __END__ |
---|
138 | |
---|
139 | =head1 SEE ALSO |
---|
140 | |
---|
141 | =head1 AUTHOR |
---|
142 | |
---|
143 | Olivier Thauvin, E<lt>olivier.thauvin@aerov.jussieu.frE<gt> |
---|
144 | |
---|
145 | =head1 COPYRIGHT AND LICENSE |
---|
146 | |
---|
147 | Copyright (C) 2008 CNRS SA/CETP/LATMOS |
---|
148 | |
---|
149 | This library is free software; you can redistribute it and/or modify |
---|
150 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
151 | at your option, any later version of Perl 5 you may have available. |
---|
152 | |
---|
153 | |
---|
154 | =cut |
---|