|
Lines 32-37
use Koha::AuthUtils qw(hash_password);
Link Here
|
| 32 |
use List::MoreUtils qw( any ); |
32 |
use List::MoreUtils qw( any ); |
| 33 |
use Net::LDAP; |
33 |
use Net::LDAP; |
| 34 |
use Net::LDAP::Filter; |
34 |
use Net::LDAP::Filter; |
|
|
35 |
use Try::Tiny; |
| 35 |
|
36 |
|
| 36 |
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug); |
37 |
use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $debug); |
| 37 |
|
38 |
|
|
Lines 401-436
sub updateborrower_ldap {
Link Here
|
| 401 |
my $patron = shift; |
402 |
my $patron = shift; |
| 402 |
my $cardnumber = $patron->cardnumber; |
403 |
my $cardnumber = $patron->cardnumber; |
| 403 |
|
404 |
|
| 404 |
return 0 unless ($config{update}); |
405 |
return unless ($config{update}); |
| 405 |
|
406 |
|
| 406 |
my @hosts = split(',', $prefhost); |
407 |
my @hosts = split(',', $prefhost); |
| 407 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
408 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return; |
| 408 |
|
409 |
|
| 409 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
410 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
| 410 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
411 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
| 411 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
412 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
| 412 |
if ($res->code) { # connection refused |
413 |
if ($res->code) { # connection refused |
| 413 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
414 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
| 414 |
return 0; |
415 |
return; |
| 415 |
} |
416 |
} |
| 416 |
my $search = $db->search( |
417 |
my $search = $db->search( |
| 417 |
base => $base, |
418 |
base => $base, |
| 418 |
filter => $filter, |
419 |
filter => $filter, |
| 419 |
); |
420 |
); |
| 420 |
|
421 |
|
| 421 |
# Search error, return 0 |
422 |
# Search error, return undef |
| 422 |
return (0, $search->error) if $search->code; |
423 |
return (undef, $search->error) if $search->code; |
| 423 |
|
424 |
|
| 424 |
my $count = $search->count; |
425 |
my $count = $search->count; |
| 425 |
if ($count == 1) { |
426 |
if ($count == 1) { |
| 426 |
# User was found, update! |
427 |
# User was found, update! |
| 427 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
428 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
|
|
429 |
|
| 428 |
&update_local($patron->userid, $patron->password, $patron->borrowernumber, \%borrower); |
430 |
&update_local($patron->userid, $patron->password, $patron->borrowernumber, \%borrower); |
| 429 |
|
431 |
|
| 430 |
return 1; |
432 |
my $updated_patron = Koha::Patrons->find( $patron->borrowernumber ); |
|
|
433 |
return $updated_patron; |
| 431 |
} |
434 |
} |
| 432 |
else { |
435 |
else { |
| 433 |
return 0; |
436 |
return; |
| 434 |
} |
437 |
} |
| 435 |
|
438 |
|
| 436 |
} |
439 |
} |
|
Lines 440-464
sub updateborrower_ldap {
Link Here
|
| 440 |
sub findcardnumber_ldap { |
443 |
sub findcardnumber_ldap { |
| 441 |
my $cardnumber = shift; |
444 |
my $cardnumber = shift; |
| 442 |
|
445 |
|
| 443 |
return 0 unless ($config{replicate}); |
446 |
return unless ($config{replicate}); |
| 444 |
|
447 |
|
| 445 |
my @hosts = split(',', $prefhost); |
448 |
my @hosts = split(',', $prefhost); |
| 446 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
449 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return; |
| 447 |
|
450 |
|
| 448 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
451 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
| 449 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
452 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
| 450 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
453 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
| 451 |
if ($res->code) { # connection refused |
454 |
if ($res->code) { # connection refused |
| 452 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
455 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
| 453 |
return 0; |
456 |
return; |
| 454 |
} |
457 |
} |
| 455 |
my $search = $db->search( |
458 |
my $search = $db->search( |
| 456 |
base => $base, |
459 |
base => $base, |
| 457 |
filter => $filter, |
460 |
filter => $filter, |
| 458 |
); |
461 |
); |
| 459 |
|
462 |
|
| 460 |
# Search error, return 0 |
463 |
# Search error, return undef |
| 461 |
return (0, $search->error) if $search->code; |
464 |
return (undef, $search->error) if $search->code; |
| 462 |
|
465 |
|
| 463 |
my $count = $search->count; |
466 |
my $count = $search->count; |
| 464 |
if ($count == 1) { |
467 |
if ($count == 1) { |
|
Lines 469-479
sub findcardnumber_ldap {
Link Here
|
| 469 |
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); |
472 |
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); |
| 470 |
$borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; |
473 |
$borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; |
| 471 |
|
474 |
|
| 472 |
my $patron = Koha::Patron->new( \%borrower )->store; |
475 |
try { |
| 473 |
return $patron->borrowernumber; |
476 |
my $patron = Koha::Patron->new( \%borrower )->store; |
|
|
477 |
$patron = Koha::Patrons->find( $patron->borrowernumber ); |
| 478 |
return $patron; |
| 479 |
} catch { |
| 480 |
return (undef, $_->message); |
| 481 |
}; |
| 474 |
} |
482 |
} |
| 475 |
else { |
483 |
else { |
| 476 |
return 0; |
484 |
return; |
| 477 |
} |
485 |
} |
| 478 |
|
486 |
|
| 479 |
} |
487 |
} |