Nitesi/Account/Manager.pm

filter: Statementstmt 42.4% 73 / 172 Branchbran 14.5% 9 / 62 Conditioncond 20.0% 2 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 57.1% 16 / 28 Podpod 94.7% 18 / 19 totaltotal 40.0% 119 / 297 CC 44 SCAR 32.4
CC
44
Cov
34%
create
110.0
exists
30.0
delete
20.0
CRAP 25.5
« Nitesi/Provider/Object.pm ↑ Summary Nitesi/Cart.pm »
1 package Nitesi::Account::Manager;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Nitesi::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Nitesi::Account::Password;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use ACL::Lite 0.0002;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Nitesi::Account::Manager - Account Manager for Nitesi Shop Machine
15  
16 =head1 SYNOPSIS
17  
18 $account = Nitesi::Account::Manager->new(provider_sub => \&account_providers,
19 session_sub => \&session);
20  
21 $account->init_from_session;
22  
23 $account->status(login_info => 'Please login before checkout',
24 login_continue => 'checkout');
25  
26 $account->login(username => 'shopper@nitesi.biz', password => 'nevairbe');
27  
28 $account->logout();
29  
30 if ($account->exists('shopper@nitesi.biz')) {
31 $account->password(username => 'shopper@nitesi.biz', password => 'nevairbe');
32 }
33  
34 $account->create(email => 'shopper@nitesi.biz');
35  
36 # use this with caution!
37 $account->become('shopper@nitesi.biz');
38  
39 =head1 DESCRIPTION
40  
41 Nitesi's account manager transparently handles multiple providers for authentication,
42 account data and permissions checks.
43  
44 =head1 METHODS
45  
46 =head2 init
47  
48 Initializer called by instance class method.
49  
50 =cut
51  
52 has password_manager => (
53 is => 'rw',
54 lazy => 1,
55 2 ▶ default => sub {Nitesi::Account::Password->new;},
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 );
57  
58 =head2 providers
59  
60 List with account providers.
61  
62 =cut
63  
64 1 has providers => (
65 is => 'ro',
66 );
67  
68 has session_sub => (
69 is => 'rw',
70 lazy => 1,
71 2 ▶ default => sub {sub {return 1;}},
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
72 1 );
73  
74 3 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=5 SCAR=16.3
Pod undocumented
75 3 my ($class, %args) = @_;
76 3 my ($ret, @list, $init);
77  
78 3 $args{providers} = [];
79
80 3 ▶ if ($args{provider_sub}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"provider_sub"}) 2 1 3
81 # retrieve list of providers
82 2 $ret = $args{provider_sub}->();
83
84 2 ▶ if (ref($ret) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ret eq 'HASH') { } 0 2 2
elsif (ref $ret eq 'ARRAY') { } 2 0 2
85 # just one provider
86 0 @list = ($ret);
87 }
88 elsif (ref($ret) eq 'ARRAY') {
89 2 @list = @$ret;
90 }
91  
92 # instantiate provider objects
93 2 for $init (@list) {
94 2 push @$init, 'crypt', Nitesi::Account::Password->new;
95 2 push @{$args{providers}}, Nitesi::Class->instantiate(@$init);
96 }
97  
98 2 delete $args{provider_sub};
99 }
100  
101 3 return \%args;
102 }
103  
104 =head2 init_from_session
105  
106 Reads user information through session routine.
107  
108 =cut
109  
110 0 ▶sub init_from_session {
Subroutine init_from_session: not called CC=2 SCAR=17.9
Pod documented
111 0 my $self = shift;
112  
113 0 ▶ $self->{account} = $self->{session_sub}->()
Condition0%0/2
$self->{'session_sub'}->() || {'uid', 0, 'username', '', 'roles', []}
l!l
00
MC/DC0%0/1
$self->{'session_sub'}->() || {'uid', 0, 'username', '', 'roles', []}
$self->{'session_sub'}->()
Truth table0%0/2
A
A= $self->{'session_sub'}->()
Aresult
11
00
114 || {uid => 0, username => '', roles => []};
115  
116 0 $self->{acl} = ACL::Lite->new(permissions => $self->{account}->{permissions});
117  
118 0 return;
119 }
120  
121 =head2 login
122  
123 Perform login. Returns 1 in case of success and
124 0 in case of failure.
125  
126 Leading and trailing spaces will be removed from
127 username and password in advance.
128  
129 =cut
130  
131 5 ▶sub login {
Subroutine login: called CC=3 SCAR=11.0
Pod documented
132 5 my ($self, %args) = @_;
133 5 my ($success, $acct);
134  
135 5 $success = 0;
136  
137 # remove leading/trailing spaces from username and password
138 5 $args{username} =~ s/^\s+//;
139 5 $args{username} =~ s/\s+$//;
140  
141 5 $args{password} =~ s/^\s+//;
142 5 $args{password} =~ s/\s+$//;
143  
144 5 my $id = 0;
145  
146 5 for my $p (@{$self->{providers}}) {
147 4 ▶ if ($acct = $p->login(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acct = $p->login(%args)) 2 2 4
148 2 $acct->{provider_id} = $id;
149 2 $self->session_sub->('init', $acct);
150 2 $self->{account} = $acct;
151 $self->{acl} = ACL::Lite->new(permissions => $self->{account}->{permissions},
152 2 uid => $acct->{uid});
153 2 $success = 1;
154 2 last;
155 }
156 2 $id++;
157 }
158  
159 5 return $success;
160 }
161  
162 =head2 logout
163  
164 Perform logout.
165  
166  
167 B<Example:>
168  
169 $account->logout();
170  
171 =cut
172  
173 2 ▶sub logout {
Subroutine logout: called CC=3 SCAR=11.1
Pod documented
174 2 my ($self, %args) = @_;
175 2 my ($provider);
176  
177 # log out if the user is authenticated, so skip it if uid is 0 (as
178 # per doc of uid).
179 2 ▶ if ($self->uid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uid) 1 1 2
180 1 $provider = $self->{providers}->[$self->{account}->{provider_id}];
181  
182 1 ▶ if ($provider->can('logout')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($provider->can("logout")) 0 1 1
183 0 $self->{providers}->[$self->{account}->{provider_id}]->logout;
184 }
185  
186 1 delete $self->{account};
187 1 $self->{acl} = ACL::Lite->new;
188 }
189  
190 2 $self->session_sub->('destroy');
191 }
192  
193 =head2 create
194  
195 Creates account and returns uid for the new account
196 in case of success.
197  
198 B<Example:>
199  
200 $uid = $account->create(email => 'shopper@nitesi.biz');
201  
202 The password is automatically generated unless you pass it to
203 this method.
204  
205 B<Example:>
206  
207 $uid = $account->create(email => 'shopper@nitesi.biz',
208 password => 'nevairbe');
209  
210 =cut
211  
212 0 ▶sub create {
Subroutine create: not called CC=10 SCAR=47.0
Pod documented
213 0 my ($self, %args) = @_;
214 0 my ($password, $uid);
215
216 # remove leading/trailing spaces from arguments
217 0 for my $name (keys %args) {
218 0 ▶ if (defined $args{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{$name}) 0 0 0
219 0 $args{$name} =~ s/^\s+//;
220 0 $args{$name} =~ s/\s+$//;
221 }
222 }
223  
224 0 ▶ unless (exists $args{username} && $args{username} =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"username"} and $args{"username"} =~ /\S/) 0 0 0
Condition0%0/3
exists $args{"username"} and $args{"username"} =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"username"} and $args{"username"} =~ /\S/
exists $args{"username"} $args{"username"} =~ /\S/
Truth table0%0/3
A and B
A= exists $args{"username"}B= $args{"username"} =~ /\S/
ABresult
0X0
100
111
225 0 $args{username} = lc($args{email});
226 }
227  
228 # password is added after account creation
229 0 ▶ unless ($password = delete $args{password}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($password = delete $args{"password"}) 0 0 0
230 0 $password = $self->password_manager->make_password;
231 }
232  
233 0 for my $p (@{$self->{providers}}) {
234 0 ▶ next unless $p->can('create');
Branch: 1 branch
BranchTrueFalseTotal
unless $p->can("create") 0 0 0
235  
236 0 ▶ if ($p->exists($args{username})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->exists($args{"username"})) 0 0 0
237 0 die "Account already exists: ", $args{username};
238 }
239  
240 0 ▶ if ($uid = $p->create(%args)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uid = $p->create(%args)) 0 0 0
241 $self->password(username => $args{username},
242 0 password => $password);
243 0 last;
244 }
245 }
246  
247 0 return $uid;
248 }
249  
250 =head2 delete
251  
252 Delete account.
253  
254 B<Example:>
255  
256 $account->delete('333');
257  
258 =cut
259  
260 0 ▶sub delete {
Subroutine delete: not called CC=4 SCAR=30.0
Pod documented
261 0 my ($self, $uid, $p);
262  
263 0 $self = shift;
264  
265 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
266 0 $uid = shift;
267 }
268 else {
269 0 $uid = $self->uid;
270 }
271  
272 0 for $p (@{$self->{providers}}) {
273 0 ▶ if ($p->load($uid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->load($uid)) 0 0 0
274 0 return $p->delete($uid);
275 }
276 }
277  
278 0 return;
279 }
280  
281 =head2 uid
282  
283 Retrieve user identifier of the current user, returns 0 if current user
284 isn't authenticated.
285  
286 B<Example:>
287  
288 $account->uid();
289  
290 =cut
291  
292 3 ▶sub uid {
Subroutine uid: called CC=2 SCAR=6.9
Pod documented
293 3 my $self = shift;
294  
295 3 ▶ return $self->{account}->{uid} || 0;
Condition100%2/2
$self->{'account'}{'uid'} || 0
l!l
12
MC/DC100%1/1
$self->{'account'}{'uid'} || 0
$self->{'account'}{'uid'}
Truth table100%2/2
A
A= $self->{'account'}{'uid'}
Aresult
11
00
296 }
297  
298 =head2 username
299  
300 Retrieve username of the current user. Returns empty string if current user
301 isn't authenticated. If you want to retrieve other user username, use $account->load.
302  
303 B<Example:>
304  
305 $account->username();
306  
307 =cut
308  
309 1 ▶sub username {
Subroutine username: called CC=1 SCAR=0.0
Pod documented
310 1 my $self = shift;
311  
312 1 return $self->{account}->{username};
313 }
314  
315 =head2 roles
316  
317 Retrieve roles of current user.
318  
319 B<Example:>
320  
321 $account->roles();
322  
323 =cut
324  
325 0 ▶sub roles {
Subroutine roles: not called CC=2 SCAR=17.9
Pod documented
326 0 my $self = shift;
327  
328 0 ▶ wantarray ? @{$self->{account}->{roles}} : $self->{account}->{roles};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
329 }
330  
331 =head2 has_role
332  
333 Returns true if user is a member of the given role.
334  
335 B<Example:>
336  
337 if ($account->has_role('admin') { print "Congratulations, you are the admin" };
338  
339 =cut
340  
341 0 ▶sub has_role {
Subroutine has_role: not called CC=1 SCAR=6.9
Pod documented
342 0 my ($self, $role) = @_;
343  
344 0 grep {$role eq $_} @{$self->{account}->{roles}};
345 }
346  
347 =head2 permissions
348  
349 Returns permissions as hash reference:
350  
351 $perms = $account->permissions;
352  
353 Returns permissions as list:
354  
355 @perms = $account->permissions;
356  
357 =cut
358  
359 3 ▶sub permissions {
Subroutine permissions: called CC=1 SCAR=0.0
Pod documented
360 3 my ($self) = @_;
361  
362 3 return $self->{acl}->permissions;
363 }
364  
365 =head2 status
366  
367 Helps you to redirect users properly on pages available only to authenticated users.
368  
369 B<Example:> Before login - Page available only if you are logged in (Step 1)
370  
371 You are not logged in. You are on a page which is available only to those logged in.
372 You set the message for users not logged in and url of the page where you send them after successful login.
373  
374 $account->status(login_info => 'Please login before checkout', login_continue => 'checkout');
375  
376 B<Example:> At Login page (Step 2)
377  
378 You retrieve the login message to make clear to user why they need to login (to access the page from step 1)
379  
380 $account->status('login_info');
381  
382 B<Example:> After login (Step 3)
383  
384 Retrieve the login_continue URL and send user to that URL (using redirect or something similar).
385  
386 $account->status('login_continue');
387  
388 =cut
389  
390 0 ▶sub status {
Subroutine status: not called CC=4 SCAR=30.0
Pod documented
391 0 my ($self, @args) = @_;
392  
393 0 ▶ if (@args > 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args > 1) { } 0 0 0
elsif (@args == 1) { } 0 0 0
394 # update status information
395 0 $self->{account} = $self->session_sub->('update', {@args});
396 }
397 elsif (@args == 1) {
398 0 ▶ if (exists $self->{account}->{$args[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'account'}{$args[0]}) { } 0 0 0
399 0 return $self->{account}->{$args[0]};
400 }
401 else {
402 0 return '';
403 }
404 }
405 }
406  
407 =head2 exists
408  
409 Check whether account exists.
410  
411 B<Example:>
412  
413 if ($account->exists('shopper@nitesi.biz')) {
414 print "Account exists\n";
415 }
416  
417 =cut
418  
419 0 ▶sub exists {
Subroutine exists: not called CC=5 SCAR=34.0
Pod documented
420 0 my ($self, $username) = @_;
421  
422 0 ▶ return unless defined $username && $username =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $username and $username =~ /\S/ 0 0 0
Condition0%0/3
defined $username and $username =~ /\S/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $username and $username =~ /\S/
defined $username $username =~ /\S/
Truth table0%0/3
A and B
A= defined $usernameB= $username =~ /\S/
ABresult
0X0
100
111
423  
424 0 for my $p (@{$self->{providers}}) {
425 0 ▶ if ($p->exists($username)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->exists($username)) 0 0 0
426 0 return $p;
427 }
428 }
429 }
430  
431 =head2 load
432  
433 Returns account data for a given uid as hash.
434  
435 B<Example:>
436  
437 $account->load('333');
438  
439 =cut
440  
441 0 ▶sub load {
Subroutine load: not called CC=3 SCAR=24.8
Pod documented
442 0 my ($self, $uid) = @_;
443 0 my ($data);
444  
445 0 for my $p (@{$self->{providers}}) {
446 0 ▶ if ($data = $p->load($uid)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data = $p->load($uid)) 0 0 0
447 0 return $data;
448 }
449 }
450 }
451  
452 =head2 password
453  
454 Changes password for current account:
455  
456 $account->password('nevairbe');
457  
458 Changes password for other account:
459  
460 $account->password(username => 'shopper@nitesi.biz',
461 password => 'nevairbe');
462  
463 =cut
464  
465 0 ▶sub password {
Subroutine password: not called CC=4 SCAR=30.0
Pod documented
466 0 my $self = shift;
467 0 my ($provider, %args);
468  
469 0 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
470 # new password only
471 0 ▶ unless ($self->{account}->{username}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"account"}{"username"}) 0 0 0
472 0 die "Cannot change password for anonymous user";
473 }
474  
475 0 $args{username} = $self->{account}->{username};
476 0 $args{password} = shift;
477 }
478 else {
479 0 %args = @_;
480  
481 0 ▶ unless ($provider = $self->exists($args{username})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provider = $self->exists($args{"username"})) 0 0 0
482 0 die "Cannot change password for user $args{username}.";
483 }
484 }
485  
486 $provider->password($self->password_manager->password($args{password}),
487 0 $args{username});
488 }
489  
490 =head2 acl
491  
492 ACL (Access list) check, see L<ACL::Lite> for details.
493  
494 B<Example:>
495  
496 if ( $account->acl( check => 'view_prices') {
497 print "You can see prices";
498 }
499  
500  
501 B<Example:>
502  
503 If you check multiple permissions at once, only one has to granted.
504 The check will return the name of the first granted one in the list (left to right).
505  
506 if ( $account->acl( check => [ qw/admin luka/ ] ) {
507 print "This is Luka's account. Only Luka and administrators can see it".
508 }
509  
510 =cut
511  
512 0 ▶sub acl {
Subroutine acl: not called CC=3 SCAR=24.8
Pod documented
513 0 my ($self, $function, @args) = @_;
514  
515 0 ▶ if ($self->{acl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"acl"}) 0 0 0
516 0 ▶ if ($function eq 'check') {
Branch: 1 branch
BranchTrueFalseTotal
if ($function eq "check") 0 0 0
517 0 $self->{acl}->check(@args);
518 }
519 }
520 }
521  
522 =head2 value
523  
524 Retrieve or set account data.
525  
526 B<Example:> Retrieve city
527  
528 $city = $account->value( 'city');
529  
530 B<Example:> Set city
531  
532 $city = $account->value( city => 'Ljubljana');
533  
534 =cut
535  
536 0 ▶sub value {
Subroutine value: not called CC=4 SCAR=30.0
Pod documented
537 0 my ($self, $name, $value) = @_;
538  
539 0 ▶ if (@_ == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 0 0 0
540 # update value
541 0 my ($username, $provider);
542  
543 0 $username = $self->{account}->{username};
544  
545 0 ▶ unless ($provider = $self->exists($username)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($provider = $self->exists($username)) 0 0 0
546 0 die "Cannot change value $name for user $username.";
547 }
548  
549 0 $provider->value($username, $name, $value);
550 0 $self->{account} = $self->session_sub->('update', {$name => $value});
551  
552 0 return $value;
553 }
554  
555 0 ▶ if (exists $self->{account}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"account"}{$name}) 0 0 0
556 0 return $self->{account}->{$name};
557 }
558 }
559  
560 =head2 last_login
561  
562 Returns time of last login (before the current one) in seconds
563 since epoch or undef if provider doesn't supply this information.
564  
565 =cut
566  
567 2 ▶sub last_login {
Subroutine last_login: called CC=1 SCAR=0.0
Pod documented
568 2 my ($self) = @_;
569  
570 2 return $self->{account}->{last_login};
571 }
572  
573 =head2 become
574  
575 Become any user you want:
576
577 $acct->become('shopper@nitesi.biz');
578  
579 Please use this method with caution.
580  
581 Some parts of the system (DBI, LDAP,...) may choose not to support this method.
582  
583 =cut
584  
585 0 ▶sub become {
Subroutine become: not called CC=4 SCAR=30.0
Pod documented
586 0 my ($self, $username) = @_;
587 0 my ($p, $acct);
588  
589 0 my $id = 0;
590  
591 0 for $p (@{$self->{providers}}) {
592 0 ▶ if ($p->can('become')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->can("become")) 0 0 0
593 0 ▶ if ($acct = $p->become($username)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($acct = $p->become($username)) 0 0 0
594 0 $acct->{provider_id} = $id;
595 0 $self->session_sub->('init', $acct);
596 0 $self->{account} = $acct;
597 $self->{acl} = ACL::Lite->new(permissions => $self->{account}->{permissions},
598 0 uid => $acct->{uid});
599 0 return 1;
600 }
601 }
602  
603 0 $id++;
604 }
605 }
606  
607 =head1 AUTHOR
608  
609 Stefan Hornburg (Racke), <racke@linuxia.de>
610  
611 =head1 LICENSE AND COPYRIGHT
612  
613 Copyright 2011-2013 Stefan Hornburg (Racke) <racke@linuxia.de>.
614  
615 This program is free software; you can redistribute it and/or modify it
616 under the terms of either: the GNU General Public License as published
617 by the Free Software Foundation; or the Artistic License.
618  
619 See http://dev.perl.org/licenses/ for more information.
620  
621 =cut
622  
623 1 1;
« Nitesi/Provider/Object.pm ↑ Summary Nitesi/Cart.pm »