From eb46be86645d79a95bbff04d9c961eb07e90ba07 Mon Sep 17 00:00:00 2001 From: Henri-Damien LAURENT Date: Fri, 18 May 2012 16:16:46 +0200 Subject: [PATCH] Bug 8123 : cronjob to pre-populate/Update patrons from ldap data This script is meant to regulary and automatically add user data from LDAP entries based on the Configuration. In order to do so, the script supposes that you have a user with read access or even anonymous access to LDAP and that users have uid. --- misc/cronjobs/update_patrons_from_LDAP.pl | 72 +++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 misc/cronjobs/update_patrons_from_LDAP.pl diff --git a/misc/cronjobs/update_patrons_from_LDAP.pl b/misc/cronjobs/update_patrons_from_LDAP.pl new file mode 100644 index 0000000..194c1e1 --- /dev/null +++ b/misc/cronjobs/update_patrons_from_LDAP.pl @@ -0,0 +1,72 @@ +#!/usr/bin/perl +use Net::LDAP; +use Net::LDAP::Filter; +use C4::Auth_with_ldap; +use C4::Members; +use C4::Context; +use C4::Debug; +use YAML; +use strict; + +my $dbh = C4::Context->dbh; +## La bonne façon de faire serait d'exporter les variables de Auth_with_ldap et de les utiliser directement Je pense. + +#Copier coller du début du script. + +my $ldap = C4::Context->config("ldapserver") or die 'No "ldapserver" in server hash from KOHA_CONF: ' . $ENV{KOHA_CONF}; +my $prefhost = $ldap->{hostname} or die ldapserver_error('hostname'); +my $base = $ldap->{base} or die ldapserver_error('base'); +my $ldapname = $ldap->{user}; +my $ldappassword = $ldap->{pass}; +our %mapping = %{ $ldap->{mapping} }; +$debug && warn Dump($ldap); + +# Infos to do an anonymous bind + +my %config = ( + anonymous => ( $ldapname and $ldappassword ) ? 0 : 1, + replicate => defined( $ldap->{replicate} ) ? $ldap->{replicate} : 1, # add from LDAP to Koha database for new user + update => defined( $ldap->{update} ) ? $ldap->{update} : 1, # update from LDAP to Koha database for existing user +); + +my @hosts = split( ',', $prefhost ); +my $db = Net::LDAP->new( \@hosts ); +unless ($db) { + warn " host $ldap doesnot exist !"; + return 0; +} +my $res = ( $config{anonymous} ) ? $db->bind : $db->bind( $ldapname, password => $ldappassword ); +warn "LDAP Auth impossible : server not responding" if ( $res->code ); + +## based on Koha Members +#my $filter = Net::LDAP::Filter->new("uid=*"); +my $filter = Net::LDAP::Filter->new("uid=*"); +my $userdnsearch = $db->search( base => $base, filter => $filter ); +$userdnsearch->code and die $userdnsearch->error; +while ( my $userldapentry = $userdnsearch->shift_entry ) { + + #warn Data::Dumper::Dumper($userldapentry); + my $uid_field = $mapping{userid}->{is} or die ldapserver_error("mapping for 'userid'"); + my $userid = $userldapentry->{$uid_field}; + $debug && warn Dump($userldapentry); + + my %borrower = C4::Auth_with_ldap::ldap_entry_2_hash( $userldapentry, $userid ); + $debug and print "checkpw_ldap received \%borrower w/ " . keys(%borrower), " keys: ", join( ' ', keys %borrower ), "\n"; + my ( $borrowernumber, $cardnumber, $savedpw ); + ( $borrowernumber, $cardnumber, $userid, $savedpw ) = C4::Auth_with_ldap::exists_local( $borrower{'userid'} ); + if ($borrowernumber) { + ( $config{update} ) and my $c2 = C4::Auth_with_ldap::update_local( $userid, "!", $borrowernumber, \%borrower ) || ''; + ( $cardnumber eq $c2 ) or warn "update_local returned cardnumber '$c2' instead of '$cardnumber'"; + } else { + if ( $config{replicate} ) { + $borrowernumber = eval { AddMember(%borrower) }; + if ($@) { + warn Dump(%borrower), " $@"; + } + } + } +} +$db->unbind; +### NOTICE It could also rely on data in LDAP and populate the data in Koha... +#Pb a) Search the whole annuary +# b) Adds data that could be useless in Koha. -- 1.7.10