dump a lot more debugging information
[virtual-ldap] / lib / LDAP / Koha.pm
index f3527d7..8e3a1e7 100644 (file)
@@ -26,7 +26,7 @@ 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;
+my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
 
 # Net::LDAP::Entry will lc all our attribute names anyway, so
 # we don't really care about correctCapitalization for LDAP
@@ -40,9 +40,12 @@ my $sql_select = q{
 
                -- SAFEQ specific mappings from UMgr-LDAP.conf
                surname                                         as displayName,
-               cardnumber                                      as pager,
+               rfid_sid                                        as pager,
                email                                           as mail,
+               categorycode                                    as ou,
                categorycode                                    as organizationalUnit,
+               categorycode                                    as memberOf,
+               categorycode                                    as department,
                borrowernumber                                  as objectGUID,
                concat('/home/',borrowernumber)                 as homeDirectory
        from borrowers
@@ -54,7 +57,7 @@ my $ldap_sql_mapping = {
        'objectGUID'    => 'borrowernumber',
        'displayName'   => 'surname',
        'sn'            => 'surname',
-       'pager'         => 'cardnumber',
+       'pager'         => 'rfid_sid',
 };
 
 # attributes which are same for whole set, but somehow
@@ -96,13 +99,15 @@ our @limits;
 
 sub __ldap_search_to_sql {
        my ( $how, $what ) = @_;
-       warn "### how $how\n";
+       warn "### __ldap_search_to_sql $how ",dump( $what ),"\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;
+               } else {
+                       warn "IGNORED: $name = $value";
                }
        } elsif ( $how eq 'substrings' ) {
                foreach my $substring ( @{ $what->{substrings} } ) {
@@ -122,7 +127,7 @@ sub __ldap_search_to_sql {
                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 );
+               warn "UNSUPPORTED: $how ",dump( $what );
        }
 }
 
@@ -144,23 +149,29 @@ sub search {
 
                foreach my $join_with ( keys %{ $reqData->{'filter'} } ) {
 
-                       warn "## join_with $join_with\n";
+                       warn "## join_with $join_with ", dump( $reqData->{'filter'}->{ $join_with } ), "\n";
 
                        @limits = ();
 
-                       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} );
+                       if ( ref $reqData->{'filter'}->{ $join_with } eq 'ARRAY' ) {
+
+                               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);
                                        }
-                                       warn "## limits ",dump(@limits), " values ",dump(@values);
                                }
-                       }
 
-                       $sql_where .= ' ' . join( " $join_with ", @limits );
+                               $sql_where .= ' ' . join( " $join_with ", @limits );
+
+                       } else {
+                               __ldap_search_to_sql( $join_with, $reqData->{'filter'}->{$join_with} );
+                       }
 
                }
 
@@ -168,7 +179,7 @@ sub search {
                        $sql_where = " where $sql_where";
                }
 
-               warn "# SQL:\n$sql_select $sql_where\n# DATA: ",dump( @values );
+               warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values );
                my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?
                $sth->execute( @values );
 
@@ -180,12 +191,21 @@ sub search {
 
                        my $dn = 'uid=' . $row->{uid} || die "no uid";
                        $dn =~ s{[@\.]}{,dc=}g;
+                       $dn .= ',' . $base unless $dn =~ m{dc}i;
 
                        my $entry = Net::LDAP::Entry->new;
-                       $entry->dn( $dn . $base );
+                       $entry->dn( $dn );
+                       $entry->add( objectClass => [
+                               "person",
+                               "organizationalPerson",
+                               "inetOrgPerson",
+                               "hrEduPerson",
+                       ] );
                        $entry->add( %$row );
 
-                       #warn "### entry ",dump( $entry );
+                       #$entry->changetype( 'modify' );
+
+                       warn "### entry ",$entry->dump( \*STDERR );
 
                        push @entries, $entry;
                }