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

(-)a/misc/cronjobs/update_patrons_from_LDAP.pl (-1 / +103 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
# Copyright 2012 BibLibre
3
#
4
# This file is part of Koha.
5
#
6
# Koha is free software; you can redistribute it and/or modify it under the
7
# terms of the GNU General Public License as published by the Free Software
8
# Foundation; either version 2 of the License, or (at your option) any later
9
# version.
10
#
11
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
12
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License along
16
# with Koha; if not, write to the Free Software Foundation, Inc.,
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
19
=head1 NAME
20
21
update_patrons_from_LDAP.pl - cron script to pre-populate borrowers data from LDAP
22
23
=head1 SYNOPSIS
24
25
./update_patrons_from_LDAP.pl 
26
27
or, in crontab:
28
0 1 * * * ${PERL5LIB}/misc/cronjobs/update_patrons_from_LDAP.pl
29
30
=head1 DESCRIPTION
31
32
This script preload borrowers data from LDAP 
33
It therefore uses a user that has read permission on user data.
34
Or performs an anonymous login, and then uses the data to preload users.
35
36
37
=cut
38
39
use Net::LDAP;
40
use Net::LDAP::Filter;
41
use C4::Auth_with_ldap;
42
use C4::Members;
43
use C4::Context;
44
use C4::Debug;
45
use YAML;
46
use strict;
47
48
my $dbh = C4::Context->dbh;
49
my $ldap     = C4::Context->config("ldapserver") or die 'No "ldapserver" in server hash from KOHA_CONF: ' . $ENV{KOHA_CONF};
50
my $prefhost = $ldap->{hostname}                 or die ldapserver_error('hostname');
51
my $base     = $ldap->{base}                     or die ldapserver_error('base');
52
my $ldapname = $ldap->{user};
53
my $ldappassword = $ldap->{pass};
54
our %mapping = %{ $ldap->{mapping} };
55
$debug && warn Dump($ldap);
56
57
# Infos to do an anonymous bind
58
59
my %config = (
60
    anonymous => ( $ldapname and $ldappassword ) ? 0 : 1,
61
    replicate => defined( $ldap->{replicate} ) ? $ldap->{replicate} : 1,    #    add from LDAP to Koha database for new user
62
    update    => defined( $ldap->{update} )    ? $ldap->{update}    : 1,    # update from LDAP to Koha database for existing user
63
);
64
65
my @hosts = split( ',', $prefhost );
66
my $db = Net::LDAP->new( \@hosts );
67
unless ($db) {
68
    warn " host $ldap doesnot exist !";
69
    return 0;
70
}
71
my $res = ( $config{anonymous} ) ? $db->bind : $db->bind( $ldapname, password => $ldappassword );
72
warn "LDAP Auth impossible : server not responding" if ( $res->code );
73
74
## based on Koha Members
75
my $filter = Net::LDAP::Filter->new("uid=*");
76
my $userdnsearch = $db->search( base => $base, filter => $filter );
77
$userdnsearch->code and die $userdnsearch->error;
78
while ( my $userldapentry = $userdnsearch->shift_entry ) {
79
80
    #warn Data::Dumper::Dumper($userldapentry);
81
    my $uid_field = $mapping{userid}->{is} or die ldapserver_error("mapping for 'userid'");
82
    my $userid = $userldapentry->{$uid_field};
83
    $debug && warn Dump($userldapentry);
84
85
    my %borrower = C4::Auth_with_ldap::ldap_entry_2_hash( $userldapentry, $userid );
86
    $debug and print "checkpw_ldap received \%borrower w/ " . keys(%borrower), " keys: ", join( ' ', keys %borrower ), "\n";
87
    my ( $borrowernumber, $cardnumber, $savedpw );
88
    ( $borrowernumber, $cardnumber, $userid, $savedpw ) = C4::Auth_with_ldap::exists_local( $borrower{'userid'} );
89
    if ($borrowernumber) {
90
        ( $config{update} ) and my $c2 = C4::Auth_with_ldap::update_local( $userid, "!", $borrowernumber, \%borrower ) || '';
91
        ( $cardnumber eq $c2 ) or warn "update_local returned cardnumber '$c2' instead of '$cardnumber'";
92
    } else {
93
        if ( $config{replicate} ) {
94
            $borrowernumber = eval { AddMember(%borrower) };
95
            if ($@) {
96
                warn Dump(%borrower), " $@";
97
            }
98
        }
99
    }
100
}
101
$db->unbind;
102
#Pb a) Search the whole annuary
103
# b) Adds data that could be useless in Koha.

Return to bug 8123