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