Lines 318-323
sub update_local {
Link Here
|
318 |
_do_changepassword($userid, $borrowerid, $password); |
318 |
_do_changepassword($userid, $borrowerid, $password); |
319 |
} |
319 |
} |
320 |
|
320 |
|
|
|
321 |
# Subroutine to update a user with LDAP using its cardnumber. |
322 |
sub updateborrower_ldap { |
323 |
my $local_borrower = shift; |
324 |
my $cardnumber = $local_borrower->{'cardnumber'}; |
325 |
|
326 |
return 0 unless ($config{update}); |
327 |
|
328 |
my @hosts = split(',', $prefhost); |
329 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
330 |
|
331 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
332 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
333 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
334 |
if ($res->code) { # connection refused |
335 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
336 |
return 0; |
337 |
} |
338 |
my $search = $db->search( |
339 |
base => $base, |
340 |
filter => $filter, |
341 |
); |
342 |
|
343 |
# Search error, return 0 |
344 |
return (0, $search->error) if $search->code; |
345 |
|
346 |
my $count = $search->count; |
347 |
if ($count == 1) { |
348 |
# User was found, update! |
349 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
350 |
&update_local($local_borrower->{userid},$local_borrower->{password},$local_borrower->{borrowernumber},\%borrower); |
351 |
|
352 |
return 1; |
353 |
} |
354 |
else { |
355 |
return 0; |
356 |
} |
357 |
|
358 |
} |
359 |
|
360 |
# Subroutine to search for a user by its cardnumber through LDAP. |
361 |
# The user is added locally if found. (We suppose this user doesn't already exist if this subroutine is called) |
362 |
sub findcardnumber_ldap { |
363 |
my $cardnumber = shift; |
364 |
|
365 |
return 0 unless ($config{replicate}); |
366 |
|
367 |
my @hosts = split(',', $prefhost); |
368 |
my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return 0; |
369 |
|
370 |
my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'"); |
371 |
my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter"; |
372 |
my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword); |
373 |
if ($res->code) { # connection refused |
374 |
warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res); |
375 |
return 0; |
376 |
} |
377 |
my $search = $db->search( |
378 |
base => $base, |
379 |
filter => $filter, |
380 |
); |
381 |
|
382 |
# Search error, return 0 |
383 |
return (0, $search->error) if $search->code; |
384 |
|
385 |
my $count = $search->count; |
386 |
if ($count == 1) { |
387 |
# User was found, add it! |
388 |
my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber); |
389 |
|
390 |
# Generate a random password to keep the user secure if no password was mapped |
391 |
my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); |
392 |
$borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'}; |
393 |
|
394 |
my $borrowernumber = AddMember(%borrower); |
395 |
return $borrowernumber; |
396 |
} |
397 |
else { |
398 |
return 0; |
399 |
} |
400 |
|
401 |
} |
402 |
|
321 |
1; |
403 |
1; |
322 |
__END__ |
404 |
__END__ |
323 |
|
405 |
|