--- bin/ldap.pl 2008/03/12 23:14:13 5 +++ bin/ldap.pl 2008/03/17 21:42:21 28 @@ -7,27 +7,69 @@ use Jifty; use Net::LDAP; +use Data::Dump qw/dump/; +use Getopt::Long; BEGIN { Jifty->new; }; -my $ldap = Net::LDAP->new( 'ldap1.skole.local' ) or die "$@"; +my $limit = 0; +GetOptions( + 'limit=i', => \$limit, +); + +my $ldap_config = Jifty->config->app('LDAP'); +Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) ); + +my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@"; # an anonymous bind -my $mesg = $ldap->bind; +#my $mesg = $ldap->bind; +my $mesg = $ldap->bind( $ldap_config->{DN}, password => $ldap_config->{Password} ); + +Jifty->log->info("Connected to ", $ldap_config->{Server}, " with DN ", $ldap_config->{DN}); # perform a search $mesg = $ldap->search( base => "dc=skole,dc=hr", # filter => "(&(sn=Barr) (o=Texas Instruments))", filter => "(objectClass=hrEduPerson)", - sizelimit => 0, # off + sizelimit => $limit, # 0 = off ); if ( $mesg->code ) { - warn $mesg->code, ": ", $mesg->error, "\n"; + Jifty->log->error( $mesg->code, ": ", $mesg->error ); } -foreach my $entry ( $mesg->entries ) { - $entry->dump; +Jifty->log->info( "found ", $mesg->count, " entries" ); + +#foreach my $entry ( $mesg->entries ) { +while ( my $entry = $mesg->shift_entry ) { + +# $entry->dump; + + my $data; + my $user = A3C::Model::User->new; + + my @columns = map { $_->name } $user->columns; + #warn "# columns = ",dump( @columns ); + + foreach my $attr ( $entry->attributes ) { + if ( grep(/^\Q$attr\E$/, @columns ) ) { + $data->{$attr} = $entry->get_value( $attr ); + } elsif ( $attr !~ m/^(objectClass)$/i ) { + Jifty->log->error(ref($user)," doesn't have $attr"); + } + } + + Jifty->log->debug( dump( $data ) ); + +# my ( $id, $message ) = $user->load_or_create( %$data ); + my ( $id, $message ) = $user->create( %$data ); + + if ( $id ) { + Jifty->log->info("added $id ", $data->{uid}); + } else { + Jifty->log->error( $message ); + } }