-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLDAP.pm
More file actions
162 lines (113 loc) · 4.23 KB
/
Copy pathLDAP.pm
File metadata and controls
162 lines (113 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package HTML::FormFu::Model::LDAP;
use strict;
use warnings;
use base 'HTML::FormFu::Model';
use Data::Dumper;
our $VERSION = '0.0103';
use Encode;
sub default_values {
my ( $self, $ldap_entry ) = @_;
my $base = $self->form;
my $cfg = $base->model_config || {};
my $elements = $base->get_all_elements();
foreach my $e (@$elements) {
my $name = $e->name();
my $val = $ldap_entry->get_value($name);
$val = decode_utf8($val) if $cfg->{decode};
if ( $name && $val ) {
$e->default($val);
}
}
}
sub update {
my ( $self, $ldap_entry, $attrs ) = @_;
$attrs ||= {};
my $form = $self->form;
my $base = defined $attrs->{base} ? delete $attrs->{base} : $form;
$base = $form->get_all_element( { nested_name => $attrs->{nested_base} } )
if defined $attrs->{nested_base}
&& ( !defined $base->nested_name
|| $base->nested_name ne $attrs->{nested_base} );
my @valid = $form->valid;
# Get ldap_server from attrs.
my $ldap_server =
defined $attrs->{ldap_server} ? delete $attrs->{ldap_server} : undef;
# Run through all possible ldap attributes, and store those from form hash
my @objectclasses = $ldap_entry->get_value('objectClass');
foreach my $oc (@objectclasses) {
foreach my $attr ( $ldap_server->schema->must($oc),
$ldap_server->schema->may($oc) )
{
my $field = $base->get_field( { name => $attr->{name} } );
my $nested_name = defined $field ? $field->nested_name : undef;
my $value =
defined $field
? $form->param_value( $field->nested_name )
: (
grep {
defined $attrs->{nested_base}
? defined $nested_name
? $nested_name eq $_
: 0
: $attr->{name} eq $_
} @valid
)
? $form->param_value( $attr->{name} )
: undef;
#warn $attr->{name} . ": " . ($value ? $value : "undef");
if ( defined $value ) {
$ldap_entry->replace( $attr->{name}, $value );
} elsif ($nested_name) {
# This exists in the form, so we should remove it?
$ldap_entry->delete( $attr->{name}) if $ldap_entry->exists( $attr->{name} );
}
}
}
return $ldap_entry->update($ldap_server);
}
1;
=head1 NAME
HTML::FormFu::Model::LDAP - Integrate FormFu and LDAP
=head1 SYNOPSIS
Set the form's L<HTML::FormFu/default_model>.
---
default_model: LDAP
Example usage:
# $ldap_entry should inherit from Net::LDAP::Entry, or atleast
# implement get_value(), replace() and update()
# set form's default values from LDAP
$form->model->default_values($ldap_entry);
# update LDAP with values from submitted form
if ($form->submitted_and_valid) {
$form->model->update($ldap_entry);
}
=head1 DESCRIPTION
This module implements the model-interface of HTML::FormFu and provides
the glue between HTML::FormFu and an Net::LDAP based "model"
See L<HTML::FormFu::Model::DBIC> for further documentation on usage.
=head2 METHODS
=head3 default_values
Arguments: $ldap_entry
Fills out the fields in $form where it can find a matching attributes in ldap
=head3 update
Arguments: $ldap_entry, [\%config]
Iterates through the $ldap_entry, finding any may and must attributes
that has a coresponding field in the form, and updates the ldap-entry.
This also calles $ldap_entry->update($ldap_server) to commit its changes
=head1 SUPPORT
Please report any bugs or feature requests to
C<bug-html-formfu-model-ldap@rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org>.
=head2 NOT IMPLEMENTED
The following L<HTML::FormFu::Model> methods are not implemented:
=over
=item create
=item options_from_model
=back
=head1 AUTHOR
Andreas Marienborg C<< <andreas@startsiden.no> >>
Andreas Dahl C<< <andread@never.no> >>
=head1 LICENCE AND COPYRIGHT
Copyright (c) 2008, Andreas Marienborg C<< <andreas@startsiden.no> >>.
This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic>.