View | Details | Raw Unified | Return to bug 11808
Collapse All | Expand All

(-)a/C4/Auth_with_ldap.pm (+82 lines)
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
(-)a/circ/circulation.pl (-3 / +21 lines)
Lines 245-257 if ($findborrower) { Link Here
245
        my $borrowers = $results->{patrons};
245
        my $borrowers = $results->{patrons};
246
        if ( scalar @$borrowers == 1 ) {
246
        if ( scalar @$borrowers == 1 ) {
247
            $borrowernumber = $borrowers->[0]->{borrowernumber};
247
            $borrowernumber = $borrowers->[0]->{borrowernumber};
248
            # Update the user with LDAP if we need to
249
            if(C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) {
250
                my ($result, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($borrowers->[0]);
251
                $template->param(ldap_error => $ldap_error) if $ldap_error;
252
            }
248
            $query->param( 'borrowernumber', $borrowernumber );
253
            $query->param( 'borrowernumber', $borrowernumber );
249
            $query->param( 'barcode',           '' );
254
            $query->param( 'barcode',           '' );
255
        } elsif ( @$borrowers == 0 ) {
256
            # Try to find a user using LDAP if we couldn't find it locally
257
            my $borrowernumber_ldap;
258
            my $ldap_error;
259
            if (C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) {
260
                ($borrowernumber_ldap, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($findborrower);
261
            }
262
263
            if ($borrowernumber_ldap) {
264
                $borrowernumber = $borrowernumber_ldap;
265
            }
266
            else {
267
                $template->param(ldap_error => $ldap_error) if $ldap_error;
268
                $query->param( 'findborrower', '' );
269
                $message = "'$findborrower'";
270
            }
250
        } elsif ( @$borrowers ) {
271
        } elsif ( @$borrowers ) {
251
            $template->param( borrowers => $borrowers );
272
            $template->param( borrowers => $borrowers );
252
        } else {
253
            $query->param( 'findborrower', '' );
254
            $message = "'$findborrower'";
255
        }
273
        }
256
    }
274
    }
257
}
275
}
(-)a/installer/data/mysql/atomicupdate/bug_11808_SearchCardnumberWithLDAP_sys_pref.sql (+2 lines)
Line 0 Link Here
1
INSERT INTO systempreferences (variable,value,explanation,options,type)
2
VALUES('SearchCardnumberWithLDAP','','Search for a cardnumber with LDAP when trying to do a checkout with a cardnumber that does not exist locally. If the cardnumber is found via LDAP, the user is added locally.','','YesNo');
(-)a/installer/data/mysql/sysprefs.sql (-1 / +2 lines)
Lines 567-571 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
567
('XSLTListsDisplay','default','','Enable XSLT stylesheet control over lists pages display on intranet','Free'),
567
('XSLTListsDisplay','default','','Enable XSLT stylesheet control over lists pages display on intranet','Free'),
568
('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'),
568
('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'),
569
('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'),
569
('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'),
570
('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo')
570
('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo'),
571
('SearchCardnumberWithLDAP','','Search for a cardnumber with LDAP when trying to do a checkout with a cardnumber that does not exist locally. If the cardnumber is found via LDAP, the user is added locally.','','YesNo')
571
;
572
;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref (+6 lines)
Lines 139-144 Patrons: Link Here
139
               dateexpiry: current membership expiry date.
139
               dateexpiry: current membership expiry date.
140
               combination: the latter of the current and expiry date.
140
               combination: the latter of the current and expiry date.
141
     -
141
     -
142
         - pref: SearchCardnumberWithLDAP
143
           choices:
144
               yes: Do
145
               no: "Don't"
146
         - search for a cardnumber with LDAP when trying to do a checkout with a cardnumber that doesn't exist locally. If the cardnumber is found via LDAP, the user is added locally. Note : This feature only works if auth_by_bind is disabled in koha-conf.xml.
147
     -
142
         - pref: TalkingTechItivaPhoneNotification
148
         - pref: TalkingTechItivaPhoneNotification
143
           choices:
149
           choices:
144
               yes: Enable
150
               yes: Enable
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt (-1 / +1 lines)
Lines 555-560 $(document).ready(function() { Link Here
555
555
556
[% IF ( message ) %]
556
[% IF ( message ) %]
557
[% INCLUDE 'patron-toolbar.inc' %]
557
[% INCLUDE 'patron-toolbar.inc' %]
558
[% IF ( ldap_error ) %]<h4><span>An error occured while trying to contact the LDAP server : [% ldap_error %]</span></h4>[% END %]
558
<h4>
559
<h4>
559
No patron matched <span class="ex">[% message | html %]</span>
560
No patron matched <span class="ex">[% message | html %]</span>
560
</h4>
561
</h4>
561
- 

Return to bug 11808