| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Catalyst::Plugin::Authorization::Roles; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Set::Object (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Catalyst::Exception (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | sub check_user_roles { | ||||||||||||||||||||||||||
|
Subroutine
check_user_roles: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 4 | my ( $c, @roles ) = @_; | |||||||||||||||||||||||||||
| 16 | 4 | local $@; | |||||||||||||||||||||||||||
| 17 | 4 | eval { $c->assert_user_roles(@roles) }; | |||||||||||||||||||||||||||
| 18 | 4 | ▶ | return $@ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 18 | ▶ | sub assert_user_roles { | ||||||||||||||||||||||||||
|
Subroutine
assert_user_roles: called CC=13 SCAR=29.4
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 18 | my ( $c, @roles ) = @_; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 18 | my $user; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 18 | ▶ | if ( Scalar::Util::blessed( $roles[0] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($roles[0]) and $roles[0]->isa("Catalyst::Authentication::User")
MC/DC50%1/2
builtin::blessed($roles[0]) and $roles[0]->isa("Catalyst::Authentication::User")
builtin::blessed($roles[0])
$roles[0]->isa("Catalyst::Authentication::User")
Truth table66%2/3
A and B
A= builtin::blessed($roles[0])B= $roles[0]->isa("Catalyst::Authentication::User")
| |||||||||||||||||||||||||||||
| 27 | && $roles[0]->isa("Catalyst::Authentication::User") ) | ||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 2 | $user = shift @roles; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 18 | ▶ | $user ||= $c->user; | ||||||||||||||||||||||||||
|
Condition100%3/3
$user ||= $c->user
MC/DC100%2/2
$user ||= $c->user
$user
$c->user
Truth table100%3/3
A ||= B
A= $userB= $c->user
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 18 | ▶ | unless ( $user ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 2 | Catalyst::Exception->throw( | |||||||||||||||||||||||||||
| 36 | "No logged in user, and none supplied as argument"); | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 16 | ▶ | Catalyst::Exception->throw("User does not support roles") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | unless $user->supports(qw/roles/); | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 16 | local $" = ", "; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 16 | ▶ | if ( $user->supports(qw/roles self_check/) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( $user->check_roles(@roles) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | $c->log->debug("Role granted: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | return 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | else { | ||||||||||||||||||||||||||||
| 50 | 0 | ▶ | $c->log->debug("Role denied: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | 0 | Catalyst::Exception->throw("Missing roles"); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | else { | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 16 | my $have = Set::Object->new($user->roles); | |||||||||||||||||||||||||||
| 57 | 16 | my $need = Set::Object->new(@roles); | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 16 | ▶ | if ( $have->superset($need) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 8 | ▶ | $c->log->debug("Role granted: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 8 | return 1; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | else { | ||||||||||||||||||||||||||||
| 64 | 8 | ▶ | $c->log->debug("Role denied: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 8 | my @missing = $need->difference($have)->members; | |||||||||||||||||||||||||||
| 66 | 8 | Catalyst::Exception->throw("Missing roles: @missing"); | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 4 | ▶ | sub check_any_user_role { | ||||||||||||||||||||||||||
|
Subroutine
check_any_user_role: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | 4 | my ( $c, @roles ) = @_; | |||||||||||||||||||||||||||
| 74 | 4 | local $@; | |||||||||||||||||||||||||||
| 75 | 4 | eval { $c->assert_any_user_role(@roles) }; | |||||||||||||||||||||||||||
| 76 | 4 | ▶ | return $@ ? 0 : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 14 | ▶ | sub assert_any_user_role { | ||||||||||||||||||||||||||
|
Subroutine
assert_any_user_role: called CC=13 SCAR=29.8
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | 14 | my ( $c, @roles ) = @_; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 14 | my $user; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 14 | ▶ | if ( Scalar::Util::blessed( $roles[0] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($roles[0]) and $roles[0]->isa("Catalyst::Authentication::User")
MC/DC50%1/2
builtin::blessed($roles[0]) and $roles[0]->isa("Catalyst::Authentication::User")
builtin::blessed($roles[0])
$roles[0]->isa("Catalyst::Authentication::User")
Truth table66%2/3
A and B
A= builtin::blessed($roles[0])B= $roles[0]->isa("Catalyst::Authentication::User")
| |||||||||||||||||||||||||||||
| 85 | && $roles[0]->isa("Catalyst::Authentication::User") ) | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 2 | $user = shift @roles; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 14 | ▶ | $user ||= $c->user; | ||||||||||||||||||||||||||
|
Condition100%3/3
$user ||= $c->user
MC/DC100%2/2
$user ||= $c->user
$user
$c->user
Truth table100%3/3
A ||= B
A= $userB= $c->user
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 14 | ▶ | unless ( $user ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 2 | Catalyst::Exception->throw( | |||||||||||||||||||||||||||
| 94 | "No logged in user, and none supplied as argument"); | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 12 | ▶ | Catalyst::Exception->throw("User does not support roles") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | unless $user->supports(qw/roles/); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 12 | ▶ | if ( $user->supports(qw/roles self_check_any/) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( $user->check_roles_any(@roles) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | $c->log->debug("At least one role granted: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | return 1; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||
| 106 | 0 | ▶ | $c->log->debug("Roles denied: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | Catalyst::Exception->throw("Missing roles"); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | else { | ||||||||||||||||||||||||||||
| 111 | 12 | my $have = Set::Object->new($user->roles); | |||||||||||||||||||||||||||
| 112 | 12 | my $need = Set::Object->new(@roles); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 12 | ▶ | if ( $have->intersection($need)->size > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 8 | ▶ | $c->log->debug("At least one role granted: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 8 | return 1; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | else { | ||||||||||||||||||||||||||||
| 119 | 4 | ▶ | $c->log->debug("Role denied: @roles") if $c->debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 4 | Catalyst::Exception->throw( "Missing roles" ); | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3 | __PACKAGE__; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||