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

(-)a/C4/Auth_with_ldap.pm (+94 lines)
Lines 28-33 use Koha::Patrons; Link Here
28
use Koha::AuthUtils qw( hash_password );
28
use Koha::AuthUtils qw( hash_password );
29
use Net::LDAP;
29
use Net::LDAP;
30
use Net::LDAP::Filter;
30
use Net::LDAP::Filter;
31
use Try::Tiny;
31
32
32
our (@ISA, @EXPORT_OK);
33
our (@ISA, @EXPORT_OK);
33
BEGIN {
34
BEGIN {
Lines 434-439 sub update_local { Link Here
434
    _do_changepassword($userid, $borrowerid, $password) if exists( $borrower->{'password'} );
435
    _do_changepassword($userid, $borrowerid, $password) if exists( $borrower->{'password'} );
435
}
436
}
436
437
438
# Subroutine to update a user with LDAP using its cardnumber.
439
sub updateborrower_ldap {
440
    my $patron = shift;
441
    my $cardnumber = $patron->cardnumber;
442
443
    return unless ($config{update});
444
445
    my @hosts = split(',', $prefhost);
446
    my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return;
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;
454
    }
455
    my $search = $db->search(
456
        base => $base,
457
        filter => $filter,
458
    );
459
460
    # Search error, return undef
461
    return (undef, $search->error) if $search->code;
462
463
    my $count = $search->count;
464
    if ($count == 1) {
465
        # User was found, update!
466
        my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber);
467
468
        &update_local($patron->userid, $patron->password, $patron->borrowernumber, \%borrower);
469
470
        my $updated_patron = Koha::Patrons->find( $patron->borrowernumber );
471
        return $updated_patron;
472
    }
473
    else {
474
        return;
475
    }
476
477
}
478
479
# Subroutine to search for a user by its cardnumber through LDAP.
480
# The user is added locally if found. (We suppose this user doesn't already exist if this subroutine is called)
481
sub findcardnumber_ldap {
482
    my $cardnumber = shift;
483
484
    return unless ($config{replicate});
485
486
    my @hosts = split(',', $prefhost);
487
    my $db = Net::LDAP->new(\@hosts, timeout => ($ldap->{timeout} || 30)) or return;
488
489
    my $cardnumber_field = $mapping{cardnumber}->{is} or die ldapserver_error("mapping for 'cardnumber'");
490
    my $filter = Net::LDAP::Filter->new("$cardnumber_field=$cardnumber") or die "Failed to create new Net::LDAP::Filter";
491
    my $res = ($config{anonymous}) ? $db->bind : $db->bind($ldapname, password=>$ldappassword);
492
    if ($res->code) {   # connection refused
493
        warn "LDAP bind failed as ldapuser " . ($ldapname || '[ANONYMOUS]') . ": " . description($res);
494
        return;
495
    }
496
    my $search = $db->search(
497
        base => $base,
498
        filter => $filter,
499
    );
500
501
    # Search error, return undef
502
    return (undef, $search->error) if $search->code;
503
504
    my $count = $search->count;
505
    if ($count == 1) {
506
        # User was found, add it!
507
        my %borrower = ldap_entry_2_hash($search->shift_entry,$cardnumber);
508
509
        # Generate a random password to keep the user secure if no password was mapped
510
        my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) );
511
        $borrower{'password'} = join("", @chars[ map { rand @chars } ( 1 .. 30 ) ]) unless $borrower{'password'};
512
513
        try {
514
            my @columns = Koha::Patrons->columns;
515
            my $patron = Koha::Patron->new(
516
                {
517
                    map { exists( $borrower{$_} ) ? ( $_ => $borrower{$_} ) : () } @columns
518
                }
519
            )->store;
520
            return $patron;
521
        } catch {
522
            return (undef, $_->{msg});
523
        };
524
    }
525
    else {
526
        return;
527
    }
528
529
}
530
437
1;
531
1;
438
__END__
532
__END__
439
533
(-)a/circ/circulation.pl (+14 lines)
Lines 241-246 my $message; Link Here
241
if ($findborrower) {
241
if ($findborrower) {
242
    Koha::Plugins->call( 'patron_barcode_transform', \$findborrower );
242
    Koha::Plugins->call( 'patron_barcode_transform', \$findborrower );
243
    my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
243
    my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
244
245
    # search cardnumber on LDAP server
246
    if ( C4::Context->config('useldapserver')
247
        and ( my $search_cardnumber = C4::Context->preference("SearchCardnumberWithLDAP") ) ne "never")
248
    {
249
        my $ldap_error;
250
        if ( $patron ) {
251
            ($patron, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($patron) if $search_cardnumber eq "always";
252
        } else {
253
            ($patron, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($findborrower);
254
        }
255
        $template->param(ldap_error => $ldap_error) if $ldap_error;
256
    }
257
244
    if ( $patron ) {
258
    if ( $patron ) {
245
        $borrowernumber = $patron->borrowernumber;
259
        $borrowernumber = $patron->borrowernumber;
246
    } else {
260
    } else {
(-)a/circ/request-article.pl (-4 / +19 lines)
Lines 51-60 my $biblio = Koha::Biblios->find($biblionumber); Link Here
51
output_and_exit( $cgi, $cookie, $template, 'unknown_biblio')
51
output_and_exit( $cgi, $cookie, $template, 'unknown_biblio')
52
    unless $biblio;
52
    unless $biblio;
53
53
54
my $patron =
54
my $patron = undef;
55
    $patron_id         ? Koha::Patrons->find($patron_id)
55
if ( $patron_id ) {
56
  : $patron_cardnumber ? Koha::Patrons->find( { cardnumber => $patron_cardnumber } )
56
    $patron = Koha::Patrons->find($patron_id);
57
  : undef;
57
} elsif ( $patron_cardnumber ) {
58
    $patron = Koha::Patrons->find( { cardnumber => $patron_cardnumber } );
59
60
    # search cardnumber on LDAP server
61
    if ( C4::Context->config('useldapserver')
62
        and ( my $search_cardnumber = C4::Context->preference("SearchCardnumberWithLDAP") ) ne "never")
63
    {
64
        my $ldap_error;
65
        if ( $patron ) {
66
            ($patron, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($patron) if $search_cardnumber eq "always";
67
        } else {
68
            ($patron, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($patron_cardnumber);
69
        }
70
        $template->param(ldap_error => $ldap_error) if $ldap_error;
71
    }
72
}
58
73
59
if ( $op eq 'cud-create' ) {
74
if ( $op eq 'cud-create' ) {
60
    my $borrowernumber = $cgi->param('borrowernumber');
75
    my $borrowernumber = $cgi->param('borrowernumber');
(-)a/installer/data/mysql/atomicupdate/bug_11808.pl (+14 lines)
Line 0 Link Here
1
use Modern::Perl;
2
3
return {
4
    bug_number => "11808",
5
    description => "Search cardnumber on the LDAP server if one is configured and add/update user.",
6
    up => sub {
7
        my ($args) = @_;
8
        my ($dbh, $out) = @$args{qw(dbh out)};
9
        $dbh->do(q{
10
            INSERT INTO systempreferences (variable,value,explanation,options,type)
11
            VALUES('SearchCardnumberWithLDAP', 'never', 'Search for a cardnumber with LDAP when searching for a patron that does not exist locally. If the cardnumber is found via LDAP, the user is added or updated.', 'always|not_found|never', 'Choice');
12
        });
13
    },
14
};
(-)a/installer/data/mysql/mandatory/sysprefs.sql (+1 lines)
Lines 673-678 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
673
('SCOLoadCheckoutsByDefault','1','','If enabled, load the list of a patrons checkouts when they log in to the Self Checkout','YesNo'),
673
('SCOLoadCheckoutsByDefault','1','','If enabled, load the list of a patrons checkouts when they log in to the Self Checkout','YesNo'),
674
('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
674
('SCOUserCSS','',NULL,'Add CSS to be included in the SCO module in an embedded <style> tag.','free'),
675
('SCOUserJS','',NULL,'Define custom javascript for inclusion in the SCO module','free'),
675
('SCOUserJS','',NULL,'Define custom javascript for inclusion in the SCO module','free'),
676
('SearchCardnumberWithLDAP', 'never', 'Search for a cardnumber with LDAP when searching for a patron that does not exist locally. If the cardnumber is found via LDAP, the user is added or updated.', 'always|not_found|never', 'Choice'),
676
('SearchEngine','Zebra','Elasticsearch|Zebra','Search Engine','Choice'),
677
('SearchEngine','Zebra','Elasticsearch|Zebra','Search Engine','Choice'),
677
('SearchLimitLibrary', 'homebranch', 'homebranch|holdingbranch|both', "When limiting search results with a library or library group, use the item's home library, or holding library, or both.", 'Choice'),
678
('SearchLimitLibrary', 'homebranch', 'homebranch|holdingbranch|both', "When limiting search results with a library or library group, use the item's home library, or holding library, or both.", 'Choice'),
678
('SearchMyLibraryFirst','0',NULL,'If ON, OPAC searches return results limited by the user\'s library by default if they are logged in','YesNo'),
679
('SearchMyLibraryFirst','0',NULL,'If ON, OPAC searches return results limited by the user\'s library by default if they are logged in','YesNo'),
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref (+7 lines)
Lines 209-214 Patrons: Link Here
209
         - '<br><strong>NOTE:</strong> All patron fields can be used, but to work correctly they must contain a valid email address or an empty string.'
209
         - '<br><strong>NOTE:</strong> All patron fields can be used, but to work correctly they must contain a valid email address or an empty string.'
210
         - "Valid options are the <a href='http://schema.koha-community.org/__VERSION__/tables/borrowers.html' target='blank'>database columns</a> of the borrowers table, separated by | (pipe)."
210
         - "Valid options are the <a href='http://schema.koha-community.org/__VERSION__/tables/borrowers.html' target='blank'>database columns</a> of the borrowers table, separated by | (pipe)."
211
         - "Example: email|emailpro|B_email"
211
         - "Example: email|emailpro|B_email"
212
     -
213
         - pref: SearchCardnumberWithLDAP
214
           choices:
215
               always: Always
216
               not_found: If not found locally,
217
               never: Never
218
         - "search cardnumbers on the LDAP server. If the cardnumber is found via LDAP, the user is added or updated locally. Note : This feature only works if auth_by_bind is disabled in koha-conf.xml."
212
     -
219
     -
213
         - pref: TalkingTechItivaPhoneNotification
220
         - pref: TalkingTechItivaPhoneNotification
214
           choices:
221
           choices:
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt (+5 lines)
Lines 710-715 Link Here
710
710
711
                    [% IF ( message ) %]
711
                    [% IF ( message ) %]
712
                        [% INCLUDE 'patron-toolbar.inc' %]
712
                        [% INCLUDE 'patron-toolbar.inc' %]
713
                        [% IF (ldap_error) %]
714
                            <div class="dialog alert">
715
                                An error occured while searching for cardnumber on the LDAP server : [% ldap_error | html %]
716
                            </div>
717
                        [% END %]
713
                        <h4>No patron matched <span class="ex">[% message | html %]</span></h4>
718
                        <h4>No patron matched <span class="ex">[% message | html %]</span></h4>
714
                    [% END %]
719
                    [% END %]
715
720
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/request-article.tt (+6 lines)
Lines 94-99 Link Here
94
            <main>
94
            <main>
95
                [% INCLUDE 'messages.inc' %]
95
                [% INCLUDE 'messages.inc' %]
96
96
97
                [% IF (ldap_error) %]
98
                    <div class="dialog alert">
99
                        An error occured while searching for cardnumber on the LDAP server : [% ldap_error | html %]
100
                    </div>
101
                [% END %]
102
97
                    <h1>Request article from [% INCLUDE 'biblio-title.inc' link = 1 %]</h1>
103
                    <h1>Request article from [% INCLUDE 'biblio-title.inc' link = 1 %]</h1>
98
                    [% IF error_message %]
104
                    [% IF error_message %]
99
                        <div class="dialog alert">
105
                        <div class="dialog alert">
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt (+6 lines)
Lines 107-112 Link Here
107
                </div>
107
                </div>
108
            [% END %]
108
            [% END %]
109
109
110
            [% IF (ldap_error) %]
111
                <div class="dialog alert">
112
                    An error occured while searching for cardnumber on the LDAP server : [% ldap_error | html %]
113
                </div>
114
            [% END %]
115
110
            [% IF CAN_user_borrowers_edit_borrowers || CAN_user_tools_manage_patron_lists %]
116
            [% IF CAN_user_borrowers_edit_borrowers || CAN_user_tools_manage_patron_lists %]
111
                [% columns.unshift('checkbox') | html %]
117
                [% columns.unshift('checkbox') | html %]
112
            [% END %]
118
            [% END %]
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/reserve/request.tt (+17 lines)
Lines 168-173 Link Here
168
                </div>
168
                </div>
169
            [% END %]
169
            [% END %]
170
170
171
            [% IF ( messagetransfert ) %]
172
                <div class="dialog message">
173
                    <h2>Hold found for ([% nextreservtitle | html %]), please transfer</h2>
174
                    <p>Hold placed by : <strong> [% nextreservsurname | html %] [% nextreservfirstname | html %]</strong> at : <strong> [% branchname | html %] </strong>, Please transfer this item.
175
                    </p>
176
                    <form name="cancelReservewithtransfert" action="branchreserves.pl" method="post">
177
                        <input type="submit" class="button" />
178
                    </form>
179
                </div>
180
            [% END %]
181
182
            [% IF (ldap_error) %]
183
                <div class="dialog alert">
184
                    An error occured while searching for cardnumber on the LDAP server : [% ldap_error | html %]
185
                </div>
186
            [% END %]
187
171
            [% UNLESS ( multi_hold ) %]
188
            [% UNLESS ( multi_hold ) %]
172
                <h2>Place a hold on [% INCLUDE 'biblio-title.inc' link = 1 %] [% IF biblio.author %] by [% biblio.author | html %][% END %]</h2>
189
                <h2>Place a hold on [% INCLUDE 'biblio-title.inc' link = 1 %] [% IF biblio.author %] by [% biblio.author | html %][% END %]</h2>
173
            [% ELSE %]
190
            [% ELSE %]
(-)a/members/member.pl (+14 lines)
Lines 53-58 if ( $quicksearch and $searchmember && !$circsearch ) { Link Here
53
        $branchcode = $userenv->{'branch'};
53
        $branchcode = $userenv->{'branch'};
54
    }
54
    }
55
    my $patron = Koha::Patrons->find( { cardnumber => $searchmember } );
55
    my $patron = Koha::Patrons->find( { cardnumber => $searchmember } );
56
57
    # search cardnumber on LDAP server
58
    if ( C4::Context->config('useldapserver')
59
        and ( my $search_cardnumber = C4::Context->preference("SearchCardnumberWithLDAP") ) ne "never")
60
    {
61
        my $ldap_error;
62
        if ( $patron ) {
63
            ($patron, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($patron) if $search_cardnumber eq "always";
64
        } else {
65
            ($patron, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($searchmember);
66
        }
67
        $template->param(ldap_error => $ldap_error) if $ldap_error;
68
    }
69
56
    if (
70
    if (
57
        $patron
71
        $patron
58
        and (  ( $branchcode and $patron->branchcode eq $branchcode )
72
        and (  ( $branchcode and $patron->branchcode eq $branchcode )
(-)a/reserve/request.pl (-1 / +14 lines)
Lines 141-146 elsif ( $op eq 'cud-cancel_bulk' ) { Link Here
141
141
142
if ($findborrower) {
142
if ($findborrower) {
143
    my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
143
    my $patron = Koha::Patrons->find( { cardnumber => $findborrower } );
144
145
    # search cardnumber on LDAP server
146
    if ( C4::Context->config('useldapserver')
147
        and ( my $search_cardnumber = C4::Context->preference("SearchCardnumberWithLDAP") ) ne "never")
148
    {
149
        my $ldap_error;
150
        if ( $patron ) {
151
            ($patron, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($patron) if $search_cardnumber eq "always";
152
        } else {
153
            ($patron, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($findborrower);
154
        }
155
        $template->param(ldap_error => $ldap_error) if $ldap_error;
156
    }
157
144
    $borrowernumber_hold = $patron->borrowernumber if $patron;
158
    $borrowernumber_hold = $patron->borrowernumber if $patron;
145
}
159
}
146
160
147
- 

Return to bug 11808