blib/lib/User/Config/DB/Ldap.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 145 CC 11 SCAR 29.8
CC
11
Cov
0%
set
56.0
_get_bind_search
12.0
_ldap
6.0
CRAP 19.7
1 package User::Config::DB::Ldap;
2  
3 use strict;
4 use warnings;
5  
6 use Moose;
7 use Moose::Util::TypeConstraints;
8  
9 with 'User::Config::DB';
10 use Net::LDAP;
11 use Net::LDAP::Entry;
12 use User::Config;
13  
14 our $VERSION = '0.01_01';
15 $VERSION = eval $VERSION; # see L<perlmodstyle>
16  
17 =pod
18  
19 =head1 NAME
20  
21 User::Config::DB::Ldap - Store User-Configuration in an LDAP-directory.
22  
23 =head1 SYNOPSIS
24  
25 use User::Config;
26  
27 my $uc = User::Config->instance;
28 $uc->db("LDAP", { table => "user", db => "dbi:SQLite:user.sqlite" });
29  
30 =head1 DESCRIPTION
31  
32 This is a database-backend for L<User::Config>. It will store all values within
33 an LDAP-directory. The options dataclass will be used to determine the
34 LDAP-schema to use. If needed, this will be added on demand.
35  
36 =head2 ATTRIBUTES
37  
38 =head3 scope
39  
40 The scope to use during searches. It might be one of "one", "base" or
41 "sub" - which is the default.
42  
43 =cut
44  
45 enum ScopeType => [ qw/ sub one base /];
46 has scope => (
47 is => "rw",
48 isa => "ScopeType",
49 default => "sub",
50 );
51  
52 =head3 userattr
53  
54 The attribute of the ldap-entry, which is used to determine the corresponding user.
55 This defaults to uid.
56  
57 =cut
58  
59 has userattr => (
60 is => "rw",
61 default => "uid",
62 );
63  
64 =head3 ns2attribute
65  
66 to generate the name of an attribute out of the namespace and name of a given
67 option, some kind of transition is needed.
68  
69 By default the '::' seperating the namespace will be replaced by an underscore;
70 as well as an underscore will be used to concatenate the namespace and the
71 options name.
72  
73 If another behaviour is needed, a corresponding sub-ref can be used here. It
74 will be called as C<<$db->ns2attribute($namespace, $name, $context)>> and
75 should return the needed attribute's name.
76  
77 =cut
78  
79 sub _ns2attribute {
80 my ($self, $namespace, $name, $ctx) = @_;
81  
82 $namespace =~ s/::/_/g;
83 return $namespace."_".$name;
84 }
85  
86 has ns2attribute => (
87 is => "rw",
88 default => sub { return \&_ns2attribute },
89 );
90  
91 =head3 searchstr
92  
93 If a search for an element has to be performed, a corresponding query is
94 generated.
95  
96 By default, a search for an element, where the attribute set in C<userattr> is
97 equal to the current's context user will be performed.
98  
99 To generate another searchstr, C<searchstr> can be set to an sub-ref. This will
100 be called like C<<$db->searchstr($namespace, $option_name, $user, $context)>>.
101  
102 =cut
103  
104 sub _searchstr {
105 my ($self, $namespace, $name, $user, $ctx) = @_;
106
107 return "(".$self->userattr."=$user)";
108 }
109  
110 has searchstr => (
111 is => "ro",
112 default => sub { return \&_searchstr },
113 );
114  
115 =head3 ldap
116  
117 If there is a preared LDAP-connection is available, this can be set using the
118 ldap-attribute. It will accept a sub-ref returning a bound connection. This
119 will be called as C<<$db->ldap($user, $modification, $context)>>, where
120 $modification will indicate, wether this connection will be used to modify
121 items in the database. The connection then has to be returned. Care has to be
122 taken not to return a connection with different user-privileges.
123  
124 =cut
125  
126 sub _ldap {
127 my ($self, $user, $bind, $context) = @_;
128  
129 my $ldap = Net::LDAP->new($self->host) or die $@;
130 if($bind and $self->binddn) {
131 my $userdn = sprintf($self->binddn, $user);
132 $ldap->bind($userdn, password => $self->bindpwd, version => 3 );
133 } else {
134 $ldap->bind(version => 3);
135 }
136 return $ldap;
137 }
138  
139 has ldap => (
140 is => "ro",
141 isa => "CodeRef",
142 default => sub { \&_ldap },
143 );
144  
145 =head3 host, binddn, bindpwd and rootdn
146  
147 If no C<ldap>-attribute is given, alternativly these parameters can be given
148 to bind to a server on every request. C<host> must be set to a corresponding
149 URI and is, like C<rootdn> in this case mandatory.
150  
151 Read-only requests will be performed using an anonymous bind. Modification will
152 do a bind on the connection in advance.
153  
154 HINT: even, if C<ldap> is given, these can be set and being used in the given sub,
155 using eg C<<$self->host>>.
156  
157 =cut
158  
159 has [ qw/host binddn bindpwd rootdn /] => (
160 isa => "Str",
161 is => "ro",
162 );
163  
164 =head3 default_objectclass
165  
166 If an option should be set, whithout any entry for the given user being present, a
167 new entry will be created. The entry will use the objectclass given by
168 C<default_objectclass> which defaults to "posixAccount".
169  
170 =cut
171  
172 has default_objectclass => (
173 isa => "Str",
174 is => "ro",
175 default => "posixAccount",
176 );
177  
178 #
179 # This returns a the bound connection as well as the executed search
180 #
181  
182 sub _get_bind_search {
183 my ($self, $args) = @_;
184  
185 my $ldap = &{$self->ldap}($self, $args->{user}, $args->{bind}, $args->{context});
186  
187 print "search:\n\tdn: ".$self->rootdn."\n\tscope: ".$self->scope."\n\tattrs: ".join(', ',@{$args->{attr}})."\n\tsearch: ".
188 &{$self->searchstr}($self,
189 $args->{namespace}, $args->{name}, $args->{user}, $args->{context})."\n" if $ENV{LDAP_DEBUG};
190 my $res = $ldap->search(
191 base => $self->rootdn,
192 scope => $self->scope,
193 attrs => $args->{attr},
194 filter => &{$self->searchstr}($self,
195 $args->{namespace}, $args->{name}, $args->{user}, $args->{context}),
196 );
197 if($ENV{LDAP_DEBUG}) {
198 $_->dump for $res->all_entries();
199 }
200 return ($ldap, $res);
201 }
202  
203 =head2 METHODS
204  
205 =head3 C<<$db->set($package, $user, $option_name, $context, $value)>>
206  
207 assigns the value for the given user to the option within a package.
208 See L<User::Config::DB>
209  
210 =cut
211  
212 sub set {
213 my ($self, $namespace, $user, $name, $ctx, $value) = @_;
214  
215 my $attr = &{$self->ns2attribute}($self, $namespace, $name, $ctx);
216 my ($ldap, $res) = $self->_get_bind_search({
217 bind => 1,
218 attr => [],
219 name => $name,
220 namespace => $namespace,
221 user => $user,
222 context => $ctx,
223 });
224 my $entry = ($res->entries())[0];
225 my $dataclass =
226 User::Config->instance()->options()->{$namespace}->{$name}->{dataclass}
227 || "extensibleObject";
228 my $add = 0;
229 unless ($entry) {
230 $add = 1;
231 $entry = Net::LDAP::Entry->new(
232 $self->userattr."=".$user.",".$self->rootdn,
233 objectclass => $self->default_objectclass,
234 $self->userattr => $user);
235 }
236 my @cls = $entry->get_value("objectclass");
237 if(grep { $_ eq $dataclass } @cls) {
238 $entry->replace($attr => $value);
239 print("set:\n\tdn: ".$entry->dn."\n\tcmd: replace($add)\n\t$attr: $value\n") if $ENV{LDAP_DEBUG};
240 } else {
241 $entry->add(objectclass => $dataclass, $attr => $value);
242 print("set:\n\tdn: ".$entry->dn."\n\tcmd: add($add)\n\tch: objectclass => $dataclass\n\t$attr: $value\n")
243 if $ENV{LDAP_DEBUG};
244 }
245 my $ret = $add?$ldap->add($entry):$entry->update($ldap);
246 die $ret->error if $ret->code;
247 }
248  
249 =head3 C<<$db->isset($package, $user, $option_name, $context)>>
250  
251 Checks wether the option was set.
252 See L<User::Config::DB>
253  
254 =cut
255  
256 sub get {
257 my ( $self, $namespace, $user, $name, $ctx) = @_;
258 my $attr = &{$self->ns2attribute}($self, $namespace, $name, $ctx);
259 my ($ldap, $res) = $self->_get_bind_search({
260 attr => [ $attr ],
261 name => $name,
262 namespace => $namespace,
263 user => $user,
264 context => $ctx,
265 });
266 my @ret = map { $_->get_value($attr) } $res->entries;
267 return @ret if wantarray;
268 return $ret[0];
269 }
270  
271 =head1 SEE ALSO
272  
273 =head1 AUTHOR
274  
275 Benjamin Tietz E<lt>benjamin@micronet24.deE<gt>
276  
277 =head1 COPYRIGHT AND LICENSE
278  
279 Copyright (C) 2011 by Benjamin Tietz
280  
281 This library is free software; you can redistribute it and/or modify
282 it under the same terms as Perl itself, either Perl version 5.10.1 or,
283 at your option, any later version of Perl 5 you may have available.
284  
285 =cut
286  
287 no Moose;
288 __PACKAGE__->meta->make_immutable;
289 1;
290