--- bin/ldap.pl 2008/03/13 14:00:40 9 +++ bin/ldap.pl 2008/05/14 16:50:02 111 @@ -6,57 +6,59 @@ 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_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 "$@"; - -# an anonymous bind -#my $mesg = $ldap->bind; -my $mesg = $ldap->bind( - DN => Jifty->config->app('LDAP')->{DN}, - password => Jifty->config->app('LDAP')->{Password}, +my $limit = 0; +my @models; +GetOptions( + 'limit=i', => \$limit, + 'model=s', => \@models, ); -# perform a search -$mesg = $ldap->search( - base => "dc=skole,dc=hr", -# filter => "(&(sn=Barr) (o=Texas Instruments))", - filter => "(objectClass=hrEduPerson)", - sizelimit => 3, # 0 = off -); +my $ldap = A3C::LDAP->new; -if ( $mesg->code ) { - warn $mesg->code, ": ", $mesg->error, "\n"; -} +if ( @models ) { -Jifty->log->info( "found ", $mesg->count, " entries" ); + Jifty->log->info( 'syncing: ', join(',', @models) ); -foreach my $entry ( $mesg->entries ) { + foreach my $model ( @models ) { + my $collection = $ldap->collection( $model, limit => $limit ); + Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); + } +} else { - $entry->dump; + my $person_oc = $ldap->objectClass->{person}; + my $org_oc = $ldap->objectClass->{organization}; - my $data; - my $user = CAdmin::Model::User->new; + Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" ); - my @columns = map { $_->name } $user->columns; - #warn "# columns = ",dump( @columns ); + my $orgs = $ldap->collection( $org_oc , limit => $limit ); - foreach my $attr ( $entry->attributes ) { - if ( grep(/^\Q$attr\E$/, @columns ) ) { - $data->{$attr} = $entry->get_value( $attr ); + my $o_nr = 1; + + my $value_from = $ldap->link->{value_from}; + + while ( my $o = $orgs->next ) { + Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); + my $org_uid = $o->$value_from || die "can't find org_uid in $value_from in ",dump( $o->as_hash ); + my $action = Jifty->web->new_action( + class => 'SyncOrganization', + moniker => 'sync', + arguments => { + org_uid => $org_uid, + } + ); + $action->run; + if ( $action->result->success ) { + Jifty->log->info( $action->result->message ); } else { - warn "model ",ref($user)," doesn't have column $attr\n"; + Jifty->log->error( "Can't sync ", $o->name ); } } - - Jifty->log->debug( dump( $data ) ); - - $user->load_or_create( %$data ); } -