| 1 | # ABSTRACT: Dancer Authentication, Security and Role-Based Access Control Framework! | ||||||||||
| 2 | |||||||||||
| 3 | package Dancer::Plugin::Auth::RBAC; | ||||||||||
| 4 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | $Dancer::Plugin::Auth::RBAC::VERSION = '1.110720'; | |||||||||
| 6 | } | ||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 3 | ▶ | use Dancer qw/:syntax/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 3 | ▶ | use Dancer::Plugin; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 3 | our $settings = {}; | |||||||||
| 13 | |||||||||||
| 14 | 6 | ▶ | register auth => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 6 | $settings = plugin_setting; | |||||||||
| 16 | 6 | return Dancer::Plugin::Auth::RBAC->new(@_) | |||||||||
| 17 | 3 | }; | |||||||||
| 18 | |||||||||||
| 19 | 0 | ▶ | register authd => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 20 | 0 | ▶ | if ( session('user') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 0 | ▶ | if ( session('user')->{id} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | 0 | return true; | |||||||||
| 23 | } | ||||||||||
| 24 | } | ||||||||||
| 25 | 0 | return false; | |||||||||
| 26 | 3 | }; | |||||||||
| 27 | |||||||||||
| 28 | |||||||||||
| 29 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 30 | 6 | my $class = shift; | |||||||||
| 31 | 6 | my @credentials = @_; | |||||||||
| 32 | |||||||||||
| 33 | my $credentialsClass = | ||||||||||
| 34 | 6 | __PACKAGE__ . "::Credentials::" . $settings->{credentials}->{class}; | |||||||||
| 35 | { | ||||||||||
| 36 | 3 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 6 | $credentialsClass =~ s/::/\//g; | |||||||||
| 38 | 6 | require "$credentialsClass.pm"; | |||||||||
| 39 | 6 | $credentialsClass =~ s/\//::/g; | |||||||||
| 40 | } | ||||||||||
| 41 | |||||||||||
| 42 | 6 | my $self = {}; | |||||||||
| 43 | 6 | bless $self, $class; | |||||||||
| 44 | |||||||||||
| 45 | # return $credentialsClass->new | ||||||||||
| 46 | # unless scalar @credentials; | ||||||||||
| 47 | |||||||||||
| 48 | 6 | my $user = session('user'); | |||||||||
| 49 | |||||||||||
| 50 | 6 | ▶ | if ($user) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | # reset authentication errors | ||||||||||
| 52 | 4 | $user->{error} = []; | |||||||||
| 53 | } | ||||||||||
| 54 | else { | ||||||||||
| 55 | # initialize user session object | ||||||||||
| 56 | 2 | $user = { | |||||||||
| 57 | id => undef, | ||||||||||
| 58 | name => undef, | ||||||||||
| 59 | login => undef, | ||||||||||
| 60 | roles => [], | ||||||||||
| 61 | error => [] | ||||||||||
| 62 | }; | ||||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | 6 | session 'user' => $user; | |||||||||
| 66 | |||||||||||
| 67 | #return $credentialsClass->new->authorize($settings->{credentials}->{options}, @credentials) | ||||||||||
| 68 | #? $self : undef; | ||||||||||
| 69 | |||||||||||
| 70 | 6 | $credentialsClass->new->authorize($settings->{credentials}->{options}, @credentials); | |||||||||
| 71 | 6 | return $self; | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | 12 | ▶ | sub asa { | ||||||||
|
Subroutine
asa: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 75 | 12 | my $self = shift; | |||||||||
| 76 | my $permissionsClass = | ||||||||||
| 77 | 12 | __PACKAGE__ . "::Permissions::" . $settings->{permissions}->{class}; | |||||||||
| 78 | { | ||||||||||
| 79 | 3 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 80 | 12 | $permissionsClass =~ s/::/\//g; | |||||||||
| 81 | 12 | require "$permissionsClass.pm"; | |||||||||
| 82 | 12 | $permissionsClass =~ s/\//::/g; | |||||||||
| 83 | } | ||||||||||
| 84 | 12 | return $permissionsClass->new->subject_asa($settings->{permissions}->{options}, @_); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 10 | ▶ | sub can { | ||||||||
|
Subroutine
can: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 88 | 10 | my $self = shift; | |||||||||
| 89 | my $permissionsClass = | ||||||||||
| 90 | 10 | __PACKAGE__ . "::Permissions::" . $settings->{permissions}->{class}; | |||||||||
| 91 | { | ||||||||||
| 92 | 3 | ▶ | no warnings 'redefine'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 10 | $permissionsClass =~ s/::/\//g; | |||||||||
| 94 | 10 | require "$permissionsClass.pm"; | |||||||||
| 95 | 10 | $permissionsClass =~ s/\//::/g; | |||||||||
| 96 | } | ||||||||||
| 97 | 10 | return $permissionsClass->new->subject_can($settings->{permissions}->{options}, @_); | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 0 | ▶ | sub roles { | ||||||||
|
Subroutine
roles: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||
| 101 | 0 | my $self = shift; | |||||||||
| 102 | 0 | ▶ | if (@_) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 103 | 0 | my $user = session('user'); | |||||||||
| 104 | 0 | ▶ | if ($user) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | 0 | ▶ | if ($user->{id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | 0 | push @{$user->{roles}}, @_; | |||||||||
| 107 | 0 | session 'user' => $user; | |||||||||
| 108 | } | ||||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | else { | ||||||||||
| 112 | 0 | my $user = session('user'); | |||||||||
| 113 | 0 | ▶ | if ($user) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | ▶ | if ($user->{id}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 115 | 0 | return $user->{roles}; | |||||||||
| 116 | } | ||||||||||
| 117 | } | ||||||||||
| 118 | } | ||||||||||
| 119 | } | ||||||||||
| 120 | |||||||||||
| 121 | 6 | ▶ | sub errors { | ||||||||
|
Subroutine
errors: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 122 | 6 | my $self = shift; | |||||||||
| 123 | 6 | return @{ session('user')->{error} }; | |||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | 6 | ▶ | sub revoke { | ||||||||
|
Subroutine
revoke: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 127 | 6 | my $self = shift; | |||||||||
| 128 | 6 | return session 'user' => {}; | |||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 3 | register_plugin; | |||||||||
| 132 | |||||||||||
| 133 | 3 | 1; | |||||||||
| 134 | |||||||||||
| 135 | __END__ | ||||||||||