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