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/; |
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 = Net::LDAP->new( 'ldap1.skole.local' ) or die "$@"; |
my $limit = 0; |
18 |
|
my @models; |
19 |
|
GetOptions( |
20 |
|
'limit=i', => \$limit, |
21 |
|
'model=s', => \@models, |
22 |
|
); |
23 |
|
|
24 |
# an anonymous bind |
my $ldap = A3C::LDAP->new; |
|
my $mesg = $ldap->bind; |
|
25 |
|
|
26 |
# perform a search |
if ( @models ) { |
|
$mesg = $ldap->search( |
|
|
base => "dc=skole,dc=hr", |
|
|
# filter => "(&(sn=Barr) (o=Texas Instruments))", |
|
|
filter => "(objectClass=hrEduPerson)", |
|
|
sizelimit => 0, # off |
|
|
); |
|
27 |
|
|
28 |
if ( $mesg->code ) { |
Jifty->log->info( 'syncing: ', join(',', @models) ); |
|
warn $mesg->code, ": ", $mesg->error, "\n"; |
|
|
} |
|
29 |
|
|
30 |
foreach my $entry ( $mesg->entries ) { |
foreach my $model ( @models ) { |
31 |
$entry->dump; |
my $collection = $ldap->collection( $model, limit => $limit ); |
32 |
|
Jifty->log->info( 'found ', $collection->count, ' entries for ', $model ); |
33 |
|
} |
34 |
|
} else { |
35 |
|
|
36 |
|
my $person_oc = $ldap->objectClass->{person}; |
37 |
|
my $org_oc = $ldap->objectClass->{organization}; |
38 |
|
|
39 |
|
Jifty->log->info( "syncing all $org_oc organizations and $person_oc persons" ); |
40 |
|
|
41 |
|
my $orgs = $ldap->collection( $org_oc , limit => $limit ); |
42 |
|
|
43 |
|
my $o_nr = 1; |
44 |
|
|
45 |
|
my $value_from = $ldap->link->{value_from}; |
46 |
|
|
47 |
|
while ( my $o = $orgs->next ) { |
48 |
|
Jifty->log->info( 'sync organization ', $o_nr++, '/', $orgs->count, ' ', $o->name ); |
49 |
|
my $org_uid = $o->$value_from || die "can't find org_uid in $value_from in ",dump( $o->as_hash ); |
50 |
|
my $action = Jifty->web->new_action( |
51 |
|
class => 'SyncOrganization', |
52 |
|
moniker => 'sync', |
53 |
|
arguments => { |
54 |
|
org_uid => $org_uid, |
55 |
|
} |
56 |
|
); |
57 |
|
$action->run; |
58 |
|
if ( $action->result->success ) { |
59 |
|
Jifty->log->info( $action->result->message ); |
60 |
|
} else { |
61 |
|
Jifty->log->error( "Can't sync ", $o->name ); |
62 |
|
} |
63 |
|
} |
64 |
} |
} |
|
|
|