--- bin/ldap.pl 2008/03/18 19:03:53 32 +++ bin/import-ldap.pl 2008/06/16 16:49:30 175 @@ -6,78 +6,84 @@ 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 $limit = 0; +my @models; +my $strix; -my $limit = 100; GetOptions( 'limit=i', => \$limit, -); + 'model=s', => \@models, + 'strix+', => \$strix, +) or die "$0: $!"; + +my $ldap = A3C::LDAP->new; + +sub sync_org { + my $org_uid = shift; + 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 { + Jifty->log->error( "Can't sync $org_uid" ); + } +} -my $ldap_config = Jifty->config->app('LDAP'); -Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) ); +@models = 'hrEduOrg' if $strix; -my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@"; +if ( @models ) { -# an anonymous bind -#my $mesg = $ldap->bind; -my $mesg = $ldap->bind( $ldap_config->{DN}, password => $ldap_config->{Password} ); + Jifty->log->info( 'syncing: ', join(',', @models) ); -Jifty->log->info("Connected to ", $ldap_config->{Server}, " with DN ", $ldap_config->{DN}); + foreach my $model ( @models ) { + my $collection = $ldap->collection( $model, limit => $limit ); + Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); + } +} else { -# perform a search -$mesg = $ldap->search( - base => "dc=skole,dc=hr", -# filter => "(&(sn=Barr) (o=Texas Instruments))", - filter => "(objectClass=hrEduPerson)", - sizelimit => $limit, # 0 = off -); + my $person_oc = $ldap->objectClass->{person}; + my $org_oc = $ldap->objectClass->{organization}; -if ( $mesg->code ) { - Jifty->log->error( $mesg->code, ": ", $mesg->error ); -} + Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" ); -Jifty->log->info( "found ", $mesg->count, " entries" ); + my $orgs = $ldap->collection( $org_oc , limit => $limit ); -sub ldap2model { - my ( $model, $entry, $additional ) = @_; - my $data; - - my @columns = map { $_->name } $model->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($model)," doesn't have $attr"); - } - } + my $o_nr = 1; - Jifty->log->debug( ref($model), ' = ', dump( $data ) ); + my $value_from = $ldap->link->{value_from}; - my ( $id, $message ) = $model->load_or_create( %$data, %$additional ); - - if ( $id ) { - Jifty->log->info("added ", ref($model), " $id" ); - } else { - Jifty->log->error( ref($model), " ", $message ); + while ( my $o = $orgs->next ) { + Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); + if ( ! $o->can( $value_from ) ) { + warn "can't find $value_from in ",dump( $o->as_hash ); + next; + } + my $org_uid = $o->$value_from || warn "can't find org_uid in $value_from in ",dump( $o->as_hash ); + sync_org( $org_uid ); } } -#foreach my $entry ( $mesg->entries ) { -while ( my $entry = $mesg->shift_entry ) { - -# $entry->dump; - - my $organization = A3C::Model::Organization->new; - ldap2model( $organization, $entry ); - - my $user = A3C::Model::User->new; - ldap2model( $user, $entry, { organization => $organization } ); - +if ( $strix ) { + my $instances = A3C::Model::StrixInstanceCollection->new; + $instances->unlimit; + my $o_nr = 1; + while ( my $instance = $instances->next ) { + Jifty->log->info( 'sync organization ', $o_nr++, '/', $instances->count, ' ', $instance->instance ); + sync_org( $instance->instance ); + } }