--- bin/ldap.pl 2008/03/13 13:49:41 8 +++ bin/ldap.pl 2008/03/17 13:20:37 26 @@ -8,40 +8,47 @@ use Jifty; use Net::LDAP; use Data::Dump qw/dump/; +use Getopt::Long; BEGIN { Jifty->new; }; -#warn "# config->app(LDAP) = ",dump( Jifty->config->app('LDAP') ); +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( Jifty->config->app('LDAP')->{Server} ) or die "$@"; +my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@"; # an anonymous bind #my $mesg = $ldap->bind; -my $mesg = $ldap->bind( - DN => Jifty->config->app('LDAP')->{DN}, - password => Jifty->config->app('LDAP')->{Password}, -); +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 => 3, # 0 = off + sizelimit => $limit, # 0 = off ); if ( $mesg->code ) { - warn $mesg->code, ": ", $mesg->error, "\n"; + Jifty->log->error( $mesg->code, ": ", $mesg->error ); } -warn "# found ", $mesg->count, " entries\n"; +Jifty->log->info( "found ", $mesg->count, " entries" ); -foreach my $entry ( $mesg->entries ) { +#foreach my $entry ( $mesg->entries ) { +while ( my $entry = $mesg->shift_entry ) { - $entry->dump; +# $entry->dump; my $data; - my $user = CAdmin::Model::User->new; + my $user = A3C::Model::User->new; my @columns = map { $_->name } $user->columns; #warn "# columns = ",dump( @columns ); @@ -49,13 +56,19 @@ foreach my $attr ( $entry->attributes ) { if ( grep(/^\Q$attr\E$/, @columns ) ) { $data->{$attr} = $entry->get_value( $attr ); - } else { - warn "model ",ref($user)," doesn't have column $attr\n"; + } elsif ( $attr !~ m/^(objectClass)$/i ) { + Jifty->log->error(ref($user)," doesn't have $attr"); } } - warn dump( $data ); + Jifty->log->debug( dump( $data ) ); + + my ( $id, $message ) = $user->load_or_create( %$data ); - $user->load_or_create( %$data ); + if ( $id ) { + Jifty->log->info("added $id ", $data->{uid}); + } else { + Jifty->log->error( $message ); + } }