/[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

revision 10 by dpavlin, Thu Mar 13 14:08:41 2008 UTC revision 106 by dpavlin, Thu May 1 17:06:02 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;
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 $ldap_config = Jifty->config->app('LDAP');  my $limit = 0;
18  Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );  my @models;
19    GetOptions(
20  my $ldap = Net::LDAP->new( Jifty->config->app('LDAP')->{Server} ) or die "$@";          'limit=i', => \$limit,
21            'model=s', => \@models,
 # an anonymous bind  
 #my $mesg = $ldap->bind;  
 my $mesg = $ldap->bind(  
         DN       => Jifty->config->app('LDAP')->{DN},  
         password => Jifty->config->app('LDAP')->{Password},  
22  );  );
23    
24  # perform a search  my $ldap = A3C::LDAP->new;
 $mesg = $ldap->search(  
         base   => "dc=skole,dc=hr",  
 #       filter => "(&(sn=Barr) (o=Texas Instruments))",  
         filter => "(objectClass=hrEduPerson)",  
         sizelimit => 100, # 0 = off  
 );  
25    
26  if ( $mesg->code ) {  if ( @models ) {
         warn $mesg->code, ": ", $mesg->error, "\n";  
 }  
27    
28  Jifty->log->info( "found ", $mesg->count, " entries" );          Jifty->log->info( 'syncing: ', join(',', @models) );
29    
30  foreach my $entry ( $mesg->entries ) {          foreach my $model ( @models ) {
31                    my $collection = $ldap->collection( $model, limit => $limit );
32                    Jifty->log->info( 'found ', $collection->count, ' entries for ', $model );
33            }
34    } else {
35    
36          $entry->dump;          my $person_oc = $ldap->objectClass->{person};
37            my $org_oc = $ldap->objectClass->{organization};
38    
39          my $data;          Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" );
         my $user = CAdmin::Model::User->new;  
40    
41          my @columns = map { $_->name } $user->columns;          my $orgs = $ldap->collection( $org_oc , limit => $limit );
         #warn "# columns = ",dump( @columns );  
42    
43          foreach my $attr ( $entry->attributes ) {          my $o_nr = 1;
44                  if ( grep(/^\Q$attr\E$/, @columns ) ) {  
45                          $data->{$attr} = $entry->get_value( $attr );          while ( my $o = $orgs->next ) {
46                    Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
47                    my $org_uid = $o->__value( $ldap->link->{value_from} ) || die "can't find org_uid";
48                    my $action = Jifty->web->new_action(
49                            class => 'SyncOrganization',
50                            moniker => 'sync',
51                            arguments => {
52                                    org_uid => $org_uid,
53                            }
54                    );
55                    $action->run;
56                    if ( $action->result->success ) {
57                            Jifty->log->info( $action->result->message );
58                  } else {                  } else {
59                          warn "model ",ref($user)," doesn't have column $attr\n";                          Jifty->log->error( "Can't sync ", $o->name );
60                  }                  }
61          }          }
   
         Jifty->log->debug( dump( $data ) );  
   
         $user->load_or_create( %$data );  
62  }  }
   

Legend:
Removed from v.10  
changed lines
  Added in v.106

  ViewVC Help
Powered by ViewVC 1.1.26