/[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 36 by dpavlin, Sun Mar 30 00:02:18 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    
15  my $ldap_config = Jifty->config->app('LDAP');  my $limit = 100;
16  Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );  GetOptions(
17            'limit=i', => \$limit,
 my $ldap = Net::LDAP->new( Jifty->config->app('LDAP')->{Server} ) or die "$@";  
   
 # an anonymous bind  
 #my $mesg = $ldap->bind;  
 my $mesg = $ldap->bind(  
         DN       => Jifty->config->app('LDAP')->{DN},  
         password => Jifty->config->app('LDAP')->{Password},  
18  );  );
19    
20  # perform a search  # perform a search
21  $mesg = $ldap->search(  my $mesg = A3C::LDAP->search(
22          base   => "dc=skole,dc=hr",          base   => "dc=skole,dc=hr",
23  #       filter => "(&(sn=Barr) (o=Texas Instruments))",  #       filter => "(&(sn=Barr) (o=Texas Instruments))",
24          filter => "(objectClass=hrEduPerson)",          filter => "(objectClass=hrEduPerson)",
25          sizelimit => 100, # 0 = off          sizelimit => $limit, # 0 = off
26  );  );
27    
28  if ( $mesg->code ) {  if ( $mesg->code ) {
29          warn $mesg->code, ": ", $mesg->error, "\n";          Jifty->log->error( $mesg->code, ": ", $mesg->error );
30  }  }
31    
32  Jifty->log->info( "found ", $mesg->count, " entries" );  Jifty->log->info( "found ", $mesg->count, " entries" );
33    
34  foreach my $entry ( $mesg->entries ) {  sub ldap2model {
35            my ( $model, $entry, $additional ) = @_;
         $entry->dump;  
   
36          my $data;          my $data;
         my $user = CAdmin::Model::User->new;  
37    
38          my @columns = map { $_->name } $user->columns;          my @columns = map { $_->name } $model->columns;
39          #warn "# columns = ",dump( @columns );          #warn "# columns = ",dump( @columns );
40    
41          foreach my $attr ( $entry->attributes ) {          foreach my $attr ( $entry->attributes ) {
42                  if ( grep(/^\Q$attr\E$/, @columns ) ) {                  if ( grep(/^\Q$attr\E$/, @columns ) ) {
43                          $data->{$attr} = $entry->get_value( $attr );                          $data->{$attr} = $entry->get_value( $attr );
44                  } else {  #               } elsif ( $attr !~ m/^(objectClass)$/i ) {
45                          warn "model ",ref($user)," doesn't have column $attr\n";  #                       Jifty->log->error(ref($model)," doesn't have $attr");
46                  }                  }
47          }          }
48    
49          Jifty->log->debug( dump( $data ) );          Jifty->log->debug( ref($model), ' = ', dump( $data ) );
50    
51            my ( $id, $message ) = $model->load_or_create( %$data, %$additional );
52    
53            if ( $id ) {
54                    Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id );
55            } else {
56                    Jifty->log->error( ref($model), " ", $message );
57            }
58    }
59    
60    #foreach my $entry ( $mesg->entries ) {
61    while ( my $entry = $mesg->shift_entry ) {
62    
63    #       $entry->dump;
64    
65            my $organization = A3C::Model::Organization->new;
66            ldap2model( $organization, $entry );
67    
68            my $user = A3C::Model::User->new;
69            ldap2model( $user, $entry, { organization => $organization } );
70    
         $user->load_or_create( %$data );  
71  }  }
72    

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

  ViewVC Help
Powered by ViewVC 1.1.26