X-Git-Url: http://git.rot13.org/?p=virtual-ldap;a=blobdiff_plain;f=lib%2FLDAP%2FKoha.pm;h=563c9685af5991c2c87c6bf73e19354ff039df5f;hp=fccc38f99894ae987601cce436fab4b245125df1;hb=ba8dc579a3496f2785ca5bc12cb291c1d489dc73;hpb=73b88761ef8b4801dddb2a8117dcaab4b98a9545 diff --git a/lib/LDAP/Koha.pm b/lib/LDAP/Koha.pm index fccc38f..563c968 100644 --- a/lib/LDAP/Koha.pm +++ b/lib/LDAP/Koha.pm @@ -15,6 +15,8 @@ use File::Slurp; use Data::Dump qw/dump/; +my $debug = 0; # XXX very slow + # XXX test with: # # ldapsearch -h localhost -p 2389 -b dc=ffzg,dc=hr -x 'otherPager=200903160021' @@ -25,9 +27,10 @@ our $database = 'koha'; our $user = 'unconfigured-user'; our $passwd = 'unconfigured-password'; -our $max_results = 1500; # 100; # FIXME +our $max_results = $ENV{MAX_RESULTS} || 3000; # FIXME must be enough for all users +our $objectclass_default = 'hrEduPerson'; -our $objectclass = 'HrEduPerson'; +our $objectclass; $SIG{__DIE__} = sub { warn "!!! DIE ", @_; @@ -36,16 +39,14 @@ $SIG{__DIE__} = sub { require 'config.pl' if -e 'config.pl'; -my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; - # we need reverse LDAP -> SQL mapping for where clause my $ldap_sql_mapping = { 'uid' => 'userid', - 'objectGUID' => 'borrowernumber', + 'objectGUID' => 'b.borrowernumber', 'displayName' => 'surname', 'sn' => 'surname', - 'pager' => 'rfid_sid', + 'pager' => qq{replace(a.attribute, '\r','')}, # was: rfid_sid }; sub __sql_column { @@ -119,23 +120,23 @@ sub __ldap_search_to_sql { sub _dn_attributes { my ($row,$base) = @_; - warn "## row = ",dump( $row ); + warn "## row = ",dump( $row ) if $debug; die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass}; $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n}; - warn "## row = ",dump( $row ); + warn "## row = ",dump( $row ) if $debug; my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row ); # this does some sanity cleanup for our data - my $base_as_domain = $base; - $base_as_domain =~ s{dn=}{.}; - $base_as_domain =~ s{^\.}{@}; - $dn =~ s{$base_as_domain$}{}; - - $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present +# my $base_as_domain = $base; +# $base_as_domain =~ s{dn=}{.}; +# $base_as_domain =~ s{^\.}{@}; +# $dn =~ s{$base_as_domain$}{}; +# +# $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present return ($dn, $row); } @@ -156,6 +157,7 @@ sub search { my $sql_where = ''; @values = (); + $objectclass = ''; foreach my $filter ( keys %{ $reqData->{'filter'} } ) { @@ -177,22 +179,35 @@ sub search { } } - $sql_where .= ' ' . join( " $filter ", @limits ); - } else { __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} ); } + $sql_where .= ' ' . join( " $filter ", @limits ) if @limits; + } + $objectclass ||= $objectclass_default; + + my $sql_select = read_file( lc "sql/$objectclass.sql" ); if ( $sql_where ) { - $sql_where = " where $sql_where"; + if ( $sql_select !~ m{where}i ) { + $sql_where = " where $sql_where"; + } else { + $sql_where = " and $sql_where"; + } } - my $sql_select = read_file( lc "sql/$objectclass.sql" ); - warn "# SQL:\n$sql_select\n", $sql_where ? $sql_where : '-- no where', "\n# DATA: ",dump( @values ); - my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit? + my $sql + = $sql_select + . $sql_where +# . ( $objectclass =~ m{person}i ? " LIMIT $max_results" : '' ) # add limit just for persons + ; + + warn "# SQL:\n$sql\n# DATA: ",dump( @values ); + my $dbh = DBI->connect_cached($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr; + my $sth = $dbh->prepare( $sql ); $sth->execute( @values ); warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} ); @@ -204,17 +219,19 @@ sub search { my ( $dn, $attributes ) = _dn_attributes( $row, $base ); - warn "# dn $last_dn ... $dn\n"; + warn "## dn $last_dn ... $dn\n" if $debug; if ( $dn ne $last_dn ) { if ( $entry ) { #$entry->changetype( 'modify' ); - warn "### entry ",$entry->dump( \*STDERR ); + warn "### entry ",$entry->dump( \*STDERR ) if $debug; push @entries, $entry; undef $entry; } + $dn =~ s{@[^,]+}{}; + $entry = Net::LDAP::Entry->new; $entry->dn( $dn ); @@ -223,7 +240,7 @@ sub search { } else { foreach my $n ( keys %$attributes ) { my $v = $attributes->{$n}; - warn "# attr $n = $v\n"; + warn "## attr $n = $v\n" if $debug; $entry->add( $n, $v ) if $entry->get_value( $n ) ne $v; } } @@ -233,8 +250,10 @@ sub search { } - warn "### last entry ",$entry->dump( \*STDERR ); - push @entries, $entry; + if ( $entry ) { + warn "### last entry ",$entry->dump( \*STDERR ) if $debug; + push @entries, $entry; + } } else { warn "UNKNOWN request: ",dump( $reqData );