--- lib/LDAP/Koha.pm 2009/03/27 16:52:05 42 +++ lib/LDAP/Koha.pm 2009/04/15 13:50:07 46 @@ -22,30 +22,66 @@ our $user = 'unconfigured-user'; our $passwd = 'unconfigured-password'; -our $max_results = 10; # 100; # FIXME +our $max_results = 3; # 100; # FIXME + +our $objectclass = 'HrEduPerson'; + +$SIG{__DIE__} = sub { + warn "!!! DIE ", @_; + die @_; +}; 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 # attributes which won't pass through DBI -my $sql_select = q{ +my $objectclass_sql = { + +HrEduPerson => q{ + select + concat('uid=',trim(userid),',dc=ffzg,dc=hr') as dn, + 'person + organizationalPerson + inetOrgPerson + hrEduPerson' as objectClass, + trim(userid) as uid, firstname as givenName, surname as sn, concat(firstname,' ',surname) as cn, -- SAFEQ specific mappings from UMgr-LDAP.conf + cardnumber as objectGUID, surname as displayName, rfid_sid as pager, email as mail, + categorycode as ou, categorycode as organizationalUnit, - borrowernumber as objectGUID, + categorycode as memberOf, + categorycode as department, concat('/home/',borrowernumber) as homeDirectory from borrowers + +}, + +organizationalUnit => q{ + + select + concat('ou=',categorycode) as dn, + 'organizationalUnit + top' as objectClass, + + hex(md5(categorycode)) % 10000 as objectGUID, + + categorycode as ou, + description as displayName + from categories + +}, }; # we need reverse LDAP -> SQL mapping for where clause @@ -57,13 +93,6 @@ 'pager' => 'rfid_sid', }; -# 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; @@ -96,11 +125,14 @@ 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 } ) { + + if ( lc $name eq 'objectclass' ) { + $objectclass = $value; + } else { push @limits, __sql_column($name) . ' = ?'; push @values, $value; } @@ -122,7 +154,7 @@ 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 ); } } @@ -142,15 +174,15 @@ my $sql_where = ''; @values = (); - foreach my $join_with ( keys %{ $reqData->{'filter'} } ) { + foreach my $filter ( keys %{ $reqData->{'filter'} } ) { - warn "## join_with $join_with\n"; + warn "## filter $filter ", dump( $reqData->{'filter'}->{ $filter } ), "\n"; @limits = (); - if ( ref $reqData->{'filter'}->{ $join_with } ) { + if ( ref $reqData->{'filter'}->{ $filter } eq 'ARRAY' ) { - foreach my $filter ( @{ $reqData->{'filter'}->{ $join_with } } ) { + foreach my $filter ( @{ $reqData->{'filter'}->{ $filter } } ) { warn "### filter ",dump($filter),$/; foreach my $how ( keys %$filter ) { if ( $how eq 'or' ) { @@ -162,10 +194,10 @@ } } - $sql_where .= ' ' . join( " $join_with ", @limits ); + $sql_where .= ' ' . join( " $filter ", @limits ); } else { - __ldap_search_to_sql( $join_with, $reqData->{'filter'}->{$join_with} ); + __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} ); } } @@ -174,7 +206,9 @@ $sql_where = " where $sql_where"; } - warn "# SQL:\n$sql_select $sql_where\n# DATA: ",dump( @values ); + my $sql_select = $objectclass_sql->{ $objectclass } || die "can't find SQL query for $objectclass"; + + 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 ); @@ -182,20 +216,16 @@ while (my $row = $sth->fetchrow_hashref) { + die "no objectClass column in $sql_select" unless defined $row->{objectClass}; + + $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n}; + warn "## row = ",dump( $row ); - my $dn = 'uid=' . $row->{uid} || die "no uid"; - $dn =~ s{[@\.]}{,dc=}g; - $dn .= ',' . $base unless $dn =~ m{dc}i; + my $dn = delete( $row->{dn} ) || die "no dn in $sql_select"; my $entry = Net::LDAP::Entry->new; $entry->dn( $dn ); - $entry->add( objectClass => [ - "person", - "organizationalPerson", - "inetOrgPerson", - "hrEduPerson", - ] ); $entry->add( %$row ); #$entry->changetype( 'modify' );