1 |
package A3C::LDAP; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
use Net::LDAP; |
7 |
use Data::Dump qw/dump/; |
8 |
use base qw(Jifty::Object Class::Accessor::Fast); |
9 |
__PACKAGE__->mk_accessors( qw(ldap server dn password current_search) ); |
10 |
|
11 |
|
12 |
=head1 NAME |
13 |
|
14 |
A3C::LDAP |
15 |
|
16 |
=head1 DESCRIPTION |
17 |
|
18 |
This object turn L<Net::LDAP> into something with looks like |
19 |
L<Jifty::Collection> |
20 |
|
21 |
=head1 METHODS |
22 |
|
23 |
=head2 new |
24 |
|
25 |
my $ldap = A3C::LDAP->new; |
26 |
|
27 |
=cut |
28 |
|
29 |
sub new { |
30 |
my $class = shift; |
31 |
|
32 |
my $args = { @_ }; |
33 |
|
34 |
my $ldap_config = Jifty->config->app('LDAP'); |
35 |
Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) ); |
36 |
|
37 |
$args->{server} ||= $ldap_config->{Server}; |
38 |
$args->{dn} ||= $ldap_config->{DN}; |
39 |
$args->{password} ||= $ldap_config->{Password}; |
40 |
|
41 |
my $ldap = Net::LDAP->new( $args->{server} ) or die "$@"; |
42 |
|
43 |
# an anonymous bind |
44 |
#$ldap->bind; |
45 |
$ldap->bind( $args->{dn}, password => $args->{password} ); |
46 |
|
47 |
Jifty->log->info("Connected to ", $args->{server}, " with DN ", $args->{dn}); |
48 |
|
49 |
$args->{ldap} = $ldap; |
50 |
|
51 |
$class->SUPER::new( $args ); |
52 |
} |
53 |
|
54 |
=head2 search |
55 |
|
56 |
my $msg = A3C::LDAP->search( |
57 |
base => 'dc=skole,dc=hr', |
58 |
filter => '(objectClass=hrEduOrg)', |
59 |
sizelimit => 10, |
60 |
); |
61 |
|
62 |
=cut |
63 |
|
64 |
sub search { |
65 |
my $self = shift; |
66 |
|
67 |
my $search = $self->ldap->search( @_ ); |
68 |
if ( $search->code != 0 ) { |
69 |
Jifty->log->error( $search->error ); |
70 |
} |
71 |
return $self->current_search( $search ); |
72 |
} |
73 |
|
74 |
=head2 next |
75 |
|
76 |
Syntaxtic shugar to look more like L<Jifty::DBI::Collection> |
77 |
|
78 |
my $entry = ldap->next; |
79 |
|
80 |
=cut |
81 |
|
82 |
sub next { |
83 |
my $self = shift; |
84 |
|
85 |
die "no current LDAP search" unless $self->current_search; |
86 |
|
87 |
return $self->current_search->shift_entry; |
88 |
} |
89 |
|
90 |
=head2 count |
91 |
|
92 |
my $search_results = $ldap->count; |
93 |
|
94 |
=cut |
95 |
|
96 |
sub count { |
97 |
my $self = shift; |
98 |
$self->current_search->count; |
99 |
} |
100 |
|
101 |
=head1 INTERNAL METHODS |
102 |
|
103 |
Following methods map directly into L<Net::LDAP> |
104 |
|
105 |
=head2 current_search |
106 |
|
107 |
Result of last C<< $ldap->search >> request |
108 |
|
109 |
=cut |
110 |
|
111 |
1; |