/[A3C]/bin/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/ldap.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 34 by dpavlin, Tue Mar 18 19:22:50 2008 UTC revision 44 by dpavlin, Sun Mar 30 21:59:34 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    
15  my $limit = 100;  my $limit = 0;
16    my @models;
17  GetOptions(  GetOptions(
18          'limit=i', => \$limit,          'limit=i', => \$limit,
19            'model=s', => \@models,
20  );  );
21    
22  my $ldap_config = Jifty->config->app('LDAP');  @models = ( 'Organization' ) unless @models;
 Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );  
23    
24  my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@";  my $ldap = A3C::LDAP->new;
25    
26  # 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" );  
   
 sub ldap2model {  
         my ( $model, $entry, $additional ) = @_;  
         my $data;  
   
         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");  
                 }  
         }  
   
         Jifty->log->debug( ref($model), ' = ', dump( $data ) );  
   
         my ( $id, $message ) = $model->load_or_create( %$data, %$additional );  
   
         if ( $id ) {  
                 Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id );  
         } else {  
                 Jifty->log->error( ref($model), " ", $message );  
         }  
 }  
   
 #foreach my $entry ( $mesg->entries ) {  
 while ( my $entry = $mesg->shift_entry ) {  
   
 #       $entry->dump;  
   
         my $organization = A3C::Model::Organization->new;  
         ldap2model( $organization, $entry );  
   
         my $user = A3C::Model::User->new;  
         ldap2model( $user, $entry, { organization => $organization } );  
27    
28    foreach my $model ( @models ) {
29            my $collection = $ldap->collection( $model, $limit );
30            Jifty->log->info( "found ", $collection->count, " entries for $model" );
31  }  }
32    

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

  ViewVC Help
Powered by ViewVC 1.1.26