X-Git-Url: http://git.rot13.org/?p=virtual-ldap;a=blobdiff_plain;f=lib%2FLDAP%2FKoha.pm;h=f3527d7bf33f2ab60b7c80153a243505272569b9;hp=b738c55d69f3242b429d95e407777d22ac73d1a6;hb=40a5da8b24fa4f79699fee8d7c10eb5c718ccf73;hpb=93928e3aef0157263201341fd7db2a59ea821b3e;ds=sidebyside diff --git a/lib/LDAP/Koha.pm b/lib/LDAP/Koha.pm index b738c55..f3527d7 100644 --- a/lib/LDAP/Koha.pm +++ b/lib/LDAP/Koha.pm @@ -22,6 +22,8 @@ our $database = 'koha'; our $user = 'unconfigured-user'; our $passwd = 'unconfigured-password'; +our $max_results = 10; # 100; # FIXME + require 'config.pl' if -e 'config.pl'; my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr; @@ -29,17 +31,43 @@ my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoC # Net::LDAP::Entry will lc all our attribute names anyway, so # we don't really care about correctCapitalization for LDAP # attributes which won't pass through DBI -my $sth = $dbh->prepare(q{ +my $sql_select = q{ select - userid as uid, - firstname as givenName, - surname as sn, - cardnumber as otherPager, - email as mail + trim(userid) as uid, + firstname as givenName, + surname as sn, + concat(firstname,' ',surname) as cn, + + -- SAFEQ specific mappings from UMgr-LDAP.conf + surname as displayName, + cardnumber as pager, + email as mail, + categorycode as organizationalUnit, + borrowernumber as objectGUID, + concat('/home/',borrowernumber) as homeDirectory from borrowers - where - cardnumber = ? -}); +}; + +# we need reverse LDAP -> SQL mapping for where clause +my $ldap_sql_mapping = { + 'uid' => 'userid', + 'objectGUID' => 'borrowernumber', + 'displayName' => 'surname', + 'sn' => 'surname', + 'pager' => 'cardnumber', +}; + +# attributes which are same for whole set, but somehow +# LDAP clients are sending they anyway and we don't +# have them in database +my $ldap_ignore = { + 'objectclass' => 1, +}; + +sub __sql_column { + my $name = shift; + $ldap_sql_mapping->{$name} || $name; +} use constant RESULT_OK => { 'matchedDN' => '', @@ -63,24 +91,88 @@ sub bind { return RESULT_OK; } +our @values; +our @limits; + +sub __ldap_search_to_sql { + my ( $how, $what ) = @_; + warn "### how $how\n"; + if ( $how eq 'equalityMatch' && defined $what ) { + my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?"; + my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?"; + if ( ! $ldap_ignore->{ $name } ) { + push @limits, __sql_column($name) . ' = ?'; + push @values, $value; + } + } elsif ( $how eq 'substrings' ) { + foreach my $substring ( @{ $what->{substrings} } ) { + my $name = $what->{type} || warn "ERROR: no type?"; + while ( my($op,$value) = each %$substring ) { + push @limits, __sql_column($name) . ' LIKE ?'; + if ( $op eq 'any' ) { + $value = '%' . $value . '%'; + } else { + warn "UNSUPPORTED: op $op - using plain $value"; + } + push @values, $value; + } + } + } elsif ( $how eq 'present' ) { + my $name = __sql_column( $what ); + push @limits, "$name IS NOT NULL and length($name) > 1"; + ## XXX length(foo) > 1 to avoid empty " " strings + } else { + warn "UNSUPPORTED: how $how what ",dump( $what ); + } +} + # the search operation sub search { my $self = shift; my $reqData = shift; print "searching...\n"; - warn "# request = ", dump($reqData); + warn "# " . localtime() . " request = ", dump($reqData); my $base = $reqData->{'baseObject'}; # FIXME use it? my @entries; - if ( $reqData->{'filter'}->{'equalityMatch'}->{'attributeDesc'} eq 'otherPager' ) { + if ( $reqData->{'filter'} ) { + + my $sql_where = ''; + @values = (); + + foreach my $join_with ( keys %{ $reqData->{'filter'} } ) { + + warn "## join_with $join_with\n"; + + @limits = (); - my $value = $reqData->{'filter'}->{'equalityMatch'}->{'assertionValue'} || die "no value?"; + foreach my $filter ( @{ $reqData->{'filter'}->{ $join_with } } ) { + warn "### filter ",dump($filter),$/; + foreach my $how ( keys %$filter ) { + if ( $how eq 'or' ) { + __ldap_search_to_sql( %$_ ) foreach ( @{ $filter->{$how} } ); + } else { + __ldap_search_to_sql( $how, $filter->{$how} ); + } + warn "## limits ",dump(@limits), " values ",dump(@values); + } + } + + $sql_where .= ' ' . join( " $join_with ", @limits ); + + } + + if ( $sql_where ) { + $sql_where = " where $sql_where"; + } - $sth->execute( $value ); + warn "# SQL:\n$sql_select $sql_where\n# DATA: ",dump( @values ); + my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit? + $sth->execute( @values ); - warn "# ", $sth->rows, " results for: $value\n"; + warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} ); while (my $row = $sth->fetchrow_hashref) { @@ -90,10 +182,10 @@ sub search { $dn =~ s{[@\.]}{,dc=}g; my $entry = Net::LDAP::Entry->new; - $entry->dn( $dn ); + $entry->dn( $dn . $base ); $entry->add( %$row ); - #warn "## entry ",dump( $entry ); + #warn "### entry ",dump( $entry ); push @entries, $entry; }