--- lib/A3C/LDAP.pm 2008/03/30 00:02:18 36 +++ lib/A3C/LDAP.pm 2008/03/30 15:02:55 40 @@ -5,24 +5,97 @@ use Net::LDAP; use Data::Dump qw/dump/; -use base 'Jifty::Object'; +use base qw(Jifty::Object Class::Accessor::Fast); +__PACKAGE__->mk_accessors( qw(ldap server dn password current_search) ); -my $ldap_config = Jifty->config->app('LDAP'); -Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) ); +=head1 NAME -my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@"; +A3C::LDAP -# an anonymous bind -#my $mesg = $ldap->bind; -my $mesg = $ldap->bind( $ldap_config->{DN}, password => $ldap_config->{Password} ); +=head1 DESCRIPTION -Jifty->log->info("Connected to ", $ldap_config->{Server}, " with DN ", $ldap_config->{DN}); +This object turn L into something with looks like +L + +=head1 METHODS + +=head2 new + + my $ldap = A3C::LDAP->new; + +=cut + +sub new { + my $class = shift; + + my $args = { @_ }; + + my $ldap_config = Jifty->config->app('LDAP'); + Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) ); + + $args->{server} ||= $ldap_config->{Server}; + $args->{dn} ||= $ldap_config->{DN}; + $args->{password} ||= $ldap_config->{Password}; + + my $ldap = Net::LDAP->new( $args->{server} ) or die "$@"; + + # an anonymous bind + #$ldap->bind; + $ldap->bind( $args->{dn}, password => $args->{password} ); + + Jifty->log->info("Connected to ", $args->{server}, " with DN ", $args->{dn}); + + $args->{ldap} = $ldap; + + $class->SUPER::new( $args ); +} + +=head2 search + + my $msg = A3C::LDAP->search( + base => 'dc=skole,dc=hr', + filter => '(objectClass=hrEduOrg)', + sizelimit => 10, + ); + +=cut sub search { my $self = shift; - return $ldap->search( @_ ); + my $search = $self->ldap->search( @_ ); + if ( $search->code != 0 ) { + Jifty->log->error( $search->error ); + } + return $self->current_search( $search ); +} + +=head2 next + +Syntaxtic shugar to look more like L + + my $entry = ldap->next; + +=cut + +sub next { + my $self = shift; + + die "no current LDAP search" unless $self->current_search; + + return $self->current_search->shift_entry; +} + +=head2 count + + my $search_results = $ldap->count; + +=cut + +sub count { + my $self = shift; + $self->current_search->count; } 1;