/[virtual-ldap]/lib/LDAP/Koha.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/LDAP/Koha.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 46 by dpavlin, Wed Apr 15 13:50:07 2009 UTC revision 51 by dpavlin, Wed Apr 29 09:18:11 2009 UTC
# Line 2  package LDAP::Koha; Line 2  package LDAP::Koha;
2    
3  use strict;  use strict;
4  use warnings;  use warnings;
 use Data::Dump qw/dump/;  
5    
6  use lib '../lib';  use lib '../lib';
7    
8  use Net::LDAP::Constant qw(LDAP_SUCCESS);  use Net::LDAP::Constant qw(LDAP_SUCCESS);
9  use Net::LDAP::Server;  use Net::LDAP::Server;
10  use base 'Net::LDAP::Server';  use base 'Net::LDAP::Server';
11  use fields qw();  use fields qw();
12    
13  use DBI;  use DBI;
14    use File::Slurp;
15    
16    use Data::Dump qw/dump/;
17    
18  # XXX test with:  # XXX test with:
19  #  #
# Line 22  our $database = 'koha'; Line 25  our $database = 'koha';
25  our $user     = 'unconfigured-user';  our $user     = 'unconfigured-user';
26  our $passwd   = 'unconfigured-password';  our $passwd   = 'unconfigured-password';
27    
28  our $max_results = 3; # 100; # FIXME  our $max_results = 3000; # FIXME must be enough for all users
29    our $objectclass_default = 'hrEduPerson';
30    
31  our $objectclass = 'HrEduPerson';  our $objectclass;
32    
33  $SIG{__DIE__} = sub {  $SIG{__DIE__} = sub {
34          warn "!!! DIE ", @_;          warn "!!! DIE ", @_;
# Line 35  require 'config.pl' if -e 'config.pl'; Line 39  require 'config.pl' if -e 'config.pl';
39    
40  my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;  my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
41    
 # 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 $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,  
                 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  
   
 },  
 };  
   
42  # we need reverse LDAP -> SQL mapping for where clause  # we need reverse LDAP -> SQL mapping for where clause
43    
44  my $ldap_sql_mapping = {  my $ldap_sql_mapping = {
45          'uid'           => 'userid',          'uid'           => 'userid',
46          'objectGUID'    => 'borrowernumber',          'objectGUID'    => 'borrowernumber',
# Line 158  sub __ldap_search_to_sql { Line 114  sub __ldap_search_to_sql {
114          }          }
115  }  }
116    
117    
118    # my ( $dn,$attributes ) = _dn_attributes( $row, $base );
119    
120    sub _dn_attributes {
121            my ($row,$base) = @_;
122    
123            warn "## row = ",dump( $row );
124    
125            die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass};
126    
127            $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};
128    
129            warn "## row = ",dump( $row );
130    
131            my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row );
132    
133            # this does some sanity cleanup for our data
134            my $base_as_domain = $base;
135            $base_as_domain =~ s{dn=}{.};
136            $base_as_domain =~ s{^\.}{@};
137            $dn =~ s{$base_as_domain$}{};
138    
139            $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present
140    
141            return ($dn, $row);
142    }
143    
144    
145  # the search operation  # the search operation
146  sub search {  sub search {
147          my $self = shift;          my $self = shift;
# Line 173  sub search { Line 157  sub search {
157    
158                  my $sql_where = '';                  my $sql_where = '';
159                  @values = ();                  @values = ();
160                    $objectclass = '';
161    
162                  foreach my $filter ( keys %{ $reqData->{'filter'} } ) {                  foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
163    
# Line 194  sub search { Line 179  sub search {
179                                          }                                          }
180                                  }                                  }
181    
                                 $sql_where .= ' ' . join( " $filter ", @limits );  
   
182                          } else {                          } else {
183                                  __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );                                  __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
184                          }                          }
185    
186                            $sql_where .= ' ' . join( " $filter ", @limits ) if @limits;
187    
188                  }                  }
189    
190                    $objectclass ||= $objectclass_default;
191    
192                    my $sql_select = read_file( lc "sql/$objectclass.sql" );
193                  if ( $sql_where ) {                  if ( $sql_where ) {
194                          $sql_where = " where $sql_where";                          if ( $sql_select !~ m{where}i ) {
195                                    $sql_where = " where $sql_where";
196                            } else {
197                                    $sql_where = " and $sql_where";
198                            }
199                  }                  }
200    
                 my $sql_select = $objectclass_sql->{ $objectclass } || die "can't find SQL query for $objectclass";  
201    
202                  warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values );                  warn "# SQL:\n$sql_select\n", $sql_where ? $sql_where : '-- no where', "\n# DATA: ",dump( @values );
203                  my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?                  my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?
204                  $sth->execute( @values );                  $sth->execute( @values );
205    
206                  warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );                  warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
207    
208                    my $last_dn = '?';
209                    my $entry;
210    
211                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
212    
213                          die "no objectClass column in $sql_select" unless defined $row->{objectClass};                          my ( $dn, $attributes ) = _dn_attributes( $row, $base );
214    
215                            warn "# dn $last_dn ... $dn\n";
216    
217                          $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};                          if ( $dn ne $last_dn ) {
218    
219                                    if ( $entry ) {
220                                            #$entry->changetype( 'modify' );
221                                            warn "### entry ",$entry->dump( \*STDERR );
222                                            push @entries, $entry;
223                                            undef $entry;
224                                    }
225    
226                          warn "## row = ",dump( $row );                                  $entry = Net::LDAP::Entry->new;
227                                    $entry->dn( $dn );
228    
229                          my $dn = delete( $row->{dn} ) || die "no dn in $sql_select";                                  $entry->add( %$attributes );
230    
231                          my $entry = Net::LDAP::Entry->new;                          } else {
232                          $entry->dn( $dn );                                  foreach my $n ( keys %$attributes ) {
233                          $entry->add( %$row );                                          my $v = $attributes->{$n};
234                                            warn "# attr $n = $v\n";
235                                            $entry->add( $n, $v ) if $entry->get_value( $n ) ne $v;
236                                    }
237                            }
238    
                         #$entry->changetype( 'modify' );  
239    
240                          warn "### entry ",$entry->dump( \*STDERR );                          $last_dn = $dn;
241    
                         push @entries, $entry;  
242                  }                  }
243    
244                    warn "### last entry ",$entry->dump( \*STDERR );
245                    push @entries, $entry;
246    
247          } else {          } else {
248                  warn "UNKNOWN request: ",dump( $reqData );                  warn "UNKNOWN request: ",dump( $reqData );
249          }          }

Legend:
Removed from v.46  
changed lines
  Added in v.51

  ViewVC Help
Powered by ViewVC 1.1.26