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

Diff of /lib/A3C/LDAP.pm

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

revision 41 by dpavlin, Sun Mar 30 15:23:35 2008 UTC revision 101 by dpavlin, Thu May 1 12:59:02 2008 UTC
# Line 6  use warnings; Line 6  use warnings;
6  use Net::LDAP;  use Net::LDAP;
7  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
8  use base qw(Jifty::Object Class::Accessor::Fast);  use base qw(Jifty::Object Class::Accessor::Fast);
9  __PACKAGE__->mk_accessors( qw(ldap server dn password current_search) );  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  =head1 NAME
# Line 34  sub new { Line 36  sub new {
36          my $ldap_config = Jifty->config->app('LDAP');          my $ldap_config = Jifty->config->app('LDAP');
37          Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );          Jifty->log->debug( "config->app(LDAP) = ",dump( $ldap_config ) );
38    
39          $args->{server}         ||= $ldap_config->{Server};          foreach my $f ( @config_fields ) {
40          $args->{dn}                     ||= $ldap_config->{DN};                  if ( my $v = $ldap_config->{$f} ) {
41          $args->{password}       ||= $ldap_config->{Password};                          $args->{$f} = $v;
42                    }
43            }
44    
45          my $ldap = Net::LDAP->new( $args->{server} ) or die "$@";          my $ldap = Net::LDAP->new( $args->{server} ) or die "$@";
46    
# Line 54  sub new { Line 58  sub new {
58  =head2 search  =head2 search
59    
60    my $msg = A3C::LDAP->search(    my $msg = A3C::LDAP->search(
61                          base    => 'dc=skole,dc=hr',          base    => 'dc=skole,dc=hr',
62                          filter  => '(objectClass=hrEduOrg)',          filter  => '(objectClass=hrEduOrg)',
63                          sizelimit => 10,          sizelimit => 10,
64    );    );
65    
66  =cut  =cut
# Line 66  sub search { Line 70  sub search {
70    
71          my $search = $self->ldap->search( @_ );          my $search = $self->ldap->search( @_ );
72          if ( $search->code != 0 ) {          if ( $search->code != 0 ) {
73                  Jifty->log->error( $search->error );                  Jifty->log->error( $search->error, ' for ', dump( @_ ) );
74          }          }
75          return $self->current_search( $search );          return $self->current_search( $search );
76  }  }
# Line 98  sub count { Line 102  sub count {
102          $self->current_search->count;          $self->current_search->count;
103  }  }
104    
105    =head2 as_collection_of
106    
107      my $connection = $ldap->collection(
108            # name of model to use
109            'Organization',
110            # optional params
111            limit => $limit,
112            filter => '(uid=foobar)',
113      );
114    
115    =cut
116    
117    my $collection2filter = {
118            'Person'                => '(objectClass=hrEduPerson)',
119            'Organization'  => '(objectClass=hrEduOrg)',
120    };
121    
122    sub collection {
123            my $self = shift;
124            my $model = shift or die "no model?";
125            my $args = {@_};
126    
127            $args->{limit} ||= 0;   # unlimited by default
128    
129            my $filter = $collection2filter->{$model};
130    #       die "unknown model $model" unless $filter;
131            # fallback to model named as objectClass
132            $filter ||= "(objectClass=$model)";
133    
134            # add user filter
135            $filter = '(&' . $filter . $args->{filter} . ')' if $args->{filter};
136    
137            $self->search(
138                    base => $self->base,
139                    filter => $filter,
140                    sizelimit => $args->{limit},
141            );
142    
143            Jifty->log->info(
144                    "Searching LDAP for $model with $filter ",
145                    $args->{limit} ? 'limit ' . $args->{limit} . ' ' : '',
146                    'returned ', $self->count, ' results'
147            );
148    
149            my $class = Jifty->app_class('Model', $model . 'Collection' ) or die "can't create ${model}Collection";
150            my $collection = $class->new() or die "can't $class->new";
151    
152            while ( my $entry = $self->next ) {
153                    my $model_obj = Jifty->app_class('Model',$model)->new;
154                    #warn dump( $model_obj );
155                    my $additional;
156                    $self->ldap2model( $model_obj, $entry, %$additional );
157                    $collection->add_record( $model_obj );
158            }
159    
160            return $collection;
161    }
162    
163  =head1 INTERNAL METHODS  =head1 INTERNAL METHODS
164    
165  Following methods map directly into L<Net::LDAP>  Following methods map directly into L<Net::LDAP>
# Line 106  Following methods map directly into L<Ne Line 168  Following methods map directly into L<Ne
168    
169  Result of last C<< $ldap->search >> request  Result of last C<< $ldap->search >> request
170    
171    =head2 model_to_entry
172    
173      $ldap->model_to_entry( $model, $entry, $additional );
174    
175  =cut  =cut
176    
177    sub ldap2model {
178            my ( $self, $model, $entry, $additional ) = @_;
179            my $data;
180    
181            my @columns = map { $_->name } $model->columns;
182            #warn "# columns = ",dump( @columns );
183    
184            foreach my $attr ( $entry->attributes ) {
185                    if ( grep(/^\Q$attr\E$/, @columns ) ) {
186                            $data->{$attr} = $entry->get_value( $attr );
187                    } elsif ( $attr !~ m/^(objectClass)$/i ) {
188                            Jifty->log->warn(ref($model)," doesn't have $attr");
189                    }
190            }
191    
192            Jifty->log->debug( ref($model), ' = ', dump( $data ) );
193    
194            my ( $id, $message ) = $model->load_or_create( %$data, %$additional );
195    
196            if ( $id ) {
197                    Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id, ' ', $model->name );
198            } else {
199                    Jifty->log->error( ref($model), " ", $message );
200            }
201    }
202    
203    
204    
205  1;  1;

Legend:
Removed from v.41  
changed lines
  Added in v.101

  ViewVC Help
Powered by ViewVC 1.1.26