Bugzilla – Attachment 26374 Details for
Bug 11808
When searching for a cardnumber in the intranet, also try to search for it on the LDAP server if one is configured and add/update user
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Fix system preference and add a note to the preference description.
0001-Add-the-SearchCardnumberWithLDAP-system-preference.patch (text/plain), 9.14 KB, created by
Frédérick Capovilla
on 2014-03-14 21:04:56 UTC
(
hide
)
Description:
Fix system preference and add a note to the preference description.
Filename:
MIME Type:
Creator:
Frédérick Capovilla
Created:
2014-03-14 21:04:56 UTC
Size:
9.14 KB
patch
obsolete
>From c15142c4fe00e1de551732a92712e36f465580ef Mon Sep 17 00:00:00 2001 >From: =?UTF-8?q?Fr=C3=A9d=C3=A9rick?= <frederick.capovilla@libeo.com> >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 +++++- > 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(-) > >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/installer/data/mysql/sysprefs.sql b/installer/data/mysql/sysprefs.sql >index 6438483..c6ad4bb 100644 >--- a/installer/data/mysql/sysprefs.sql >+++ b/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') > ; >diff --git a/installer/data/mysql/updatedatabase.pl b/installer/data/mysql/updatedatabase.pl >index 19893b4..d676e71 100755 >--- a/installer/data/mysql/updatedatabase.pl >+++ b/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 > >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..ce954cf 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. Note : This feature only works if auth_by_bind is disabled in koha-conf.xml. >+ - > - 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 ) %]<h4><span>An error occured while trying to contact the LDAP server : [% ldap_error %]</span></h4>[% END %] > <h4> > No patron matched <span class="ex">[% message %]</span> > </h4> >-- >1.7.2.5 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 11808
:
25527
|
26374
|
60562
|
93767
|
93768
|
95573
|
144823
|
144826
|
162857
|
166773
|
173237
|
174288