--- bin/ldap.pl 2008/03/13 14:08:41 10 +++ bin/ldap.pl 2008/03/18 16:43:14 30 @@ -8,41 +8,47 @@ use Jifty; use Net::LDAP; use Data::Dump qw/dump/; +use Getopt::Long; BEGIN { Jifty->new; }; +my $limit = 100; +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 => 100, # 0 = off + sizelimit => $limit, # 0 = off ); if ( $mesg->code ) { - warn $mesg->code, ": ", $mesg->error, "\n"; + Jifty->log->error( $mesg->code, ": ", $mesg->error ); } 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 ); @@ -50,13 +56,20 @@ 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"); } } Jifty->log->debug( dump( $data ) ); - $user->load_or_create( %$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 ); + } }