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 |
|
Jifty->web->request(Jifty::Request->new); |
15 |
|
Jifty->web->response(Jifty::Response->new); |
16 |
|
|
17 |
my $ldap_config = Jifty->config->app('LDAP'); |
my $limit = 0; |
18 |
Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) ); |
my @models; |
19 |
|
GetOptions( |
20 |
my $ldap = Net::LDAP->new( Jifty->config->app('LDAP')->{Server} ) or die "$@"; |
'limit=i', => \$limit, |
21 |
|
'model=s', => \@models, |
|
# an anonymous bind |
|
|
#my $mesg = $ldap->bind; |
|
|
my $mesg = $ldap->bind( |
|
|
DN => Jifty->config->app('LDAP')->{DN}, |
|
|
password => Jifty->config->app('LDAP')->{Password}, |
|
22 |
); |
); |
23 |
|
|
24 |
# perform a search |
my $ldap = A3C::LDAP->new; |
|
$mesg = $ldap->search( |
|
|
base => "dc=skole,dc=hr", |
|
|
# filter => "(&(sn=Barr) (o=Texas Instruments))", |
|
|
filter => "(objectClass=hrEduPerson)", |
|
|
sizelimit => 100, # 0 = off |
|
|
); |
|
25 |
|
|
26 |
if ( $mesg->code ) { |
if ( @models ) { |
|
warn $mesg->code, ": ", $mesg->error, "\n"; |
|
|
} |
|
27 |
|
|
28 |
Jifty->log->info( "found ", $mesg->count, " entries" ); |
Jifty->log->info( 'syncing: ', join(',', @models) ); |
29 |
|
|
30 |
foreach my $entry ( $mesg->entries ) { |
foreach my $model ( @models ) { |
31 |
|
my $collection = $ldap->collection( $model, limit => $limit ); |
32 |
|
Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); |
33 |
|
} |
34 |
|
} else { |
35 |
|
|
36 |
$entry->dump; |
Jifty->log->info( 'syncing all organizations and users' ); |
37 |
|
|
38 |
my $data; |
my $orgs = $ldap->collection( 'Organization', limit => $limit ); |
|
my $user = A3C::Model::User->new; |
|
39 |
|
|
40 |
my @columns = map { $_->name } $user->columns; |
my $o_nr = 1; |
|
#warn "# columns = ",dump( @columns ); |
|
41 |
|
|
42 |
foreach my $attr ( $entry->attributes ) { |
while ( my $o = $orgs->next ) { |
43 |
if ( grep(/^\Q$attr\E$/, @columns ) ) { |
Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); |
44 |
$data->{$attr} = $entry->get_value( $attr ); |
my $action = Jifty->web->new_action( |
45 |
|
class => 'SyncOrganization', |
46 |
|
moniker => 'sync', |
47 |
|
arguments => { |
48 |
|
cn => $o->cn, |
49 |
|
} |
50 |
|
); |
51 |
|
warn "## ",dump( $action ); |
52 |
|
$action->run; |
53 |
|
if ( $action->result->success ) { |
54 |
|
Jifty->log->info( $action->result->message ); |
55 |
} else { |
} else { |
56 |
warn "model ",ref($user)," doesn't have column $attr\n"; |
Jifty->log->error( "Can't sync ", $o->name ); |
57 |
} |
} |
58 |
} |
} |
|
|
|
|
Jifty->log->debug( dump( $data ) ); |
|
|
|
|
|
$user->load_or_create( %$data ); |
|
59 |
} |
} |
|
|
|