/[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 8 by dpavlin, Thu Mar 13 13:49:41 2008 UTC revision 116 by dpavlin, Thu May 15 17:38:13 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  #warn "# config->app(LDAP) = ",dump( Jifty->config->app('LDAP') );  my $limit = 0;
18    my @models;
19    GetOptions(
20            'limit=i', => \$limit,
21            'model=s', => \@models,
22    );
23    
24  my $ldap = Net::LDAP->new( Jifty->config->app('LDAP')->{Server} ) or die "$@";  my $ldap = A3C::LDAP->new;
25    
26  # an anonymous bind  if ( @models ) {
 #my $mesg = $ldap->bind;  
 my $mesg = $ldap->bind(  
         DN       => Jifty->config->app('LDAP')->{DN},  
         password => Jifty->config->app('LDAP')->{Password},  
 );  
27    
28  # perform a search          Jifty->log->info( 'syncing: ', join(',', @models) );
 $mesg = $ldap->search(  
         base   => "dc=skole,dc=hr",  
 #       filter => "(&(sn=Barr) (o=Texas Instruments))",  
         filter => "(objectClass=hrEduPerson)",  
         sizelimit => 3, # 0 = off  
 );  
29    
30  if ( $mesg->code ) {          foreach my $model ( @models ) {
31          warn $mesg->code, ": ", $mesg->error, "\n";                  my $collection = $ldap->collection( $model, limit => $limit );
32  }                  Jifty->log->info( 'found ', $collection->count, ' entries for ', $model );
33            }
34    } else {
35    
36  warn "# found ", $mesg->count, " entries\n";          my $person_oc = $ldap->objectClass->{person};
37            my $org_oc = $ldap->objectClass->{organization};
38    
39  foreach my $entry ( $mesg->entries ) {          Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" );
40    
41          $entry->dump;          my $orgs = $ldap->collection( $org_oc , limit => $limit );
42    
43          my $data;          my $o_nr = 1;
         my $user = CAdmin::Model::User->new;  
44    
45          my @columns = map { $_->name } $user->columns;          my $value_from = $ldap->link->{value_from};
         #warn "# columns = ",dump( @columns );  
46    
47          foreach my $attr ( $entry->attributes ) {          while ( my $o = $orgs->next ) {
48                  if ( grep(/^\Q$attr\E$/, @columns ) ) {                  Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
49                          $data->{$attr} = $entry->get_value( $attr );                  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 {                  } else {
65                          warn "model ",ref($user)," doesn't have column $attr\n";                          Jifty->log->error( "Can't sync ", $o->name );
66                  }                  }
67          }          }
   
         warn dump( $data );  
   
         $user->load_or_create( %$data );  
68  }  }
   

Legend:
Removed from v.8  
changed lines
  Added in v.116

  ViewVC Help
Powered by ViewVC 1.1.26