TABOO/XSP/User.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 46 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 200 CC 4 SCAR 10.2
CC
4
Cov
0%
authlevel_extremes
12.0
_exists_check
6.0
makeSalt
2.0
CRAP 2.8
1 package AxKit::App::TABOO::XSP::User;
2 use 5.6.0;
3 use strict;
4 use warnings;
5 use Apache::AxKit::Language::XSP::SimpleTaglib;
6 use Apache::AxKit::Exception;
7 use AxKit;
8 use AxKit::App::TABOO;
9 use AxKit::App::TABOO::Data::User;
10 use AxKit::App::TABOO::Data::User::Contributor;
11 use Session;
12 use Apache::Cookie;
13 use Crypt::GeneratePassword;
14 use Data::Dumper;
15  
16 use vars qw/$NS/;
17  
18  
19 our $VERSION = '0.4';
20  
21 # Some constants
22 # TODO: This stuff should go somewhere else!
23  
24 use constant GUEST => 0;
25 use constant NEWMEMBER => 1;
26 use constant MEMBER => 2;
27 use constant OLDTIMER => 3;
28 use constant ASSISTANT => 4;
29 use constant EDITOR => 5;
30 use constant ADMIN => 6;
31 use constant DIRECTOR => 7;
32 use constant GURU => 8;
33 use constant GOD => 9;
34  
35  
36 =head1 NAME
37  
38 AxKit::App::TABOO::XSP::User - User information management and authorization tag library for TABOO
39  
40 =head1 SYNOPSIS
41  
42 Add the user: namespace to your XSP C<E<lt>xsp:pageE<gt>> tag, e.g.:
43  
44 <xsp:page
45 language="Perl"
46 xmlns:xsp="http://apache.org/xsp/core/v1"
47 xmlns:user="http://www.kjetil.kjernsmo.net/software/TABOO/NS/User"
48 >
49  
50 Add this taglib to AxKit (via httpd.conf or .htaccess):
51  
52 AxAddXSPTaglib AxKit::App::TABOO::XSP::User
53  
54  
55 =head1 DESCRIPTION
56  
57 This XSP taglib provides a few tags to retrieve, set, modify and save
58 user information, as it communicates with TABOO Data objects,
59 particulary L<AxKit::App::TABOO::Data::User> and
60 <AxKit::App::TABOO::Data::User::Contributor>.
61  
62 L<Apache::AxKit::Language::XSP::SimpleTaglib> has been used to write
63 this taglib.
64  
65 =cut
66  
67 $NS = 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/User';
68  
69 # Shamelessly lifted from Joergs module
70 sub makeSalt {
71 my $result = '$1$';
72 my @chars = ('.', '/', 0..9, 'A'..'Z', 'a'..'z');
73 for (0..7) {
74 $result .= $chars[int(rand(64))];
75 }
76 $result .= '$';
77 return $result;
78 }
79  
80  
81 # This little sub takes the user name of the user we want to change
82 # the authlevel of, and returns a hash with the smallest and highest
83 # level we are allowed to give that user.
84 sub authlevel_extremes {
85 my $username = shift;
86 my $r = Apache->request;
87 my $session = AxKit::App::TABOO::session($r);
88 my $authlevel = AxKit::App::TABOO::authlevel($session);
89 my $maxlevel = ($username eq AxKit::App::TABOO::loggedin($session)) ? $authlevel : ($authlevel - 2);
90 my $user = AxKit::App::TABOO::Data::User::Contributor->new();
91 my $oldlevel = $user->load_authlevel($username);
92 my $minlevel = ($authlevel < AxKit::App::TABOO::XSP::User::ADMIN) ? $oldlevel : 0;
93 return {'minlevel' => $minlevel, 'maxlevel' => $maxlevel};
94 }
95  
96  
97 sub _sanatize_username {
98 my $tmp = lc shift;
99 $tmp =~ tr/a-z/_/cs;
100 return $tmp;
101 }
102  
103 sub _exists_check {
104 my $username = shift;
105 my $user = AxKit::App::TABOO::Data::User->new();
106 if (($username =~ m/comment|thread|all|respond|edit/) || ($user->load_name($username)) || ($username ne _sanatize_username($username))) {
107 return 1;
108 } else {
109 return 0;
110 }
111 }
112  
113  
114 package AxKit::App::TABOO::XSP::User::Handlers;
115  
116  
117 =head1 Tag Reference
118  
119 =head2 C<E<lt>store/E<gt>>
120  
121 It will take whatever data it finds in the L<Apache::Request> object
122 held by AxKit, and hand it to a new L<AxKit::App::TABOO::Data::User>
123 object, which will use whatever data it finds useful. It may also take
124 C<newpasswd1> and C<newpasswd2> fields, and if they are encountered,
125 they will be checked if they are equal and then the password will be
126 encrypted before it is sent to the Data object. The Data object is
127 then instructed to save itself.
128  
129 =cut
130  
131 sub store {
132 return << 'EOC';
133 my %args = map { $_ => join('', $cgi->param($_)) } $cgi->param;
134 my $session = AxKit::App::TABOO::session($r);
135 my $editinguser = AxKit::App::TABOO::loggedin($session);
136 my $authlevel = AxKit::App::TABOO::authlevel($session);
137 AxKit::Debug(9, $editinguser . " logged in at level " . $authlevel);
138 unless (defined($authlevel)) {
139 throw Apache::AxKit::Exception::Retval(
140 return_code => AUTH_REQUIRED,
141 -text => "Not authenticated and authorized with an authlevel");
142 }
143 my $user = AxKit::App::TABOO::Data::User::Contributor->new();
144 # Retrieve old data
145 $user->load(what => '*', limit => {'username' => $args{'username'}});
146  
147 if ($args{'username'} eq 'guest') {
148 throw Apache::AxKit::Exception::Retval(
149 return_code => FORBIDDEN,
150 -text => "Leave the guest user alone!");
151 }
152 if ($args{'username'} eq $editinguser) {
153 # It is the user editing his own data
154 if ($args{'authlevel'} > $authlevel) {
155 throw Apache::AxKit::Exception::Retval(
156 return_code => FORBIDDEN,
157 -text => "Can you say privilege escalation, huh?");
158 }
159 if (($args{'newpasswd1'}) && ($args{'newpasswd2'})) {
160 # So, we want to update password
161 if ($args{'newpasswd1'} eq $args{'newpasswd2'}) {
162 $args{'passwd'} = crypt($args{'newpasswd1'}, AxKit::App::TABOO::XSP::User::makeSalt());
163 delete $args{'newpasswd1'};
164 delete $args{'newpasswd2'};
165 } else {
166 throw Apache::AxKit::Exception::Error(-text => "Passwords don't match");
167 }
168 }
169 } else {
170 # It is a higher privileged user editing another user's data.
171 my @changing = $user->apache_request_changed(\%args); # These are the fields sought to be changed
172 AxKit::Debug(10, "Changing fields: " . join(", ", @changing));
173 if ((scalar @changing == 1) && grep(/authlevel/, @changing)) {
174 # Then, it is only the authlevel that is to be changed, and OK levels are given by authlevel_extremes.
175 my $extremes = AxKit::App::TABOO::XSP::User::authlevel_extremes($args{'username'});
176 if ((${$extremes}{'minlevel'} > ${$extremes}{'maxlevel'})
177 || ($args{'authlevel'} > ${$extremes}{'maxlevel'})
178 || ($args{'authlevel'} < ${$extremes}{'minlevel'}))
179 {
180 throw Apache::AxKit::Exception::Retval(
181 return_code => FORBIDDEN,
182 -text => "You may only set an authlevel between " . ${$extremes}{'minlevel'} . " and " . ${$extremes}{'maxlevel'} . ". Your level: " . $authlevel);
183 }
184 } else {
185 # Any other fields require ADMIN privs
186 if ($authlevel < AxKit::App::TABOO::XSP::User::ADMIN) {
187 throw Apache::AxKit::Exception::Retval(
188 return_code => FORBIDDEN,
189 -text => "Admin Privileges are needed to edit other user's data. Your level: " . $authlevel);
190 }
191 }
192 }
193 $user->populate(\%args);
194 $user->save();
195 EOC
196 }
197  
198  
199 =head2 C<E<lt>new-user/E<gt>>
200  
201 This tag will store the contents of an Apache::Request object in the
202 data store, but perform little checks on the data given. The only
203 thing it checks is that the username isn't in use and allready. Then,
204 if the authlevel is different from 1, it is checked if the logged in
205 user is privileged to set the authlevel.
206  
207 =cut
208  
209 sub new_user
210 {
211 return << 'EOC';
212 my %args = map { $_ => join('', $cgi->param($_)) } $cgi->param;
213 my $user = AxKit::App::TABOO::Data::User::Contributor->new();
214 if($args{'username'} =~ m/comment|thread|all|respond|edit/) {
215 throw Apache::AxKit::Exception::Retval(
216 return_code => FORBIDDEN,
217 -text => "Username matching a administratively prohibited name")
218 }
219 if($user->load_name($args{'username'})) {
220 throw Apache::AxKit::Exception::Retval(
221 return_code => FORBIDDEN,
222 -text => "User exists allready")
223 }
224 my $extremes = AxKit::App::TABOO::XSP::User::authlevel_extremes('');
225 if(($args{'authlevel'} > ${$extremes}{'maxlevel'}) || (! $args{'authlevel'})) {
226 $args{'authlevel'} = 1;
227 }
228 $args{'passwd'} = crypt($args{'passwd'}, AxKit::App::TABOO::XSP::User::makeSalt());
229 $user->populate(\%args);
230 AxKit::Debug(9, "Saving new user " . $args{'username'});
231 $user->save();
232 EOC
233 }
234  
235  
236 =head2 C<E<lt>get-passwd username="foo"/E<gt>>
237  
238 This tag will return the password of a user. The username may be given
239 in an attribute or child element named C<username>.
240  
241 =cut
242  
243 sub get_passwd : expr attribOrChild(username)
244 {
245 return << 'EOC';
246 my $user = AxKit::App::TABOO::Data::User->new();
247 $user->load_passwd($attr_username);
248 EOC
249 }
250  
251  
252  
253 =head2 C<E<lt>get-authlevel username="foo"/E<gt>>
254  
255 This tag will return the authorization level of a user, which is an
256 integer that may be used to grant or deny access to certain elements
257 or pages. The username may be given in an attribute or child element
258 named C<username>.
259  
260 =cut
261  
262  
263 sub get_authlevel : expr attribOrChild(username)
264 {
265 return << 'EOC';
266 my $user = AxKit::App::TABOO::Data::User::Contributor->new();
267 $user->load_authlevel($attr_username);
268 EOC
269 }
270  
271  
272  
273 =head2 C<E<lt>get-user username="foo"/E<gt>>
274  
275 This tag will return and XML representation of the user
276 information. The username may be given in an attribute or child
277 element named C<username>.
278  
279 =cut
280  
281 sub get_user : struct attribOrChild(username)
282 {
283 return << 'EOC';
284 my $user = AxKit::App::TABOO::Data::User::Contributor->new();
285 $user->load(what => '*', limit => {'username' => $attr_username});
286 my $doc = XML::LibXML::Document->new();
287 my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/User/Output', 'user:get-user');
288 $doc->setDocumentElement($root);
289 $user->write_xml($doc, $root); # Return an XML representation
290 EOC
291 }
292  
293  
294  
295  
296  
297 =head2 C<E<lt>exists username="foo"/E<gt>>
298  
299 This tag will check if a user allready exists. Like
300 C<E<lt>password-matchesE<gt>> this tag is a boolean tag, which has
301 child elements C<E<lt>trueE<gt>> and C<E<lt>falseE<gt>>. It takes a
302 username, which may be given as an attribute or a child element named
303 C<username>, and if the user is found in the data store, the contents
304 of C<E<lt>trueE<gt>> child element is included, otherwise, the
305 contents of C<E<lt>falseE<gt>> is included.
306  
307 =cut
308  
309 sub exists : attribOrChild(username) {
310 return ''; # Gotta be something here
311 }
312  
313 sub exists___true__open {
314 return << 'EOC';
315 if (AxKit::App::TABOO::XSP::User::_exists_check($attr_username)) {
316 EOC
317 }
318  
319 sub exists___true {
320 return '}'
321 }
322  
323  
324 sub exists___false__open {
325 return << 'EOC';
326 unless (AxKit::App::TABOO::XSP::User::_exists_check($attr_username)) {
327 EOC
328 }
329  
330 sub exists___false {
331 return '}'
332 }
333  
334  
335 =head2 C<E<lt>is-authorized authlevel="5" username="foo"E<gt>>
336  
337 This is a boolean tag, which has child elements C<E<lt>trueE<gt>> and
338 C<E<lt>falseE<gt>>. It takes an autherization level in an attribute or
339 child element named C<authlevel>, and an attribute or child element
340 named C<username>. If the authenticated user has it least this level
341 I<or> the given C<username> matches the username of the authenticated
342 user, the contents of the C<E<lt>trueE<gt>> element will be included
343 in the output document. Conversely, if the user has insufficient
344 privileges the contents of C<E<lt>falseE<gt>> will be in the result
345 document.
346  
347 B<NOTE:> This should I<not> be looked upon as a "security feature".
348 While it is possible to use it to make sure that an input control is
349 not shown to someone who is not authorized to modify it (and this may
350 indeed be its primary use), a malicious user could still insert data
351 to that field by supplying arguments in a POST or GET
352 request. Consequently, critical data must be checked for sanity before
353 they are passed to the Data objects. The Data objects themselves are
354 designed to believe anything they're fed, so it is most natural to do
355 it in a taglib before handing the data to a Data object. See e.g. the
356 internals of the C<E<lt>store/E<gt>> tag for an example.
357  
358  
359 =cut
360  
361 sub is_authorized : attribOrChild(username,authlevel) {
362 return ''; # Gotta be something here
363 }
364  
365 sub is_authorized___true__open {
366 return << 'EOC';
367 my $session = AxKit::App::TABOO::session($r);
368 my $editinguser = AxKit::App::TABOO::loggedin($session);
369 my $authlevel = AxKit::App::TABOO::authlevel($session);
370 AxKit::Debug(9, $editinguser . " is authorized at level " . $authlevel);
371 if ((defined($editinguser))
372 && (defined($authlevel))) {
373 if (($attr_username eq $editinguser)
374 || (($attr_authlevel)
375 && ($attr_authlevel <= $authlevel))) # Grant access
376 {
377 EOC
378 }
379  
380  
381 sub is_authorized___true {
382 return '} }'
383 }
384  
385  
386 sub is_authorized___false__open {
387 return << 'EOC';
388 my $session = AxKit::App::TABOO::session($r);
389 my $editinguser = AxKit::App::TABOO::loggedin($session);
390 my $authlevel = AxKit::App::TABOO::authlevel($session);
391 if ((! defined($editinguser)
392 || ($attr_username ne $editinguser))
393 && ((! defined($authlevel))
394 || ($attr_authlevel > $authlevel))) # Deny access
395 {
396 EOC
397 }
398  
399  
400 sub is_authorized___false {
401 return '}'
402 }
403  
404 =head2 C<E<lt>valid-authlevels/E<gt>>
405  
406 This returns a list of the authorization levels that the present user
407 can legitimitely set. This is an ugly and temporary solution, I think
408 it should be worked out elsewhere than the taglib, but I couldn't find
409 a way to do it....
410  
411 =cut
412  
413 sub valid_authlevels : nodelist({http://www.kjetil.kjernsmo.net/software/TABOO/NS/User/Output}level) attribOrChild(username) {
414 return << 'EOC';
415 # my @levels = ("Guest", "New member", "Member", "Oldtimer", "Assistant", "Editor", "Administrator", "Director", "Guru", "God");
416 my $extremes = AxKit::App::TABOO::XSP::User::authlevel_extremes($attr_username);
417 (${$extremes}{'minlevel'} > ${$extremes}{'maxlevel'})
418 ? ()
419 : (${$extremes}{'minlevel'} .. ${$extremes}{'maxlevel'});
420 EOC
421 }
422  
423  
424 =head2 C<E<lt>random-password/E<gt>>
425  
426 Shamelessly stolen from Jörg Walter's L<AxKit::XSP::Auth> taglib, this
427 would generate a new random password, see his documentation for
428 details.
429  
430 =cut
431  
432 sub random_password : expr attribOrChild(lang,signs,numbers,minlen,maxlen)
433 {
434 return 'Crypt::GeneratePassword::word(int($attr_minlen)||7,int($attr_maxlen)||7,$attr_lang,int($attr_signs),(defined $attr_numbers?int($attr_numbers):2))';
435 }
436  
437 =head2 C<E<lt>authnuser/E<gt>>
438  
439 This tag will return the username of the logged in and authenticated user.
440  
441 =cut
442  
443 sub authnuser : expr {
444 return 'AxKit::App::TABOO::loggedin(AxKit::App::TABOO::session($r));'
445 }
446  
447  
448  
449 1;
450  
451 =head1 TODO
452  
453 Currently, C<E<lt>existsE<gt>> checks if a user exists by checking if
454 the real name is defined. This is likely to change in the future. Do
455 not rely on this behaviour, but do make sure every-one has a real
456 name!
457  
458  
459 =head1 FORMALITIES
460  
461 See L<AxKit::App::TABOO>.
462  
463 =cut
464