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 |
|
my $strix; |
20 |
my $ldap = Net::LDAP->new( $ldap_config->{Server} ) or die "$@"; |
|
21 |
|
GetOptions( |
22 |
# an anonymous bind |
'limit=i', => \$limit, |
23 |
#my $mesg = $ldap->bind; |
'model=s', => \@models, |
24 |
my $mesg = $ldap->bind( |
'strix+', => \$strix, |
25 |
DN => $ldap_config->{DN}, |
) or die "$0: $!"; |
26 |
password => $ldap_config->{Password}, |
|
27 |
); |
my $ldap = A3C::LDAP->new; |
28 |
|
|
29 |
Jifty->log->info("Connected to ", $ldap_config->{Server}, " with DN ", $ldap_config->{DN}); |
sub sync_org { |
30 |
|
my $org_uid = shift; |
31 |
# perform a search |
my $action = Jifty->web->new_action( |
32 |
$mesg = $ldap->search( |
class => 'SyncOrganization', |
33 |
base => "dc=skole,dc=hr", |
moniker => 'sync', |
34 |
# filter => "(&(sn=Barr) (o=Texas Instruments))", |
arguments => { |
35 |
filter => "(objectClass=hrEduPerson)", |
org_uid => $org_uid, |
36 |
sizelimit => 100, # 0 = off |
} |
37 |
); |
); |
38 |
|
$action->run; |
39 |
|
if ( $action->result->success ) { |
40 |
|
Jifty->log->info( $action->result->message ); |
41 |
|
} else { |
42 |
|
Jifty->log->error( "Can't sync $org_uid" ); |
43 |
|
} |
44 |
|
} |
45 |
|
|
46 |
if ( $mesg->code ) { |
if ( $strix ) { |
47 |
warn $mesg->code, ": ", $mesg->error, "\n"; |
my $strix_sites = A3C::Model::StrixSiteCollection->new; |
48 |
|
$strix_sites->unlimit; |
49 |
|
my $o_nr = 1; |
50 |
|
while ( my $site = $strix_sites->next ) { |
51 |
|
Jifty->log->info( 'sync organization ', $o_nr++, '/', $strix_sites->count, ' ', $site->site ); |
52 |
|
sync_org( $site->site ); |
53 |
|
} |
54 |
|
exit; |
55 |
} |
} |
56 |
|
|
57 |
Jifty->log->info( "found ", $mesg->count, " entries" ); |
if ( @models ) { |
58 |
|
|
59 |
foreach my $entry ( $mesg->entries ) { |
Jifty->log->info( 'syncing: ', join(',', @models) ); |
60 |
|
|
61 |
$entry->dump; |
foreach my $model ( @models ) { |
62 |
|
my $collection = $ldap->collection( $model, limit => $limit ); |
63 |
|
Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); |
64 |
|
} |
65 |
|
} else { |
66 |
|
|
67 |
my $data; |
my $person_oc = $ldap->objectClass->{person}; |
68 |
my $user = A3C::Model::User->new; |
my $org_oc = $ldap->objectClass->{organization}; |
69 |
|
|
70 |
my @columns = map { $_->name } $user->columns; |
Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" ); |
|
#warn "# columns = ",dump( @columns ); |
|
71 |
|
|
72 |
foreach my $attr ( $entry->attributes ) { |
my $orgs = $ldap->collection( $org_oc , limit => $limit ); |
|
if ( grep(/^\Q$attr\E$/, @columns ) ) { |
|
|
$data->{$attr} = $entry->get_value( $attr ); |
|
|
} else { |
|
|
Jifty->log->error(ref($user)," doesn't have $attr"); |
|
|
} |
|
|
} |
|
73 |
|
|
74 |
Jifty->log->debug( dump( $data ) ); |
my $o_nr = 1; |
75 |
|
|
76 |
my ( $id, $message ) = $user->load_or_create( %$data ); |
my $value_from = $ldap->link->{value_from}; |
77 |
|
|
78 |
Jifty->log->error( $message ) if ! $id; |
while ( my $o = $orgs->next ) { |
79 |
|
Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); |
80 |
|
if ( ! $o->can( $value_from ) ) { |
81 |
|
warn "can't find $value_from in ",dump( $o->as_hash ); |
82 |
|
next; |
83 |
|
} |
84 |
|
my $org_uid = $o->$value_from || warn "can't find org_uid in $value_from in ",dump( $o->as_hash ); |
85 |
|
sync_org( $org_uid ); |
86 |
|
} |
87 |
} |
} |
|
|
|