/[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 36 by dpavlin, Sun Mar 30 00:02:18 2008 UTC revision 61 by dpavlin, Wed Apr 9 21:32:34 2008 UTC
# Line 11  use Data::Dump qw/dump/; Line 11  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 = 100;  my $limit = 0;
18    my @models;
19  GetOptions(  GetOptions(
20          'limit=i', => \$limit,          'limit=i', => \$limit,
21            'model=s', => \@models,
22  );  );
23    
24  # perform a search  my $ldap = A3C::LDAP->new;
 my $mesg = A3C::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");  
                 }  
         }  
25    
26          Jifty->log->debug( ref($model), ' = ', dump( $data ) );  if ( @models ) {
27    
28          my ( $id, $message ) = $model->load_or_create( %$data, %$additional );          Jifty->log->info( 'syncing: ', join(',', @models) );
29    
30          if ( $id ) {          foreach my $model ( @models ) {
31                  Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id );                  my $collection = $ldap->collection( $model, limit => $limit );
32          } else {                  Jifty->log->info( 'found ', $collection->count, ' entries for ', $model );
                 Jifty->log->error( ref($model), " ", $message );  
33          }          }
34  }  } else {
   
 #foreach my $entry ( $mesg->entries ) {  
 while ( my $entry = $mesg->shift_entry ) {  
35    
36  #       $entry->dump;          Jifty->log->info( 'syncing all organizations and users' );
37    
38          my $organization = A3C::Model::Organization->new;          my $orgs = $ldap->collection( 'Organization', limit => $limit );
         ldap2model( $organization, $entry );  
39    
40          my $user = A3C::Model::User->new;          my $o_nr = 1;
         ldap2model( $user, $entry, { organization => $organization } );  
41    
42            while ( my $o = $orgs->next ) {
43                    Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name );
44                    my $action = Jifty->web->new_action(
45                            class => 'SyncOrganization',
46                            moniker => 'sync',
47                            arguments => {
48                                    cn => $o->cn,
49                            }
50                    );
51                    $action->run;
52                    if ( $action->result->success ) {
53                            Jifty->log->info( $action->result->message );
54                    } else {
55                            Jifty->log->error( "Can't sync ", $o->name );
56                    }
57            }
58  }  }
   

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

  ViewVC Help
Powered by ViewVC 1.1.26