Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
use Net::LDAP; |
3 |
use Net::LDAP::Filter; |
4 |
use C4::Auth_with_ldap; |
5 |
use C4::Members; |
6 |
use C4::Context; |
7 |
use C4::Debug; |
8 |
use YAML; |
9 |
use strict; |
10 |
|
11 |
my $dbh = C4::Context->dbh; |
12 |
## La bonne façon de faire serait d'exporter les variables de Auth_with_ldap et de les utiliser directement Je pense. |
13 |
|
14 |
#Copier coller du début du script. |
15 |
|
16 |
my $ldap = C4::Context->config("ldapserver") or die 'No "ldapserver" in server hash from KOHA_CONF: ' . $ENV{KOHA_CONF}; |
17 |
my $prefhost = $ldap->{hostname} or die ldapserver_error('hostname'); |
18 |
my $base = $ldap->{base} or die ldapserver_error('base'); |
19 |
my $ldapname = $ldap->{user}; |
20 |
my $ldappassword = $ldap->{pass}; |
21 |
our %mapping = %{ $ldap->{mapping} }; |
22 |
$debug && warn Dump($ldap); |
23 |
|
24 |
# Infos to do an anonymous bind |
25 |
|
26 |
my %config = ( |
27 |
anonymous => ( $ldapname and $ldappassword ) ? 0 : 1, |
28 |
replicate => defined( $ldap->{replicate} ) ? $ldap->{replicate} : 1, # add from LDAP to Koha database for new user |
29 |
update => defined( $ldap->{update} ) ? $ldap->{update} : 1, # update from LDAP to Koha database for existing user |
30 |
); |
31 |
|
32 |
my @hosts = split( ',', $prefhost ); |
33 |
my $db = Net::LDAP->new( \@hosts ); |
34 |
unless ($db) { |
35 |
warn " host $ldap doesnot exist !"; |
36 |
return 0; |
37 |
} |
38 |
my $res = ( $config{anonymous} ) ? $db->bind : $db->bind( $ldapname, password => $ldappassword ); |
39 |
warn "LDAP Auth impossible : server not responding" if ( $res->code ); |
40 |
|
41 |
## based on Koha Members |
42 |
#my $filter = Net::LDAP::Filter->new("uid=*"); |
43 |
my $filter = Net::LDAP::Filter->new("uid=*"); |
44 |
my $userdnsearch = $db->search( base => $base, filter => $filter ); |
45 |
$userdnsearch->code and die $userdnsearch->error; |
46 |
while ( my $userldapentry = $userdnsearch->shift_entry ) { |
47 |
|
48 |
#warn Data::Dumper::Dumper($userldapentry); |
49 |
my $uid_field = $mapping{userid}->{is} or die ldapserver_error("mapping for 'userid'"); |
50 |
my $userid = $userldapentry->{$uid_field}; |
51 |
$debug && warn Dump($userldapentry); |
52 |
|
53 |
my %borrower = C4::Auth_with_ldap::ldap_entry_2_hash( $userldapentry, $userid ); |
54 |
$debug and print "checkpw_ldap received \%borrower w/ " . keys(%borrower), " keys: ", join( ' ', keys %borrower ), "\n"; |
55 |
my ( $borrowernumber, $cardnumber, $savedpw ); |
56 |
( $borrowernumber, $cardnumber, $userid, $savedpw ) = C4::Auth_with_ldap::exists_local( $borrower{'userid'} ); |
57 |
if ($borrowernumber) { |
58 |
( $config{update} ) and my $c2 = C4::Auth_with_ldap::update_local( $userid, "!", $borrowernumber, \%borrower ) || ''; |
59 |
( $cardnumber eq $c2 ) or warn "update_local returned cardnumber '$c2' instead of '$cardnumber'"; |
60 |
} else { |
61 |
if ( $config{replicate} ) { |
62 |
$borrowernumber = eval { AddMember(%borrower) }; |
63 |
if ($@) { |
64 |
warn Dump(%borrower), " $@"; |
65 |
} |
66 |
} |
67 |
} |
68 |
} |
69 |
$db->unbind; |
70 |
### NOTICE It could also rely on data in LDAP and populate the data in Koha... |
71 |
#Pb a) Search the whole annuary |
72 |
# b) Adds data that could be useless in Koha. |