--- lib/A3C/LDAP.pm 2008/04/01 17:32:59 53 +++ lib/A3C/LDAP.pm 2008/05/01 17:06:02 106 @@ -6,7 +6,7 @@ use Net::LDAP; use Data::Dump qw/dump/; use base qw(Jifty::Object Class::Accessor::Fast); -our @config_fields = qw( server dn password base ); +our @config_fields = keys %{ Jifty->config->app('LDAP') }; Jifty->log->debug("using fields from configuration: ",dump( @config_fields )); __PACKAGE__->mk_accessors( qw(ldap current_search), @config_fields ); @@ -42,6 +42,17 @@ } } + # configuration sanity testing + foreach ( qw/server dn password base objectClass link/ ) { + die "missing required field $_ in LDAP from etc/config.yaml" unless $args->{$_}; + } + foreach ( qw/person organization/ ) { + die "missing required field $_ in LDAP.objectClass.$_ from etc/config.yaml" unless $args->{objectClass}->{$_}; + } + foreach ( qw/person_filter display_from value_from/ ) { + die "missing required field $_ in LDAP.link.$_ from etc/config.yaml" unless $args->{link}->{$_}; + } + my $ldap = Net::LDAP->new( $args->{server} ) or die "$@"; # an anonymous bind @@ -102,11 +113,11 @@ $self->current_search->count; } -=head2 as_collection_of +=head2 collection my $connection = $ldap->collection( # name of model to use - 'Organization', + $ldap->objectClass->{organization}, # optional params limit => $limit, filter => '(uid=foobar)', @@ -115,7 +126,7 @@ =cut my $collection2filter = { - 'User' => '(objectClass=hrEduPerson)', + 'Person' => '(objectClass=hrEduPerson)', 'Organization' => '(objectClass=hrEduOrg)', }; @@ -127,7 +138,9 @@ $args->{limit} ||= 0; # unlimited by default my $filter = $collection2filter->{$model}; - die "unknown model $model" unless $filter; +# die "unknown model $model" unless $filter; + # fallback to model named as objectClass + $filter ||= "(objectClass=$model)"; # add user filter $filter = '(&' . $filter . $args->{filter} . ')' if $args->{filter}; @@ -139,8 +152,8 @@ ); Jifty->log->info( - "searching LDAP for $model with $filter ", - $args->{limit} ? 'limit ' . $args->{limit} : '', + "Searching LDAP for $model with $filter ", + $args->{limit} ? 'limit ' . $args->{limit} . ' ' : '', 'returned ', $self->count, ' results' ); @@ -151,12 +164,7 @@ my $model_obj = Jifty->app_class('Model',$model)->new; #warn dump( $model_obj ); my $additional; -# if ( $model eq 'User' ) { -# my $organization = A3C::Model::Organization->new; -# $self->ldap2model( $organization, $entry ); -# $additional->{organization} = $organization; -# } - $self->ldap2model( $model_obj, $entry, %$additional ); + $self->model_from_entry( $model_obj, $entry, %$additional ); $collection->add_record( $model_obj ); } @@ -171,13 +179,13 @@ Result of last C<< $ldap->search >> request -=head2 model_to_entry +=head2 model_from_entry - $ldap->model_to_entry( $model, $entry, $additional ); + $ldap->model_from_entry( $model, $entry, $additional ); =cut -sub ldap2model { +sub model_from_entry { my ( $self, $model, $entry, $additional ) = @_; my $data; @@ -187,7 +195,8 @@ foreach my $attr ( $entry->attributes ) { if ( grep(/^\Q$attr\E$/, @columns ) ) { $data->{$attr} = $entry->get_value( $attr ); - } elsif ( $attr !~ m/^(objectClass)$/i ) { +# } elsif ( $attr !~ m/^(objectClass)$/i ) { + } else { Jifty->log->warn(ref($model)," doesn't have $attr"); } }