/[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 44 by dpavlin, Wed Apr 15 11:06:27 2009 UTC revision 62 by dpavlin, Thu Feb 18 16:22:32 2010 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    my $debug = 0; # XXX very slow
19    
20  # XXX test with:  # XXX test with:
21  #  #
# Line 22  our $database = 'koha'; Line 27  our $database = 'koha';
27  our $user     = 'unconfigured-user';  our $user     = 'unconfigured-user';
28  our $passwd   = 'unconfigured-password';  our $passwd   = 'unconfigured-password';
29    
30  our $max_results = 10; # 100; # FIXME  our $max_results = $ENV{MAX_RESULTS} || 3000; # FIXME must be enough for all users
31    our $objectclass_default = 'hrEduPerson';
32    
33    our $objectclass;
34    
35    $SIG{__DIE__} = sub {
36            warn "!!! DIE ", @_;
37            die @_;
38    };
39    
40  require 'config.pl' if -e 'config.pl';  require 'config.pl' if -e 'config.pl';
41    
42  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;
43    
 # 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{  
         select  
                 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,  
                 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  
 };  
   
44  # we need reverse LDAP -> SQL mapping for where clause  # we need reverse LDAP -> SQL mapping for where clause
45    
46  my $ldap_sql_mapping = {  my $ldap_sql_mapping = {
47          'uid'           => 'userid',          'uid'           => 'userid',
48          'objectGUID'    => 'borrowernumber',          'objectGUID'    => 'borrowernumber',
49          'displayName'   => 'surname',          'displayName'   => 'surname',
50          'sn'            => 'surname',          'sn'            => 'surname',
51          'pager'         => 'rfid_sid',          'pager'         => 'a.attribute',       # was: 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,  
52  };  };
53    
54  sub __sql_column {  sub __sql_column {
# Line 103  sub __ldap_search_to_sql { Line 87  sub __ldap_search_to_sql {
87          if ( $how eq 'equalityMatch' && defined $what ) {          if ( $how eq 'equalityMatch' && defined $what ) {
88                  my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";                  my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";
89                  my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";                  my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";
90                  if ( ! $ldap_ignore->{ $name } ) {  
91                    if ( lc $name eq 'objectclass' ) {
92                            $objectclass = $value;
93                    } else {
94                          push @limits, __sql_column($name) . ' = ?';                          push @limits, __sql_column($name) . ' = ?';
95                          push @values, $value;                          push @values, $value;
                 } else {  
                         warn "IGNORED: $name = $value";  
96                  }                  }
97          } elsif ( $how eq 'substrings' ) {          } elsif ( $how eq 'substrings' ) {
98                  foreach my $substring ( @{ $what->{substrings} } ) {                  foreach my $substring ( @{ $what->{substrings} } ) {
# Line 131  sub __ldap_search_to_sql { Line 116  sub __ldap_search_to_sql {
116          }          }
117  }  }
118    
119    
120    # my ( $dn,$attributes ) = _dn_attributes( $row, $base );
121    
122    sub _dn_attributes {
123            my ($row,$base) = @_;
124    
125            warn "## row = ",dump( $row ) if $debug;
126    
127            die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass};
128    
129            $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};
130    
131            warn "## row = ",dump( $row ) if $debug;
132    
133            my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row );
134    
135            # this does some sanity cleanup for our data
136    #       my $base_as_domain = $base;
137    #       $base_as_domain =~ s{dn=}{.};
138    #       $base_as_domain =~ s{^\.}{@};
139    #       $dn =~ s{$base_as_domain$}{};
140    #
141    #       $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present
142    
143            return ($dn, $row);
144    }
145    
146    
147  # the search operation  # the search operation
148  sub search {  sub search {
149          my $self = shift;          my $self = shift;
# Line 146  sub search { Line 159  sub search {
159    
160                  my $sql_where = '';                  my $sql_where = '';
161                  @values = ();                  @values = ();
162                    $objectclass = '';
163    
164                  foreach my $join_with ( keys %{ $reqData->{'filter'} } ) {                  foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
165    
166                          warn "## join_with $join_with ", dump( $reqData->{'filter'}->{ $join_with } ), "\n";                          warn "## filter $filter ", dump( $reqData->{'filter'}->{ $filter } ), "\n";
167    
168                          @limits = ();                          @limits = ();
169    
170                          if ( ref $reqData->{'filter'}->{ $join_with } eq 'ARRAY' ) {                          if ( ref $reqData->{'filter'}->{ $filter } eq 'ARRAY' ) {
171    
172                                  foreach my $filter ( @{ $reqData->{'filter'}->{ $join_with } } ) {                                  foreach my $filter ( @{ $reqData->{'filter'}->{ $filter } } ) {
173                                          warn "### filter ",dump($filter),$/;                                          warn "### filter ",dump($filter),$/;
174                                          foreach my $how ( keys %$filter ) {                                          foreach my $how ( keys %$filter ) {
175                                                  if ( $how eq 'or' ) {                                                  if ( $how eq 'or' ) {
# Line 167  sub search { Line 181  sub search {
181                                          }                                          }
182                                  }                                  }
183    
                                 $sql_where .= ' ' . join( " $join_with ", @limits );  
   
184                          } else {                          } else {
185                                  __ldap_search_to_sql( $join_with, $reqData->{'filter'}->{$join_with} );                                  __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
186                          }                          }
187    
188                            $sql_where .= ' ' . join( " $filter ", @limits ) if @limits;
189    
190                  }                  }
191    
192                    $objectclass ||= $objectclass_default;
193    
194                    my $sql_select = read_file( lc "sql/$objectclass.sql" );
195                  if ( $sql_where ) {                  if ( $sql_where ) {
196                          $sql_where = " where $sql_where";                          if ( $sql_select !~ m{where}i ) {
197                                    $sql_where = " where $sql_where";
198                            } else {
199                                    $sql_where = " and $sql_where";
200                            }
201                  }                  }
202    
203                  warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values );  
204                  my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?                  my $sql
205                            = $sql_select
206                            . $sql_where
207    #                       . ( $objectclass =~ m{person}i ? " LIMIT $max_results" : '' ) # add limit just for persons
208                            ;
209    
210                    warn "# SQL:\n$sql\n# DATA: ",dump( @values );
211                    my $sth = $dbh->prepare( $sql );
212                  $sth->execute( @values );                  $sth->execute( @values );
213    
214                  warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );                  warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
215    
216                    my $last_dn = '?';
217                    my $entry;
218    
219                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
220    
221                          warn "## row = ",dump( $row );                          my ( $dn, $attributes ) = _dn_attributes( $row, $base );
222    
223                          my $dn = 'uid=' . $row->{uid} || die "no uid";                          warn "# dn $last_dn ... $dn\n";
                         $dn =~ s{[@\.]}{,dc=}g;  
                         $dn .= ',' . $base unless $dn =~ m{dc}i;  
   
                         my $entry = Net::LDAP::Entry->new;  
                         $entry->dn( $dn );  
                         $entry->add( objectClass => [  
                                 "person",  
                                 "organizationalPerson",  
                                 "inetOrgPerson",  
                                 "hrEduPerson",  
                         ] );  
                         $entry->add( %$row );  
224    
225                          #$entry->changetype( 'modify' );                          if ( $dn ne $last_dn ) {
226    
227                          warn "### entry ",$entry->dump( \*STDERR );                                  if ( $entry ) {
228                                            #$entry->changetype( 'modify' );
229                                            warn "### entry ",$entry->dump( \*STDERR );
230                                            push @entries, $entry;
231                                            undef $entry;
232                                    }
233    
234                                    $dn =~ s{@[^,]+}{};
235    
236                                    $entry = Net::LDAP::Entry->new;
237                                    $entry->dn( $dn );
238    
239                                    $entry->add( %$attributes );
240    
241                            } else {
242                                    foreach my $n ( keys %$attributes ) {
243                                            my $v = $attributes->{$n};
244                                            warn "# attr $n = $v\n";
245                                            $entry->add( $n, $v ) if $entry->get_value( $n ) ne $v;
246                                    }
247                            }
248    
249    
250                            $last_dn = $dn;
251    
252                    }
253    
254                    if ( $entry ) {
255                            warn "### last entry ",$entry->dump( \*STDERR );
256                          push @entries, $entry;                          push @entries, $entry;
257                  }                  }
258    

Legend:
Removed from v.44  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.26