/[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

Contents of /lib/LDAP/Koha.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 45 - (show annotations)
Wed Apr 15 12:47:57 2009 UTC (14 years, 11 months ago) by dpavlin
File size: 5440 byte(s)
rename join_with -> filter

1 package LDAP::Koha;
2
3 use strict;
4 use warnings;
5 use Data::Dump qw/dump/;
6
7 use lib '../lib';
8 use Net::LDAP::Constant qw(LDAP_SUCCESS);
9 use Net::LDAP::Server;
10 use base 'Net::LDAP::Server';
11 use fields qw();
12
13 use DBI;
14
15 # XXX test with:
16 #
17 # ldapsearch -h localhost -p 2389 -b dc=ffzg,dc=hr -x 'otherPager=200903160021'
18 #
19
20 our $dsn = 'DBI:mysql:dbname=';
21 our $database = 'koha';
22 our $user = 'unconfigured-user';
23 our $passwd = 'unconfigured-password';
24
25 our $max_results = 10; # 100; # FIXME
26
27 require 'config.pl' if -e 'config.pl';
28
29 my $dbh = DBI->connect($dsn . $database, $user,$passwd, { RaiseError => 1, AutoCommit => 1 }) || die $DBI::errstr;
30
31 # Net::LDAP::Entry will lc all our attribute names anyway, so
32 # we don't really care about correctCapitalization for LDAP
33 # attributes which won't pass through DBI
34 my $sql_select = q{
35 select
36 trim(userid) as uid,
37 firstname as givenName,
38 surname as sn,
39 concat(firstname,' ',surname) as cn,
40
41 -- SAFEQ specific mappings from UMgr-LDAP.conf
42 surname as displayName,
43 rfid_sid as pager,
44 email as mail,
45 categorycode as ou,
46 categorycode as organizationalUnit,
47 categorycode as memberOf,
48 categorycode as department,
49 borrowernumber as objectGUID,
50 concat('/home/',borrowernumber) as homeDirectory
51 from borrowers
52 };
53
54 # we need reverse LDAP -> SQL mapping for where clause
55 my $ldap_sql_mapping = {
56 'uid' => 'userid',
57 'objectGUID' => 'borrowernumber',
58 'displayName' => 'surname',
59 'sn' => 'surname',
60 'pager' => 'rfid_sid',
61 };
62
63 # attributes which are same for whole set, but somehow
64 # LDAP clients are sending they anyway and we don't
65 # have them in database
66 my $ldap_ignore = {
67 'objectclass' => 1,
68 };
69
70 sub __sql_column {
71 my $name = shift;
72 $ldap_sql_mapping->{$name} || $name;
73 }
74
75 use constant RESULT_OK => {
76 'matchedDN' => '',
77 'errorMessage' => '',
78 'resultCode' => LDAP_SUCCESS
79 };
80
81 # constructor
82 sub new {
83 my ($class, $sock) = @_;
84 my $self = $class->SUPER::new($sock);
85 print "connection from: ", $sock->peerhost(), "\n";
86 return $self;
87 }
88
89 # the bind operation
90 sub bind {
91 my $self = shift;
92 my $reqData = shift;
93 warn "# bind ",dump($reqData);
94 return RESULT_OK;
95 }
96
97 our @values;
98 our @limits;
99
100 sub __ldap_search_to_sql {
101 my ( $how, $what ) = @_;
102 warn "### __ldap_search_to_sql $how ",dump( $what ),"\n";
103 if ( $how eq 'equalityMatch' && defined $what ) {
104 my $name = $what->{attributeDesc} || warn "ERROR: no attributeDesc?";
105 my $value = $what->{assertionValue} || warn "ERROR: no assertionValue?";
106 if ( ! $ldap_ignore->{ $name } ) {
107 push @limits, __sql_column($name) . ' = ?';
108 push @values, $value;
109 } else {
110 warn "IGNORED: $name = $value";
111 }
112 } elsif ( $how eq 'substrings' ) {
113 foreach my $substring ( @{ $what->{substrings} } ) {
114 my $name = $what->{type} || warn "ERROR: no type?";
115 while ( my($op,$value) = each %$substring ) {
116 push @limits, __sql_column($name) . ' LIKE ?';
117 if ( $op eq 'any' ) {
118 $value = '%' . $value . '%';
119 } else {
120 warn "UNSUPPORTED: op $op - using plain $value";
121 }
122 push @values, $value;
123 }
124 }
125 } elsif ( $how eq 'present' ) {
126 my $name = __sql_column( $what );
127 push @limits, "$name IS NOT NULL and length($name) > 1";
128 ## XXX length(foo) > 1 to avoid empty " " strings
129 } else {
130 warn "UNSUPPORTED: $how ",dump( $what );
131 }
132 }
133
134 # the search operation
135 sub search {
136 my $self = shift;
137 my $reqData = shift;
138 print "searching...\n";
139
140 warn "# " . localtime() . " request = ", dump($reqData);
141
142 my $base = $reqData->{'baseObject'}; # FIXME use it?
143
144 my @entries;
145 if ( $reqData->{'filter'} ) {
146
147 my $sql_where = '';
148 @values = ();
149
150 foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
151
152 warn "## filter $filter ", dump( $reqData->{'filter'}->{ $filter } ), "\n";
153
154 @limits = ();
155
156 if ( ref $reqData->{'filter'}->{ $filter } eq 'ARRAY' ) {
157
158 foreach my $filter ( @{ $reqData->{'filter'}->{ $filter } } ) {
159 warn "### filter ",dump($filter),$/;
160 foreach my $how ( keys %$filter ) {
161 if ( $how eq 'or' ) {
162 __ldap_search_to_sql( %$_ ) foreach ( @{ $filter->{$how} } );
163 } else {
164 __ldap_search_to_sql( $how, $filter->{$how} );
165 }
166 warn "## limits ",dump(@limits), " values ",dump(@values);
167 }
168 }
169
170 $sql_where .= ' ' . join( " $filter ", @limits );
171
172 } else {
173 __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
174 }
175
176 }
177
178 if ( $sql_where ) {
179 $sql_where = " where $sql_where";
180 }
181
182 warn "# SQL:\n$sql_select\n$sql_where\n# DATA: ",dump( @values );
183 my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?
184 $sth->execute( @values );
185
186 warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
187
188 while (my $row = $sth->fetchrow_hashref) {
189
190 warn "## row = ",dump( $row );
191
192 my $dn = 'uid=' . $row->{uid} || die "no uid";
193 $dn =~ s{[@\.]}{,dc=}g;
194 $dn .= ',' . $base unless $dn =~ m{dc}i;
195
196 my $entry = Net::LDAP::Entry->new;
197 $entry->dn( $dn );
198 $entry->add( objectClass => [
199 "person",
200 "organizationalPerson",
201 "inetOrgPerson",
202 "hrEduPerson",
203 ] );
204 $entry->add( %$row );
205
206 #$entry->changetype( 'modify' );
207
208 warn "### entry ",$entry->dump( \*STDERR );
209
210 push @entries, $entry;
211 }
212
213 } else {
214 warn "UNKNOWN request: ",dump( $reqData );
215 }
216
217 return RESULT_OK, @entries;
218 }
219
220 # the rest of the operations will return an "unwilling to perform"
221
222 1;

  ViewVC Help
Powered by ViewVC 1.1.26