/[A3C]/bin/import-ldap.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /bin/import-ldap.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

bin/ldap.pl revision 36 by dpavlin, Sun Mar 30 00:02:18 2008 UTC bin/import-ldap.pl revision 175 by dpavlin, Mon Jun 16 16:49:30 2008 UTC
# Line 11  use Data::Dump qw/dump/; Line 11  use Data::Dump qw/dump/;
11  use Getopt::Long;  use Getopt::Long;
12    
13  BEGIN { Jifty->new; };  BEGIN { Jifty->new; };
14    Jifty->web->request(Jifty::Request->new);
15    Jifty->web->response(Jifty::Response->new);
16    
17    my $limit = 0;
18    my @models;
19    my $strix;
20    
 my $limit = 100;  
21  GetOptions(  GetOptions(
22          'limit=i', => \$limit,          'limit=i', => \$limit,
23  );          'model=s', => \@models,
24            'strix+', => \$strix,
25    ) or die "$0: $!";
26    
27    my $ldap = A3C::LDAP->new;
28    
29    sub sync_org {
30            my $org_uid = shift;
31            my $action = Jifty->web->new_action(
32                    class => 'SyncOrganization',
33                    moniker => 'sync',
34                    arguments => {
35                            org_uid => $org_uid,
36                    }
37            );
38            $action->run;
39            if ( $action->result->success ) {
40                    Jifty->log->info( $action->result->message );
41            } else {
42                    Jifty->log->error( "Can't sync $org_uid" );
43            }
44    }
45    
46  # perform a search  @models = 'hrEduOrg' if $strix;
 my $mesg = A3C::LDAP->search(  
         base   => "dc=skole,dc=hr",  
 #       filter => "(&(sn=Barr) (o=Texas Instruments))",  
         filter => "(objectClass=hrEduPerson)",  
         sizelimit => $limit, # 0 = off  
 );  
47    
48  if ( $mesg->code ) {  if ( @models ) {
         Jifty->log->error( $mesg->code, ": ", $mesg->error );  
 }  
49    
50  Jifty->log->info( "found ", $mesg->count, " entries" );          Jifty->log->info( 'syncing: ', join(',', @models) );
51    
52  sub ldap2model {          foreach my $model ( @models ) {
53          my ( $model, $entry, $additional ) = @_;                  my $collection = $ldap->collection( $model, limit => $limit );
54          my $data;                  Jifty->log->info( 'found ', $collection->count, ' entries for ', $model );
   
         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");  
                 }  
55          }          }
56    } else {
57    
58          Jifty->log->debug( ref($model), ' = ', dump( $data ) );          my $person_oc = $ldap->objectClass->{person};
59            my $org_oc = $ldap->objectClass->{organization};
60    
61          my ( $id, $message ) = $model->load_or_create( %$data, %$additional );          Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" );
62    
63          if ( $id ) {          my $orgs = $ldap->collection( $org_oc , limit => $limit );
                 Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id );  
         } else {  
                 Jifty->log->error( ref($model), " ", $message );  
         }  
 }  
64    
65  #foreach my $entry ( $mesg->entries ) {          my $o_nr = 1;
 while ( my $entry = $mesg->shift_entry ) {  
66    
67  #       $entry->dump;          my $value_from = $ldap->link->{value_from};
68    
69          my $organization = A3C::Model::Organization->new;          while ( my $o = $orgs->next ) {
70          ldap2model( $organization, $entry );                  Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
71                    if ( ! $o->can( $value_from ) ) {
72          my $user = A3C::Model::User->new;                          warn "can't find $value_from in ",dump( $o->as_hash );
73          ldap2model( $user, $entry, { organization => $organization } );                          next;
74                    }
75                    my $org_uid = $o->$value_from || warn "can't find org_uid in $value_from in ",dump( $o->as_hash );
76                    sync_org( $org_uid );
77            }
78    }
79    
80    if ( $strix ) {
81            my $instances = A3C::Model::StrixInstanceCollection->new;
82            $instances->unlimit;
83            my $o_nr = 1;
84            while ( my $instance = $instances->next ) {
85                    Jifty->log->info( 'sync organization ', $o_nr++, '/', $instances->count, ' ', $instance->instance );
86                    sync_org( $instance->instance );
87            }
88  }  }
89    

Legend:
Removed from v.36  
changed lines
  Added in v.175

  ViewVC Help
Powered by ViewVC 1.1.26