/[A3C]/lib/A3C/LDAP.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/A3C/LDAP.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (show annotations)
Sun Mar 30 16:58:21 2008 UTC (14 years, 10 months ago) by dpavlin
File size: 3948 byte(s)
- move all LDAP-related logic in A3C::LDAP
- remove logic to link User and Organization
- ldap search base is now configurable in config.yml
1 package A3C::LDAP;
2
3 use strict;
4 use warnings;
5
6 use Net::LDAP;
7 use Data::Dump qw/dump/;
8 use base qw(Jifty::Object Class::Accessor::Fast);
9 our @config_fields = qw( server dn password base );
10 Jifty->log->debug("using fields from configuration: ",dump( @config_fields ));
11 __PACKAGE__->mk_accessors( qw(ldap current_search), @config_fields );
12
13
14 =head1 NAME
15
16 A3C::LDAP
17
18 =head1 DESCRIPTION
19
20 This object turn L<Net::LDAP> into something with looks like
21 L<Jifty::Collection>
22
23 =head1 METHODS
24
25 =head2 new
26
27 my $ldap = A3C::LDAP->new;
28
29 =cut
30
31 sub new {
32 my $class = shift;
33
34 my $args = { @_ };
35
36 my $ldap_config = Jifty->config->app('LDAP');
37 Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );
38
39 foreach my $f ( @config_fields ) {
40 if ( my $v = $ldap_config->{$f} ) {
41 $args->{$f} = $v;
42 }
43 }
44
45 my $ldap = Net::LDAP->new( $args->{server} ) or die "$@";
46
47 # an anonymous bind
48 #$ldap->bind;
49 $ldap->bind( $args->{dn}, password => $args->{password} );
50
51 Jifty->log->info("Connected to ", $args->{server}, " with DN ", $args->{dn});
52
53 $args->{ldap} = $ldap;
54
55 $class->SUPER::new( $args );
56 }
57
58 =head2 search
59
60 my $msg = A3C::LDAP->search(
61 base => 'dc=skole,dc=hr',
62 filter => '(objectClass=hrEduOrg)',
63 sizelimit => 10,
64 );
65
66 =cut
67
68 sub search {
69 my $self = shift;
70
71 my $search = $self->ldap->search( @_ );
72 if ( $search->code != 0 ) {
73 Jifty->log->error( $search->error, ' for ', dump( @_ ) );
74 }
75 return $self->current_search( $search );
76 }
77
78 =head2 next
79
80 Syntaxtic shugar to look more like L<Jifty::DBI::Collection>
81
82 my $entry = ldap->next;
83
84 =cut
85
86 sub next {
87 my $self = shift;
88
89 die "no current LDAP search" unless $self->current_search;
90
91 return $self->current_search->shift_entry;
92 }
93
94 =head2 count
95
96 my $search_results = $ldap->count;
97
98 =cut
99
100 sub count {
101 my $self = shift;
102 $self->current_search->count;
103 }
104
105 =head2 as_collection_of
106
107 my $connection = $ldap->collection('Organization', $limit);
108
109 =cut
110
111 my $collection2filter = {
112 'User' => '(objectClass=hrEduPerson)',
113 'Organization' => '(objectClass=hrEduOrg)',
114 };
115
116 sub collection {
117 my ( $self, $model, $limit ) = @_;
118
119 $limit ||= 100; # FIXME
120
121 my $filter = $collection2filter->{$model};
122 die "unknown model $model" unless $filter;
123
124 $self->search(
125 base => $self->base,
126 filter => $filter,
127 sizelimit => $limit,
128 );
129
130 Jifty->log->info("searching LDAP for $model with $filter limit $limit returned ", $self->count, " results");
131
132 my $class = Jifty->app_class('Model', $model . 'Collection' ) or die "can't create ${model}Collection";
133 my $collection = $class->new() or die "can't $class->new";
134
135 while ( my $entry = $self->next ) {
136 my $model_obj = Jifty->app_class('Model',$model)->new;
137 #warn dump( $model_obj );
138 my $additional;
139 # if ( $model eq 'User' ) {
140 # my $organization = A3C::Model::Organization->new;
141 # $self->ldap2model( $organization, $entry );
142 # $additional->{organization} = $organization;
143 # }
144 $self->ldap2model( $model_obj, $entry, %$additional );
145 $collection->add_record( $model_obj );
146 }
147
148 return $collection;
149 }
150
151 =head1 INTERNAL METHODS
152
153 Following methods map directly into L<Net::LDAP>
154
155 =head2 current_search
156
157 Result of last C<< $ldap->search >> request
158
159 =head2 model_to_entry
160
161 $ldap->model_to_entry( $model, $entry, $additional );
162
163 =cut
164
165 sub ldap2model {
166 my ( $self, $model, $entry, $additional ) = @_;
167 my $data;
168
169 my @columns = map { $_->name } $model->columns;
170 #warn "# columns = ",dump( @columns );
171
172 foreach my $attr ( $entry->attributes ) {
173 if ( grep(/^\Q$attr\E$/, @columns ) ) {
174 $data->{$attr} = $entry->get_value( $attr );
175 } elsif ( $attr !~ m/^(objectClass)$/i ) {
176 Jifty->log->error(ref($model)," doesn't have $attr");
177 }
178 }
179
180 Jifty->log->debug( ref($model), ' = ', dump( $data ) );
181
182 my ( $id, $message ) = $model->load_or_create( %$data, %$additional );
183
184 if ( $id ) {
185 Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id );
186 } else {
187 Jifty->log->error( ref($model), " ", $message );
188 }
189 }
190
191
192
193 1;

  ViewVC Help
Powered by ViewVC 1.1.26