From 99e51dcd5c6bf512bc1625adb5ad4679eec75857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9rick?= Date: Wed, 19 Feb 2014 12:12:55 -0500 Subject: [PATCH] Add the SearchCardnumberWithLDAP system preference. When activated, cardnumber searches in the intranet will also do a search on the LDAP server if a user was not found in the local database. If the cardnumber is found on the LDAP server, the user account is imported in the local database. --- C4/Auth_with_ldap.pm | 82 ++++++++++++++++++++ circ/circulation.pl | 23 +++++- .../prog/en/modules/admin/preferences/patrons.pref | 6 ++ .../prog/en/modules/circ/circulation.tt | 1 + 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/C4/Auth_with_ldap.pm b/C4/Auth_with_ldap.pm index 3b40015..f2ecbfa 100644 --- a/C4/Auth_with_ldap.pm +++ b/C4/Auth_with_ldap.pm @@ -318,6 +318,88 @@ sub update_local { _do_changepassword($userid, $borrowerid, $password); } +# Subroutine to update a user with LDAP using its cardnumber. +sub updateborrower_ldap { + my $local_borrower = shift; + my $cardnumber = $local_borrower->{'cardnumber'}; + + return 0 unless ($config{update}); + + my @hosts = split(',', $prefhost); + my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; + + my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); + my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; + my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); + if ($res->code) { # connection refused + warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); + return 0; + } + my $search = $db->search( + base => $base, + filter => $filter, + ); + + # Search error, return 0 + return (0, $search->error) if $search->code; + + my $count = $search->count; + if ($count == 1) { + # User was found, update! + my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); + &update_local($local_borrower->{userid},$local_borrower->{password},$local_borrower->{borrowernumber},\%borrower); + + return 1; + } + else { + return 0; + } + +} + +# Subroutine to search for a user by its cardnumber through LDAP. +# The user is added locally if found. (We suppose this user doesn't already exist if this subroutine is called) +sub findcardnumber_ldap { + my $cardnumber = shift; + + return 0 unless ($config{replicate}); + + my @hosts = split(',', $prefhost); + my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; + + my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); + my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; + my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); + if ($res->code) { # connection refused + warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); + return 0; + } + my $search = $db->search( + base => $base, + filter => $filter, + ); + + # Search error, return 0 + return (0, $search->error) if $search->code; + + my $count = $search->count; + if ($count == 1) { + # User was found, add it! + my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); + + # Generate a random password to keep the user secure if no password was mapped + my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); + $borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; + + my $borrowernumber = AddMember(%borrower); + return $borrowernumber; + } + else { + return 0; + } + +} + 1; __END__ diff --git a/circ/circulation.pl b/circ/circulation.pl index 9769731..85e0157 100755 --- a/circ/circulation.pl +++ b/circ/circulation.pl @@ -205,10 +205,29 @@ if ($findborrower) { } } if ( @$borrowers == 0 ) { - $query->param( 'findborrower', '' ); - $message = "'$findborrower'"; + # Try to find a user using LDAP if we couldn't find it locally + my $borrowernumber_ldap; + my $ldap_error; + if(C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) { + ($borrowernumber_ldap, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($findborrower); + } + + if($borrowernumber_ldap) { + $borrowernumber = $borrowernumber_ldap; + } + else { + $template->param(ldap_error => $ldap_error) if $ldap_error; + $query->param( 'findborrower', '' ); + $message = "'$findborrower'"; + } } elsif ( @$borrowers == 1 ) { + # Update the user with LDAP if we need to + if(C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) { + my ($result, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($borrowers->[0]); + $template->param(ldap_error => $ldap_error) if $ldap_error; + } + $borrowernumber = $borrowers->[0]->{'borrowernumber'}; $query->param( 'borrowernumber', $borrowernumber ); $query->param( 'barcode', '' ); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref index 74d14af..9055219 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref @@ -123,6 +123,12 @@ Patrons: now: current date. dateexpiry: current membership expiry date. - + - pref: SearchCardnumberWithLDAP + choices: + yes: Do + no: "Don't" + - search for a cardnumber with LDAP when trying to do a checkout with a cardnumber that doesn't exist locally. If the cardnumber is found via LDAP, the user is added locally. + - - pref: TalkingTechItivaPhoneNotification choices: yes: Enable diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt index 81ea2b6..e044ced 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt @@ -430,6 +430,7 @@ var MSG_EXPORT_SELECT_CHECKOUTS = _("You must select checkout(s) to export"); [% IF ( message ) %] [% INCLUDE 'patron-toolbar.inc' %] +[% IF ( ldap_error ) %]

An error occured while trying to contact the LDAP server : [% ldap_error %]

[% END %]

No patron matched [% message %]

-- 1.7.2.5