/[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 34 by dpavlin, Tue Mar 18 19:22:50 2008 UTC bin/import-ldap.pl revision 132 by dpavlin, Thu May 29 22:37:20 2008 UTC
# Line 6  use strict; Line 6  use strict;
6  use lib 'lib';  use lib 'lib';
7    
8  use Jifty;  use Jifty;
9  use Net::LDAP;  use A3C::LDAP;
10  use Data::Dump qw/dump/;  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 = 100;  my $limit = 0;
18    my @models;
19  GetOptions(  GetOptions(
20          'limit=i', => \$limit,          'limit=i', => \$limit,
21            'model=s', => \@models,
22  );  );
23    
24  my $ldap_config = Jifty->config->app('LDAP');  my $ldap = A3C::LDAP->new;
 Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );  
25    
26  my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@";  if ( @models ) {
27    
28  # an anonymous bind          Jifty->log->info( 'syncing: ', join(',', @models) );
 #my $mesg = $ldap->bind;  
 my $mesg = $ldap->bind( $ldap_config->{DN}, password => $ldap_config->{Password} );  
   
 Jifty->log->info("Connected to ", $ldap_config->{Server}, " with DN ", $ldap_config->{DN});  
   
 # perform a search  
 $mesg = $ldap->search(  
         base   => "dc=skole,dc=hr",  
 #       filter => "(&(sn=Barr) (o=Texas Instruments))",  
         filter => "(objectClass=hrEduPerson)",  
         sizelimit => $limit, # 0 = off  
 );  
   
 if ( $mesg->code ) {  
         Jifty->log->error( $mesg->code, ": ", $mesg->error );  
 }  
   
 Jifty->log->info( "found ", $mesg->count, " entries" );  
29    
30  sub ldap2model {          foreach my $model ( @models ) {
31          my ( $model, $entry, $additional ) = @_;                  my $collection = $ldap->collection( $model, limit => $limit );
32          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");  
                 }  
33          }          }
34    } else {
35    
36          Jifty->log->debug( ref($model), ' = ', dump( $data ) );          my $person_oc = $ldap->objectClass->{person};
37            my $org_oc = $ldap->objectClass->{organization};
38    
39          my ( $id, $message ) = $model->load_or_create( %$data, %$additional );          Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" );
40    
41          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 );  
         }  
 }  
42    
43  #foreach my $entry ( $mesg->entries ) {          my $o_nr = 1;
 while ( my $entry = $mesg->shift_entry ) {  
44    
45  #       $entry->dump;          my $value_from = $ldap->link->{value_from};
   
         my $organization = A3C::Model::Organization->new;  
         ldap2model( $organization, $entry );  
   
         my $user = A3C::Model::User->new;  
         ldap2model( $user, $entry, { organization => $organization } );  
46    
47            while ( my $o = $orgs->next ) {
48                    Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
49                    if ( ! $o->can( $value_from ) ) {
50                            warn "can't find $value_from in ",dump( $o->as_hash );
51                            next;
52                    }
53                    my $org_uid = $o->$value_from || warn "can't find org_uid in $value_from in ",dump( $o->as_hash );
54                    my $action = Jifty->web->new_action(
55                            class => 'SyncOrganization',
56                            moniker => 'sync',
57                            arguments => {
58                                    org_uid => $org_uid,
59                            }
60                    );
61                    $action->run;
62                    if ( $action->result->success ) {
63                            Jifty->log->info( $action->result->message );
64                    } else {
65                            Jifty->log->error( "Can't sync ", $o->name );
66                    }
67            }
68  }  }
   

Legend:
Removed from v.34  
changed lines
  Added in v.132

  ViewVC Help
Powered by ViewVC 1.1.26