|
Lines 28-33
use Koha::Patrons;
Link Here
|
| 28 |
use Koha::AuthUtils qw( hash_password ); |
28 |
use Koha::AuthUtils qw( hash_password ); |
| 29 |
use Net::LDAP; |
29 |
use Net::LDAP; |
| 30 |
use Net::LDAP::Filter; |
30 |
use Net::LDAP::Filter; |
|
|
31 |
use Try::Tiny; |
| 31 |
|
32 |
|
| 32 |
our ( @ISA, @EXPORT_OK ); |
33 |
our ( @ISA, @EXPORT_OK ); |
| 33 |
|
34 |
|
|
Lines 443-448
sub update_local {
Link Here
|
| 443 |
_do_changepassword( $userid, $borrowerid, $password ) if exists( $borrower->{'password'} ); |
444 |
_do_changepassword( $userid, $borrowerid, $password ) if exists( $borrower->{'password'} ); |
| 444 |
} |
445 |
} |
| 445 |
|
446 |
|
|
|
447 |
# Subroutine to update a user with LDAP using its cardnumber. |
| 448 |
sub updateborrower_ldap { |
| 449 |
my $patron = shift; |
| 450 |
my $cardnumber = $patron->cardnumber; |
| 451 |
|
| 452 |
return unless ($config{update}); |
| 453 |
|
| 454 |
my @hosts = split(',', $prefhost); |
| 455 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return; |
| 456 |
|
| 457 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
| 458 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
| 459 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
| 460 |
if ($res->code) { # connection refused |
| 461 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
| 462 |
return; |
| 463 |
} |
| 464 |
my $search = $db->search( |
| 465 |
base => $base, |
| 466 |
filter => $filter, |
| 467 |
); |
| 468 |
|
| 469 |
# Search error, return undef |
| 470 |
return (undef, $search->error) if $search->code; |
| 471 |
|
| 472 |
my $count = $search->count; |
| 473 |
if ($count == 1) { |
| 474 |
# User was found, update! |
| 475 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
| 476 |
|
| 477 |
&update_local($patron->userid, $patron->password, $patron->borrowernumber, \%borrower); |
| 478 |
|
| 479 |
my $updated_patron = Koha::Patrons->find( $patron->borrowernumber ); |
| 480 |
return $updated_patron; |
| 481 |
} |
| 482 |
else { |
| 483 |
return; |
| 484 |
} |
| 485 |
|
| 486 |
} |
| 487 |
|
| 488 |
# Subroutine to search for a user by its cardnumber through LDAP. |
| 489 |
# The user is added locally if found. (We suppose this user doesn't already exist if this subroutine is called) |
| 490 |
sub findcardnumber_ldap { |
| 491 |
my $cardnumber = shift; |
| 492 |
|
| 493 |
return unless ($config{replicate}); |
| 494 |
|
| 495 |
my @hosts = split(',', $prefhost); |
| 496 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return; |
| 497 |
|
| 498 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
| 499 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
| 500 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
| 501 |
if ($res->code) { # connection refused |
| 502 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
| 503 |
return; |
| 504 |
} |
| 505 |
my $search = $db->search( |
| 506 |
base => $base, |
| 507 |
filter => $filter, |
| 508 |
); |
| 509 |
|
| 510 |
# Search error, return undef |
| 511 |
return (undef, $search->error) if $search->code; |
| 512 |
|
| 513 |
my $count = $search->count; |
| 514 |
if ($count == 1) { |
| 515 |
# User was found, add it! |
| 516 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
| 517 |
|
| 518 |
# Generate a random password to keep the user secure if no password was mapped |
| 519 |
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); |
| 520 |
$borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; |
| 521 |
|
| 522 |
try { |
| 523 |
my @columns = Koha::Patrons->columns; |
| 524 |
my $patron = Koha::Patron->new( |
| 525 |
{ |
| 526 |
map { exists( $borrower{$_} ) ? ( $_ => $borrower{$_} ) : () } @columns |
| 527 |
} |
| 528 |
)->store; |
| 529 |
return $patron; |
| 530 |
} catch { |
| 531 |
return (undef, $_->{msg}); |
| 532 |
}; |
| 533 |
} |
| 534 |
else { |
| 535 |
return; |
| 536 |
} |
| 537 |
|
| 538 |
} |
| 539 |
|
| 446 |
1; |
540 |
1; |
| 447 |
__END__ |
541 |
__END__ |
| 448 |
|
542 |
|