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