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 |
|
|
132 |
|
# add user filter |
133 |
|
$filter = '(&' . $filter . $args->{filter} . ')' if $args->{filter}; |
134 |
|
|
135 |
$self->search( |
$self->search( |
136 |
base => $self->base, |
base => $self->base, |
137 |
filter => $filter, |
filter => $filter, |
138 |
sizelimit => $limit, |
sizelimit => $args->{limit}, |
139 |
); |
); |
140 |
|
|
141 |
Jifty->log->info("searching LDAP for $model with $filter limit $limit returned ", $self->count, " results"); |
Jifty->log->info( |
142 |
|
"Searching LDAP for $model with $filter ", |
143 |
|
$args->{limit} ? 'limit ' . $args->{limit} . ' ' : '', |
144 |
|
'returned ', $self->count, ' results' |
145 |
|
); |
146 |
|
|
147 |
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"; |
148 |
my $collection = $class->new() or die "can't $class->new"; |
my $collection = $class->new() or die "can't $class->new"; |
151 |
my $model_obj = Jifty->app_class('Model',$model)->new; |
my $model_obj = Jifty->app_class('Model',$model)->new; |
152 |
#warn dump( $model_obj ); |
#warn dump( $model_obj ); |
153 |
my $additional; |
my $additional; |
|
# if ( $model eq 'User' ) { |
|
|
# my $organization = A3C::Model::Organization->new; |
|
|
# $self->ldap2model( $organization, $entry ); |
|
|
# $additional->{organization} = $organization; |
|
|
# } |
|
154 |
$self->ldap2model( $model_obj, $entry, %$additional ); |
$self->ldap2model( $model_obj, $entry, %$additional ); |
155 |
$collection->add_record( $model_obj ); |
$collection->add_record( $model_obj ); |
156 |
} |
} |
183 |
if ( grep(/^\Q$attr\E$/, @columns ) ) { |
if ( grep(/^\Q$attr\E$/, @columns ) ) { |
184 |
$data->{$attr} = $entry->get_value( $attr ); |
$data->{$attr} = $entry->get_value( $attr ); |
185 |
} elsif ( $attr !~ m/^(objectClass)$/i ) { |
} elsif ( $attr !~ m/^(objectClass)$/i ) { |
186 |
Jifty->log->error(ref($model)," doesn't have $attr"); |
Jifty->log->warn(ref($model)," doesn't have $attr"); |
187 |
} |
} |
188 |
} |
} |
189 |
|
|
192 |
my ( $id, $message ) = $model->load_or_create( %$data, %$additional ); |
my ( $id, $message ) = $model->load_or_create( %$data, %$additional ); |
193 |
|
|
194 |
if ( $id ) { |
if ( $id ) { |
195 |
Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id ); |
Jifty->log->info( $message || 'Added', ' ', ref($model), ' ', $model->id, ' ', $model->name ); |
196 |
} else { |
} else { |
197 |
Jifty->log->error( ref($model), " ", $message ); |
Jifty->log->error( ref($model), " ", $message ); |
198 |
} |
} |