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

(-)a/C4/Auth_with_ldap.pm (+82 lines)
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
(-)a/circ/circulation.pl (-2 / +21 lines)
Lines 205-214 if ($findborrower) { Link Here
205
        }
205
        }
206
    }
206
    }
207
    if ( @$borrowers == 0 ) {
207
    if ( @$borrowers == 0 ) {
208
        $query->param( 'findborrower', '' );
208
        # Try to find a user using LDAP if we couldn't find it locally
209
        $message = "'$findborrower'";
209
        my $borrowernumber_ldap;
210
        my $ldap_error;
211
        if(C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) {
212
            ($borrowernumber_ldap, $ldap_error) = C4::Auth_with_ldap::findcardnumber_ldap($findborrower);
213
        }
214
215
        if($borrowernumber_ldap) {
216
            $borrowernumber = $borrowernumber_ldap;
217
        }
218
        else {
219
            $template->param(ldap_error => $ldap_error) if $ldap_error;
220
            $query->param( 'findborrower', '' );
221
            $message = "'$findborrower'";
222
        }
210
    }
223
    }
211
    elsif ( @$borrowers == 1 ) {
224
    elsif ( @$borrowers == 1 ) {
225
        # Update the user with LDAP if we need to
226
        if(C4::Context->config('useldapserver') and C4::Context->preference("SearchCardnumberWithLDAP")) {
227
            my ($result, $ldap_error) = C4::Auth_with_ldap::updateborrower_ldap($borrowers->[0]);
228
            $template->param(ldap_error => $ldap_error) if $ldap_error;
229
        }
230
212
        $borrowernumber = $borrowers->[0]->{'borrowernumber'};
231
        $borrowernumber = $borrowers->[0]->{'borrowernumber'};
213
        $query->param( 'borrowernumber', $borrowernumber );
232
        $query->param( 'borrowernumber', $borrowernumber );
214
        $query->param( 'barcode',           '' );
233
        $query->param( 'barcode',           '' );
(-)a/installer/data/mysql/sysprefs.sql (-1 / +2 lines)
Lines 423-427 INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` Link Here
423
('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'),
423
('XSLTResultsDisplay','default','','Enable XSL stylesheet control over results page display on intranet','Free'),
424
('yuipath','local','local|http://yui.yahooapis.com/2.5.1/build','Insert the path to YUI libraries, choose local if you use koha offline','Choice'),
424
('yuipath','local','local|http://yui.yahooapis.com/2.5.1/build','Insert the path to YUI libraries, choose local if you use koha offline','Choice'),
425
('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'),
425
('z3950AuthorAuthFields','701,702,700',NULL,'Define the MARC biblio fields for Personal Name Authorities to fill biblio.author','free'),
426
('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo')
426
('z3950NormalizeAuthor','0','','If ON, Personal Name Authorities will replace authors in biblio.author','YesNo'),
427
('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')
427
;
428
;
(-)a/installer/data/mysql/updatedatabase.pl (+9 lines)
Lines 7824-7829 if ( CheckVersion($DBversion) ) { Link Here
7824
    SetVersion($DBversion);
7824
    SetVersion($DBversion);
7825
}
7825
}
7826
7826
7827
$DBversion = "XXX";
7828
if(CheckVersion($DBversion)) {
7829
    $dbh->do(q{
7830
        INSERT INTO systempreferences (variable,value,explanation,options,type) 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');
7831
    });
7832
    print "Upgrade to $DBversion done (Added SearchCardnumberWithLDAP syspref)\n";
7833
    SetVersion($DBversion);
7834
}
7835
7827
7836
7828
=head1 FUNCTIONS
7837
=head1 FUNCTIONS
7829
7838
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/patrons.pref (+6 lines)
Lines 123-128 Patrons: Link Here
123
               now: current date.
123
               now: current date.
124
               dateexpiry: current membership expiry date.
124
               dateexpiry: current membership expiry date.
125
     -
125
     -
126
         - pref: SearchCardnumberWithLDAP
127
           choices:
128
               yes: Do
129
               no: "Don't"
130
         - 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.
131
     -
126
         - pref: TalkingTechItivaPhoneNotification
132
         - pref: TalkingTechItivaPhoneNotification
127
           choices:
133
           choices:
128
               yes: Enable
134
               yes: Enable
(-)a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt (-1 / +1 lines)
Lines 430-435 var MSG_EXPORT_SELECT_CHECKOUTS = _("You must select checkout(s) to export"); Link Here
430
430
431
[% IF ( message ) %]
431
[% IF ( message ) %]
432
[% INCLUDE 'patron-toolbar.inc' %]
432
[% INCLUDE 'patron-toolbar.inc' %]
433
[% IF ( ldap_error ) %]<h4><span>An error occured while trying to contact the LDAP server : [% ldap_error %]</span></h4>[% END %]
433
<h4>
434
<h4>
434
No patron matched <span class="ex">[% message %]</span>
435
No patron matched <span class="ex">[% message %]</span>
435
</h4>
436
</h4>
436
- 

Return to bug 11808