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

(-)a/C4/Auth_with_ldap.pm (+82 lines)
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
(-)a/circ/circulation.pl (-3 / +21 lines)
Lines 264-276 if ($findborrower) { Link Here
264
        my $borrowers = $results->{patrons};
264
        my $borrowers = $results->{patrons};
265
        if ( scalar @$borrowers == 1 ) {
265
        if ( scalar @$borrowers == 1 ) {
266
            $borrowernumber = $borrowers->[0]->{borrowernumber};
266
            $borrowernumber = $borrowers->[0]->{borrowernumber};
267
            # Update the user with LDAP if we need to
268
            if(C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) {
269
                my ($result, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($borrowers->[0]);
270
                $template->param(ldap_error => $ldap_error) if $ldap_error;
271
            }
267
            $query->param( 'borrowernumber', $borrowernumber );
272
            $query->param( 'borrowernumber', $borrowernumber );
268
            $query->param( 'barcode',           '' );
273
            $query->param( 'barcode',           '' );
274
        } elsif ( @$borrowers == 0 ) {
275
            # Try to find a user using LDAP if we couldn't find it locally
276
            my $borrowernumber_ldap;
277
            my $ldap_error;
278
            if (C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) {
279
                ($borrowernumber_ldap, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($findborrower);
280
            }
281
282
            if ($borrowernumber_ldap) {
283
                $borrowernumber = $borrowernumber_ldap;
284
            }
285
            else {
286
                $template->param(ldap_error => $ldap_error) if $ldap_error;
287
                $query->param( 'findborrower', '' );
288
                $message = "'$findborrower'";
289
            }
269
        } elsif ( @$borrowers ) {
290
        } elsif ( @$borrowers ) {
270
            $template->param( borrowers => $borrowers );
291
            $template->param( borrowers => $borrowers );
271
        } else {
272
            $query->param( 'findborrower', '' );
273
            $message = "'$findborrower'";
274
        }
292
        }
275
    }
293
    }
276
}
294
}
(-)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 671-675 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
671
('XSLTListsDisplay','default','','Enable XSLT stylesheet control over lists pages display on intranet','Free'),
671
('XSLTListsDisplay','default','','Enable XSLT stylesheet control over lists pages display on intranet','Free'),
672
('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'),
672
('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'),
673
('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'),
673
('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'),
674
('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo')
674
('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo'),
675
('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')
675
;
676
;
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref (+6 lines)
Lines 162-167 Patrons: Link Here
162
               dateexpiry: current membership expiry date.
162
               dateexpiry: current membership expiry date.
163
               combination: the latter of the current and expiry date.
163
               combination: the latter of the current and expiry date.
164
     -
164
     -
165
         - pref: SearchCardnumberWithLDAP
166
           choices:
167
               yes: Do
168
               no: "Don't"
169
         - 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.
170
     -
165
         - pref: TalkingTechItivaPhoneNotification
171
         - pref: TalkingTechItivaPhoneNotification
166
           choices:
172
           choices:
167
               yes: Enable
173
               yes: Enable
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt (-1 / +1 lines)
Lines 479-484 Link Here
479
479
480
                    [% IF ( message ) %]
480
                    [% IF ( message ) %]
481
                        [% INCLUDE 'patron-toolbar.inc' %]
481
                        [% INCLUDE 'patron-toolbar.inc' %]
482
                        [% IF ( ldap_error ) %]<h4><span>An error occured while trying to contact the LDAP server : [% ldap_error %]</span></h4>[% END %]
482
                        <h4>No patron matched <span class="ex">[% message | html %]</span></h4>
483
                        <h4>No patron matched <span class="ex">[% message | html %]</span></h4>
483
                    [% END %]
484
                    [% END %]
484
485
485
- 

Return to bug 11808