--- bin/ldap.pl 2008/03/12 23:14:13 5 +++ bin/ldap.pl 2008/04/09 21:32:34 61 @@ -6,28 +6,53 @@ use lib 'lib'; use Jifty; -use Net::LDAP; +use A3C::LDAP; +use Data::Dump qw/dump/; +use Getopt::Long; BEGIN { Jifty->new; }; +Jifty->web->request(Jifty::Request->new); +Jifty->web->response(Jifty::Response->new); -my $ldap = Net::LDAP->new( 'ldap1.skole.local' ) or die "$@"; +my $limit = 0; +my @models; +GetOptions( + 'limit=i', => \$limit, + 'model=s', => \@models, +); -# an anonymous bind -my $mesg = $ldap->bind; +my $ldap = A3C::LDAP->new; -# perform a search -$mesg = $ldap->search( - base => "dc=skole,dc=hr", -# filter => "(&(sn=Barr) (o=Texas Instruments))", - filter => "(objectClass=hrEduPerson)", - sizelimit => 0, # off -); +if ( @models ) { -if ( $mesg->code ) { - warn $mesg->code, ": ", $mesg->error, "\n"; -} + Jifty->log->info( 'syncing: ', join(',', @models) ); -foreach my $entry ( $mesg->entries ) { - $entry->dump; + foreach my $model ( @models ) { + my $collection = $ldap->collection( $model, limit => $limit ); + Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); + } +} else { + + Jifty->log->info( 'syncing all organizations and users' ); + + my $orgs = $ldap->collection( 'Organization', limit => $limit ); + + my $o_nr = 1; + + while ( my $o = $orgs->next ) { + Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); + my $action = Jifty->web->new_action( + class => 'SyncOrganization', + moniker => 'sync', + arguments => { + cn => $o->cn, + } + ); + $action->run; + if ( $action->result->success ) { + Jifty->log->info( $action->result->message ); + } else { + Jifty->log->error( "Can't sync ", $o->name ); + } + } } -