/[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 38 by dpavlin, Wed Mar 25 22:06:00 2009 UTC revision 47 by dpavlin, Fri Apr 17 21:39:45 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 = 15; # 100; # FIXME
   
 require 'config.pl' if -e 'config.pl';  
29    
30  my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 0 }) || die $DBI::errstr;  our $objectclass = 'HrEduPerson';
31    
32  # Net::LDAP::Entry will lc all our attribute names anyway, so  $SIG{__DIE__} = sub {
33  # we don't really care about correctCapitalization for LDAP          warn "!!! DIE ", @_;
34  # attributes which won't pass through DBI          die @_;
 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  
                 concat(firstname,' ',surname)   as displayName,  
                 cardnumber                                              as otherPager,  
                 email                                                   as mail,  
                 categorycode                                    as organizationalUnit,  
                 borrowernumber                                  as objectGUID,  
                 concat('/home/',borrowernumber) as homeDirectory  
         from borrowers  
35  };  };
36    
37  # needed for where clause  require 'config.pl' if -e 'config.pl';
 my $sql_ldap_mapping = {  
         'userid'                        => 'uid',  
         'borrowernumber'        => 'objectGUID',  
 };  
38    
39  # attributes which are same for whole set, but somehow  my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
 # LDAP clients are sending they anyway and we don't  
 # have them in database  
 my $ldap_ignore = {  
         'objectclass' => 1,  
 };  
40    
41  my $ldap_sql_mapping;  # we need reverse LDAP -> SQL mapping for where clause
42  while ( my ($sql,$ldap) = each %$sql_ldap_mapping ) {  
43          $ldap_sql_mapping->{ $ldap } = $sql;  my $ldap_sql_mapping = {
44  }          'uid'           => 'userid',
45            'objectGUID'    => 'borrowernumber',
46            'displayName'   => 'surname',
47            'sn'            => 'surname',
48            'pager'         => 'rfid_sid',
49    };
50    
51  sub __sql_column {  sub __sql_column {
52          my $name = shift;          my $name = shift;
# Line 93  sub bind { Line 75  sub bind {
75          return RESULT_OK;          return RESULT_OK;
76  }  }
77    
78    our @values;
79    our @limits;
80    
81    sub __ldap_search_to_sql {
82            my ( $how, $what ) = @_;
83            warn "### __ldap_search_to_sql $how ",dump( $what ),"\n";
84            if ( $how eq 'equalityMatch' && defined $what ) {
85                    my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";
86                    my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";
87    
88                    if ( lc $name eq 'objectclass' ) {
89                            $objectclass = $value;
90                    } else {
91                            push @limits, __sql_column($name) . ' = ?';
92                            push @values, $value;
93                    }
94            } elsif ( $how eq 'substrings' ) {
95                    foreach my $substring ( @{ $what->{substrings} } ) {
96                            my $name = $what->{type} || warn "ERROR: no type?";
97                            while ( my($op,$value) = each %$substring ) {
98                                    push @limits, __sql_column($name) . ' LIKE ?';
99                                    if ( $op eq 'any' ) {
100                                            $value = '%' . $value . '%';
101                                    } else {
102                                            warn "UNSUPPORTED: op $op - using plain $value";
103                                    }
104                                    push @values, $value;
105                            }
106                    }
107            } elsif ( $how eq 'present' ) {
108                    my $name = __sql_column( $what );
109                    push @limits, "$name IS NOT NULL and length($name) > 1";
110                    ## XXX length(foo) > 1 to avoid empty " " strings
111            } else {
112                    warn "UNSUPPORTED: $how ",dump( $what );
113            }
114    }
115    
116    
117    # my ( $dn,$attributes ) = _dn_attributes( $row, $base );
118    
119    sub _dn_attributes {
120            my ($row,$base) = @_;
121    
122            warn "## row = ",dump( $row );
123    
124            die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass};
125    
126            $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};
127    
128            warn "## row = ",dump( $row );
129    
130            my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row );
131    
132            # this does some sanity cleanup for our data
133            my $base_as_domain = $base;
134            $base_as_domain =~ s{dn=}{.};
135            $base_as_domain =~ s{^\.}{@};
136            $dn =~ s{$base_as_domain$}{};
137    
138            $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present
139    
140            return ($dn, $row);
141    }
142    
143    
144  # the search operation  # the search operation
145  sub search {  sub search {
146          my $self = shift;          my $self = shift;
# Line 107  sub search { Line 155  sub search {
155          if ( $reqData->{'filter'} ) {          if ( $reqData->{'filter'} ) {
156    
157                  my $sql_where = '';                  my $sql_where = '';
158                  my @values;                  @values = ();
159    
160                  foreach my $join_with ( keys %{ $reqData->{'filter'} } ) {                  foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
161    
162                          warn "## join_with $join_with\n";                          warn "## filter $filter ", dump( $reqData->{'filter'}->{ $filter } ), "\n";
163    
164                          my @limits;                          @limits = ();
165    
166                          foreach my $filter ( @{ $reqData->{'filter'}->{ $join_with } } ) {                          if ( ref $reqData->{'filter'}->{ $filter } eq 'ARRAY' ) {
167                                  warn "### filter ",dump($filter),$/;  
168                                  foreach my $how ( keys %$filter ) {                                  foreach my $filter ( @{ $reqData->{'filter'}->{ $filter } } ) {
169                                          warn "### how $how\n";                                          warn "### filter ",dump($filter),$/;
170                                          if ( $how eq 'equalityMatch' && defined $filter->{$how} ) {                                          foreach my $how ( keys %$filter ) {
171                                                  my $name = $filter->{$how}->{attributeDesc} || warn "ERROR: no attributeDesc?";                                                  if ( $how eq 'or' ) {
172                                                  my $value = $filter->{$how}->{assertionValue} || warn "ERROR: no assertionValue?";                                                          __ldap_search_to_sql( %$_ ) foreach ( @{ $filter->{$how} } );
173                                                  if ( ! $ldap_ignore->{ $name } ) {                                                  } else {
174                                                                  push @limits, __sql_column($name) . ' = ?';                                                          __ldap_search_to_sql( $how, $filter->{$how} );
                                                                 push @values, $value;  
                                                 }  
                                         } elsif ( $how eq 'substrings' ) {  
                                                 foreach my $substring ( @{ $filter->{$how}->{substrings} } ) {  
                                                         my $name = $filter->{$how}->{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;  
                                                         }  
175                                                  }                                                  }
176                                          } elsif ( $how eq 'present' ) {                                                  warn "## limits ",dump(@limits), " values ",dump(@values);
                                                 my $name = __sql_column( $filter->{$how} );  
                                                 push @limits, "$name IS NOT NULL and length($name) > 1";  
                                                 ## XXX length(foo) > 1 to avoid empty " " strings  
                                         } else {  
                                                 warn "UNSUPPORTED: how $how ",dump( $filter );  
177                                          }                                          }
                                         warn "## limits ",dump(@limits), " values ",dump(@values);  
178                                  }                                  }
                         }  
179    
180                          $sql_where .= ' ' . join( " $join_with ", @limits );                                  $sql_where .= ' ' . join( " $filter ", @limits );
181    
182                            } else {
183                                    __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
184                            }
185    
186                  }                  }
187    
# Line 158  sub search { Line 189  sub search {
189                          $sql_where = " where $sql_where";                          $sql_where = " where $sql_where";
190                  }                  }
191    
192                  warn "# SQL:\n$sql_select $sql_where\n# DATA: ",dump( @values );                  my $sql_select = read_file( lc "sql/$objectclass.sql" );
193    
194                    warn "# SQL:\n$sql_select\n", $sql_where ? $sql_where : '-- no where', "\n# DATA: ",dump( @values );
195                  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?
196                  $sth->execute( @values );                  $sth->execute( @values );
197    
# Line 166  sub search { Line 199  sub search {
199    
200                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
201    
202                          warn "## row = ",dump( $row );                          my ( $dn, $attributes ) = _dn_attributes( $row, $base );
   
                         my $dn = 'uid=' . $row->{uid} || die "no uid";  
                         $dn =~ s{[@\.]}{,dc=}g;  
203    
204                          my $entry = Net::LDAP::Entry->new;                          my $entry = Net::LDAP::Entry->new;
205                          $entry->dn( $dn . $base );                          $entry->dn( $dn );
206                          $entry->add( %$row );                          $entry->add( %$attributes );
207    
208                            #$entry->changetype( 'modify' );
209    
210                          #warn "### entry ",dump( $entry );                          warn "### entry ",$entry->dump( \*STDERR );
211    
212                          push @entries, $entry;                          push @entries, $entry;
213                  }                  }

Legend:
Removed from v.38  
changed lines
  Added in v.47

  ViewVC Help
Powered by ViewVC 1.1.26