/[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 141 by dpavlin, Fri May 30 22:46:53 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  # perform a search  ) or die "$0: $!";
26  my $mesg = A3C::LDAP->search(  
27          base   => "dc=skole,dc=hr",  my $ldap = A3C::LDAP->new;
28  #       filter => "(&(sn=Barr) (o=Texas Instruments))",  
29          filter => "(objectClass=hrEduPerson)",  sub sync_org {
30          sizelimit => $limit, # 0 = off          my $org_uid = shift;
31  );          my $action = Jifty->web->new_action(
32                    class => 'SyncOrganization',
33  if ( $mesg->code ) {                  moniker => 'sync',
34          Jifty->log->error( $mesg->code, ": ", $mesg->error );                  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  Jifty->log->info( "found ", $mesg->count, " entries" );  if ( $strix ) {
47            my $strix_sites = A3C::Model::StrixSiteCollection->new;
48  sub ldap2model {          $strix_sites->unlimit;
49          my ( $model, $entry, $additional ) = @_;          my $o_nr = 1;
50          my $data;          while ( my $site = $strix_sites->next ) {
51                    Jifty->log->info( 'sync organization ', $o_nr++, '/', $strix_sites->count, ' ', $site->site );
52          my @columns = map { $_->name } $model->columns;                  sync_org( $site->site );
         #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");  
                 }  
53          }          }
54            exit;
55    }
56    
57          Jifty->log->debug( ref($model), ' = ', dump( $data ) );  if ( @models ) {
58    
59          my ( $id, $message ) = $model->load_or_create( %$data, %$additional );          Jifty->log->info( 'syncing: ', join(',', @models) );
60    
61          if ( $id ) {          foreach my $model ( @models ) {
62                  Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id );                  my $collection = $ldap->collection( $model, limit => $limit );
63          } else {                  Jifty->log->info( 'found ', $collection->count, ' entries for ', $model );
                 Jifty->log->error( ref($model), " ", $message );  
64          }          }
65  }  } else {
66    
67  #foreach my $entry ( $mesg->entries ) {          my $person_oc = $ldap->objectClass->{person};
68  while ( my $entry = $mesg->shift_entry ) {          my $org_oc = $ldap->objectClass->{organization};
69    
70  #       $entry->dump;          Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" );
71    
72          my $organization = A3C::Model::Organization->new;          my $orgs = $ldap->collection( $org_oc , limit => $limit );
         ldap2model( $organization, $entry );  
73    
74          my $user = A3C::Model::User->new;          my $o_nr = 1;
         ldap2model( $user, $entry, { organization => $organization } );  
75    
76  }          my $value_from = $ldap->link->{value_from};
77    
78            while ( my $o = $orgs->next ) {
79                    Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
80                    if ( ! $o->can( $value_from ) ) {
81                            warn "can't find $value_from in ",dump( $o->as_hash );
82                            next;
83                    }
84                    my $org_uid = $o->$value_from || warn "can't find org_uid in $value_from in ",dump( $o->as_hash );
85                    sync_org( $org_uid );
86            }
87    }

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

  ViewVC Help
Powered by ViewVC 1.1.26