From b2a042446c16f37dee25780ff5e5cb9608dade2a 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 | 103 +++++++++++++++++++++++++++++ 1 file changed, 103 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..4de88b9 --- /dev/null +++ b/misc/cronjobs/update_patrons_from_LDAP.pl @@ -0,0 +1,103 @@ +#!/usr/bin/perl +# Copyright 2012 BibLibre +# +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it under the +# terms of the GNU General Public License as published by the Free Software +# Foundation; either version 2 of the License, or (at your option) any later +# version. +# +# Koha is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with Koha; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +=head1 NAME + +update_patrons_from_LDAP.pl - cron script to pre-populate borrowers data from LDAP + +=head1 SYNOPSIS + +./update_patrons_from_LDAP.pl + +or, in crontab: +0 1 * * * ${PERL5LIB}/misc/cronjobs/update_patrons_from_LDAP.pl + +=head1 DESCRIPTION + +This script preload borrowers data from LDAP +It therefore uses a user that has read permission on user data. +Or performs an anonymous login, and then uses the data to preload users. + + +=cut + +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; +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 $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; +#Pb a) Search the whole annuary +# b) Adds data that could be useless in Koha. -- 1.7.10