104 |
|
|
105 |
=head2 as_collection_of |
=head2 as_collection_of |
106 |
|
|
107 |
my $connection = $ldap->collection('Organization', $limit); |
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 |
=cut |
116 |
|
|
117 |
my $collection2filter = { |
my $collection2filter = { |
118 |
'User' => '(objectClass=hrEduPerson)', |
'Person' => '(objectClass=hrEduPerson)', |
119 |
'Organization' => '(objectClass=hrEduOrg)', |
'Organization' => '(objectClass=hrEduOrg)', |
120 |
}; |
}; |
121 |
|
|
122 |
sub collection { |
sub collection { |
123 |
my ( $self, $model, $limit ) = @_; |
my $self = shift; |
124 |
|
my $model = shift or die "no model?"; |
125 |
|
my $args = {@_}; |
126 |
|
|
127 |
$limit ||= 100; # FIXME |
$args->{limit} ||= 0; # unlimited by default |
128 |
|
|
129 |
my $filter = $collection2filter->{$model}; |
my $filter = $collection2filter->{$model}; |
130 |
die "unknown model $model" unless $filter; |
# 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( |
$self->search( |
138 |
base => $self->base, |
base => $self->base, |
139 |
filter => $filter, |
filter => $filter, |
140 |
sizelimit => $limit, |
sizelimit => $args->{limit}, |
141 |
); |
); |
142 |
|
|
143 |
Jifty->log->info("searching LDAP for $model with $filter limit $limit returned ", $self->count, " results"); |
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"; |
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"; |
my $collection = $class->new() or die "can't $class->new"; |
153 |
my $model_obj = Jifty->app_class('Model',$model)->new; |
my $model_obj = Jifty->app_class('Model',$model)->new; |
154 |
#warn dump( $model_obj ); |
#warn dump( $model_obj ); |
155 |
my $additional; |
my $additional; |
156 |
# if ( $model eq 'User' ) { |
$self->model_from_entry( $model_obj, $entry, %$additional ); |
|
# my $organization = A3C::Model::Organization->new; |
|
|
# $self->ldap2model( $organization, $entry ); |
|
|
# $additional->{organization} = $organization; |
|
|
# } |
|
|
$self->ldap2model( $model_obj, $entry, %$additional ); |
|
157 |
$collection->add_record( $model_obj ); |
$collection->add_record( $model_obj ); |
158 |
} |
} |
159 |
|
|
168 |
|
|
169 |
Result of last C<< $ldap->search >> request |
Result of last C<< $ldap->search >> request |
170 |
|
|
171 |
=head2 model_to_entry |
=head2 model_from_entry |
172 |
|
|
173 |
$ldap->model_to_entry( $model, $entry, $additional ); |
$ldap->model_from_entry( $model, $entry, $additional ); |
174 |
|
|
175 |
=cut |
=cut |
176 |
|
|
177 |
sub ldap2model { |
sub model_from_entry { |
178 |
my ( $self, $model, $entry, $additional ) = @_; |
my ( $self, $model, $entry, $additional ) = @_; |
179 |
my $data; |
my $data; |
180 |
|
|
184 |
foreach my $attr ( $entry->attributes ) { |
foreach my $attr ( $entry->attributes ) { |
185 |
if ( grep(/^\Q$attr\E$/, @columns ) ) { |
if ( grep(/^\Q$attr\E$/, @columns ) ) { |
186 |
$data->{$attr} = $entry->get_value( $attr ); |
$data->{$attr} = $entry->get_value( $attr ); |
187 |
} elsif ( $attr !~ m/^(objectClass)$/i ) { |
# } elsif ( $attr !~ m/^(objectClass)$/i ) { |
188 |
Jifty->log->error(ref($model)," doesn't have $attr"); |
# Jifty->log->warn(ref($model)," doesn't have $attr"); |
189 |
} |
} |
190 |
} |
} |
191 |
|
|
194 |
my ( $id, $message ) = $model->load_or_create( %$data, %$additional ); |
my ( $id, $message ) = $model->load_or_create( %$data, %$additional ); |
195 |
|
|
196 |
if ( $id ) { |
if ( $id ) { |
197 |
Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id ); |
Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id, ' ', $model->name ); |
198 |
} else { |
} else { |
199 |
Jifty->log->error( ref($model), " ", $message ); |
Jifty->log->error( ref($model), " ", $message ); |
200 |
} |
} |