@@ -, +, @@ --- C4/Auth_with_ldap.pm | 82 ++++++++++++++++++++ circ/circulation.pl | 23 +++++- installer/data/mysql/sysprefs.sql | 3 +- installer/data/mysql/updatedatabase.pl | 9 ++ .../prog/en/modules/admin/preferences/patrons.pref | 6 ++ .../prog/en/modules/circ/circulation.tt | 1 + 6 files changed, 121 insertions(+), 3 deletions(-) --- a/C4/Auth_with_ldap.pm +++ a/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__ --- a/circ/circulation.pl +++ a/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', '' ); --- a/installer/data/mysql/sysprefs.sql +++ a/installer/data/mysql/sysprefs.sql @@ -423,5 +423,6 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'), ('yuipath','local','local|http://yui.yahooapis.com/2.5.1/build','Insert the path to YUI libraries, choose local if you use koha offline','Choice'), ('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'), -('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo') +('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo'), +('SearchCardnumberWithLDAP','','Search for a cardnumber with LDAP when trying to do a checkout with a cardnumber that does not exist locally. If the cardnumber is found via LDAP, the user is added locally.','','YesNo') ; --- a/installer/data/mysql/updatedatabase.pl +++ a/installer/data/mysql/updatedatabase.pl @@ -7824,6 +7824,15 @@ if ( CheckVersion($DBversion) ) { SetVersion($DBversion); } +$DBversion = "XXX"; +if(CheckVersion($DBversion)) { + $dbh->do(q{ + INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('SearchCardnumberWithLDAP','','Search for a cardnumber with LDAP when trying to do a checkout with a cardnumber that does not exist locally. If the cardnumber is found via LDAP, the user is added locally.','','YesNo'); + }); + print "Upgrade to $DBversion done (Added SearchCardnumberWithLDAP syspref)\n"; + SetVersion($DBversion); +} + =head1 FUNCTIONS --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref +++ a/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. Note : This feature only works if auth_by_bind is disabled in koha-conf.xml. + - - pref: TalkingTechItivaPhoneNotification choices: yes: Enable --- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt +++ a/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 %]

--