Lines 369-374
sub update_local {
Link Here
|
369 |
_do_changepassword($userid, $borrowerid, $password) if $borrower->{'password'}; |
369 |
_do_changepassword($userid, $borrowerid, $password) if $borrower->{'password'}; |
370 |
} |
370 |
} |
371 |
|
371 |
|
|
|
372 |
# Subroutine to update a user with LDAP using its cardnumber. |
373 |
sub updateborrower_ldap { |
374 |
my $local_borrower = shift; |
375 |
my $cardnumber = $local_borrower->{'cardnumber'}; |
376 |
|
377 |
return 0 unless ($config{update}); |
378 |
|
379 |
my @hosts = split(',', $prefhost); |
380 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
381 |
|
382 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
383 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
384 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
385 |
if ($res->code) { # connection refused |
386 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
387 |
return 0; |
388 |
} |
389 |
my $search = $db->search( |
390 |
base => $base, |
391 |
filter => $filter, |
392 |
); |
393 |
|
394 |
# Search error, return 0 |
395 |
return (0, $search->error) if $search->code; |
396 |
|
397 |
my $count = $search->count; |
398 |
if ($count == 1) { |
399 |
# User was found, update! |
400 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
401 |
&update_local($local_borrower->{userid},$local_borrower->{password},$local_borrower->{borrowernumber},\%borrower); |
402 |
|
403 |
return 1; |
404 |
} |
405 |
else { |
406 |
return 0; |
407 |
} |
408 |
|
409 |
} |
410 |
|
411 |
# Subroutine to search for a user by its cardnumber through LDAP. |
412 |
# The user is added locally if found. (We suppose this user doesn't already exist if this subroutine is called) |
413 |
sub findcardnumber_ldap { |
414 |
my $cardnumber = shift; |
415 |
|
416 |
return 0 unless ($config{replicate}); |
417 |
|
418 |
my @hosts = split(',', $prefhost); |
419 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
420 |
|
421 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
422 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
423 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
424 |
if ($res->code) { # connection refused |
425 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
426 |
return 0; |
427 |
} |
428 |
my $search = $db->search( |
429 |
base => $base, |
430 |
filter => $filter, |
431 |
); |
432 |
|
433 |
# Search error, return 0 |
434 |
return (0, $search->error) if $search->code; |
435 |
|
436 |
my $count = $search->count; |
437 |
if ($count == 1) { |
438 |
# User was found, add it! |
439 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
440 |
|
441 |
# Generate a random password to keep the user secure if no password was mapped |
442 |
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); |
443 |
$borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; |
444 |
|
445 |
my $borrowernumber = AddMember(%borrower); |
446 |
return $borrowernumber; |
447 |
} |
448 |
else { |
449 |
return 0; |
450 |
} |
451 |
|
452 |
} |
453 |
|
372 |
1; |
454 |
1; |
373 |
__END__ |
455 |
__END__ |
374 |
|
456 |
|