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

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

  ViewVC Help
Powered by ViewVC 1.1.26