/[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 26 by dpavlin, Mon Mar 17 13:20:37 2008 UTC bin/import-ldap.pl revision 175 by dpavlin, Mon Jun 16 16:49:30 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 = 0;  my $limit = 0;
18    my @models;
19    my $strix;
20    
21  GetOptions(  GetOptions(
22          'limit=i', => \$limit,          'limit=i', => \$limit,
23  );          'model=s', => \@models,
24            'strix+', => \$strix,
25  my $ldap_config = Jifty->config->app('LDAP');  ) or die "$0: $!";
26  Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );  
27    my $ldap = A3C::LDAP->new;
28  my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@";  
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  # an anonymous bind  @models = 'hrEduOrg' if $strix;
 #my $mesg = $ldap->bind;  
 my $mesg = $ldap->bind( $ldap_config->{DN}, password => $ldap_config->{Password} );  
47    
48  Jifty->log->info("Connected to ", $ldap_config->{Server}, " with DN ", $ldap_config->{DN});  if ( @models ) {
49    
50  # 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 => $limit, # 0 = off  
 );  
51    
52  if ( $mesg->code ) {          foreach my $model ( @models ) {
53          Jifty->log->error( $mesg->code, ": ", $mesg->error );                  my $collection = $ldap->collection( $model, limit => $limit );
54  }                  Jifty->log->info( 'found ', $collection->count, ' entries for ', $model );
55            }
56    } else {
57    
58  Jifty->log->info( "found ", $mesg->count, " entries" );          my $person_oc = $ldap->objectClass->{person};
59            my $org_oc = $ldap->objectClass->{organization};
60    
61  #foreach my $entry ( $mesg->entries ) {          Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" );
 while ( my $entry = $mesg->shift_entry ) {  
62    
63  #       $entry->dump;          my $orgs = $ldap->collection( $org_oc , limit => $limit );
64    
65          my $data;          my $o_nr = 1;
         my $user = A3C::Model::User->new;  
66    
67          my @columns = map { $_->name } $user->columns;          my $value_from = $ldap->link->{value_from};
         #warn "# columns = ",dump( @columns );  
68    
69          foreach my $attr ( $entry->attributes ) {          while ( my $o = $orgs->next ) {
70                  if ( grep(/^\Q$attr\E$/, @columns ) ) {                  Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
71                          $data->{$attr} = $entry->get_value( $attr );                  if ( ! $o->can( $value_from ) ) {
72                  } elsif ( $attr !~ m/^(objectClass)$/i ) {                          warn "can't find $value_from in ",dump( $o->as_hash );
73                          Jifty->log->error(ref($user)," doesn't have $attr");                          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          Jifty->log->debug( dump( $data ) );  if ( $strix ) {
81            my $instances = A3C::Model::StrixInstanceCollection->new;
82          my ( $id, $message ) = $user->load_or_create( %$data );          $instances->unlimit;
83            my $o_nr = 1;
84          if ( $id ) {          while ( my $instance = $instances->next ) {
85                  Jifty->log->info("added $id ", $data->{uid});                  Jifty->log->info( 'sync organization ', $o_nr++, '/', $instances->count, ' ', $instance->instance );
86          } else {                  sync_org( $instance->instance );
                 Jifty->log->error( $message );  
87          }          }
88  }  }
89    

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

  ViewVC Help
Powered by ViewVC 1.1.26