|
Lines 396-401
sub update_local {
Link Here
|
| 396 |
_do_changepassword($userid, $borrowerid, $password) if $borrower->{'password'}; |
396 |
_do_changepassword($userid, $borrowerid, $password) if $borrower->{'password'}; |
| 397 |
} |
397 |
} |
| 398 |
|
398 |
|
|
|
399 |
# Subroutine to update a user with LDAP using its cardnumber. |
| 400 |
sub updateborrower_ldap { |
| 401 |
my $local_borrower = shift; |
| 402 |
my $cardnumber = $local_borrower->{'cardnumber'}; |
| 403 |
|
| 404 |
return 0 unless ($config{update}); |
| 405 |
|
| 406 |
my @hosts = split(',', $prefhost); |
| 407 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
| 408 |
|
| 409 |
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 $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
| 412 |
if ($res->code) { # connection refused |
| 413 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
| 414 |
return 0; |
| 415 |
} |
| 416 |
my $search = $db->search( |
| 417 |
base => $base, |
| 418 |
filter => $filter, |
| 419 |
); |
| 420 |
|
| 421 |
# Search error, return 0 |
| 422 |
return (0, $search->error) if $search->code; |
| 423 |
|
| 424 |
my $count = $search->count; |
| 425 |
if ($count == 1) { |
| 426 |
# User was found, update! |
| 427 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
| 428 |
&update_local($local_borrower->{userid},$local_borrower->{password},$local_borrower->{borrowernumber},\%borrower); |
| 429 |
|
| 430 |
return 1; |
| 431 |
} |
| 432 |
else { |
| 433 |
return 0; |
| 434 |
} |
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
# Subroutine to search for a user by its cardnumber through LDAP. |
| 439 |
# The user is added locally if found. (We suppose this user doesn't already exist if this subroutine is called) |
| 440 |
sub findcardnumber_ldap { |
| 441 |
my $cardnumber = shift; |
| 442 |
|
| 443 |
return 0 unless ($config{replicate}); |
| 444 |
|
| 445 |
my @hosts = split(',', $prefhost); |
| 446 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
| 447 |
|
| 448 |
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"; |
| 450 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
| 451 |
if ($res->code) { # connection refused |
| 452 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
| 453 |
return 0; |
| 454 |
} |
| 455 |
my $search = $db->search( |
| 456 |
base => $base, |
| 457 |
filter => $filter, |
| 458 |
); |
| 459 |
|
| 460 |
# Search error, return 0 |
| 461 |
return (0, $search->error) if $search->code; |
| 462 |
|
| 463 |
my $count = $search->count; |
| 464 |
if ($count == 1) { |
| 465 |
# User was found, add it! |
| 466 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
| 467 |
|
| 468 |
# Generate a random password to keep the user secure if no password was mapped |
| 469 |
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); |
| 470 |
$borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; |
| 471 |
|
| 472 |
my $patron = Koha::Patron->new( \%borrower )->store; |
| 473 |
return $patron->borrowernumber; |
| 474 |
} |
| 475 |
else { |
| 476 |
return 0; |
| 477 |
} |
| 478 |
|
| 479 |
} |
| 480 |
|
| 399 |
1; |
481 |
1; |
| 400 |
__END__ |
482 |
__END__ |
| 401 |
|
483 |
|