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