| 1 | package Articulate::Permission; | ||||||||||||
| 2 | 11 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 11 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 11 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 11 | ▶ | use Devel::StackTrace; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 11 | ▶ | use overload bool => sub { shift->granted }, '0+' => sub { shift->granted }; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Articulate::Permission - represent a permission request/response | ||||||||||||
| 13 | |||||||||||||
| 14 | =cut | ||||||||||||
| 15 | |||||||||||||
| 16 | =head1 FUNCTIONS | ||||||||||||
| 17 | |||||||||||||
| 18 | =head3 permission | ||||||||||||
| 19 | |||||||||||||
| 20 | my $permission = permission $user, verb => $location; | ||||||||||||
| 21 | |||||||||||||
| 22 | Creates a new request, using the verb and data supplied as the respective arguments. | ||||||||||||
| 23 | |||||||||||||
| 24 | =cut | ||||||||||||
| 25 | |||||||||||||
| 26 | 11 | ▶ | use Exporter::Declare; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | 11 | default_exports qw(permission); | |||||||||||
| 28 | |||||||||||||
| 29 | |||||||||||||
| 30 | sub permission { | ||||||||||||
| 31 | 7 | ▶ | __PACKAGE__->new( { | ||||||||||
|
Subroutine
permission: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
shift() // "[guest]"
MC/DC100%1/1
shift() // "[guest]"
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||
| 32 | user_id => shift // '[guest]', | ||||||||||||
| 33 | verb => shift, | ||||||||||||
| 34 | location => shift | ||||||||||||
| 35 | } ); | ||||||||||||
| 36 | }; | ||||||||||||
| 37 | |||||||||||||
| 38 | =head1 METHODS | ||||||||||||
| 39 | |||||||||||||
| 40 | =head3 new | ||||||||||||
| 41 | |||||||||||||
| 42 | An unremarkable Moo constructor. | ||||||||||||
| 43 | |||||||||||||
| 44 | =cut | ||||||||||||
| 45 | |||||||||||||
| 46 | =head3 grant | ||||||||||||
| 47 | |||||||||||||
| 48 | $permission->grant('Anybody can do that!'); | ||||||||||||
| 49 | |||||||||||||
| 50 | Declares that the user has that permission, for the reason given; sets C<granted> and C<denied> and populates the stack trace. | ||||||||||||
| 51 | |||||||||||||
| 52 | =cut | ||||||||||||
| 53 | |||||||||||||
| 54 | sub grant { | ||||||||||||
| 55 | 6 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
grant: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 56 | 6 | my $reason = shift; | |||||||||||
| 57 | # die if granted or denied are already set? | ||||||||||||
| 58 | 6 | $self->granted(1); | |||||||||||
| 59 | 6 | $self->reason($reason); | |||||||||||
| 60 | 6 | $self->stack_trace(Devel::StackTrace->new); | |||||||||||
| 61 | 6 | return $self; | |||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | =head3 deny | ||||||||||||
| 65 | |||||||||||||
| 66 | $permission->deny('Don\t touch that!'); | ||||||||||||
| 67 | |||||||||||||
| 68 | Declares that the user does not have that permission, for the reason given; sets C<granted> and C<denied> and populates the stack trace. | ||||||||||||
| 69 | |||||||||||||
| 70 | =cut | ||||||||||||
| 71 | |||||||||||||
| 72 | sub deny { | ||||||||||||
| 73 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
deny: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 74 | 0 | my $reason = shift; | |||||||||||
| 75 | # die if granted or denied are already set? | ||||||||||||
| 76 | 0 | $self->granted(0); | |||||||||||
| 77 | 0 | $self->denied(1); | |||||||||||
| 78 | 0 | $self->reason($reason); | |||||||||||
| 79 | 0 | $self->stack_trace(Devel::StackTrace->new); | |||||||||||
| 80 | 0 | return $self; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | =head1 ATTRIBUTES | ||||||||||||
| 84 | |||||||||||||
| 85 | =head3 user_id | ||||||||||||
| 86 | |||||||||||||
| 87 | The user_id requesting permission to access the resource. | ||||||||||||
| 88 | |||||||||||||
| 89 | =cut | ||||||||||||
| 90 | |||||||||||||
| 91 | has user_id => | ||||||||||||
| 92 | is => 'rw', | ||||||||||||
| 93 | 0 | ▶ | default => sub { undef }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 94 | |||||||||||||
| 95 | =head3 verb | ||||||||||||
| 96 | |||||||||||||
| 97 | The action being performed, e.g. C<read>, C<write>, etc. The verbs available are entirely dependant on the application. | ||||||||||||
| 98 | |||||||||||||
| 99 | A permission request will be granted or denied by an authorisation rule (see Articulate::Authorisation), who will typically implement verbs that bay be different from but are either a) broader than, or b) equally broad as, the verbs used by the Articulate::Service. | ||||||||||||
| 100 | |||||||||||||
| 101 | =cut | ||||||||||||
| 102 | |||||||||||||
| 103 | has verb => | ||||||||||||
| 104 | is => 'rw', | ||||||||||||
| 105 | 0 | ▶ | default => sub { 'error' }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 106 | |||||||||||||
| 107 | =head3 location | ||||||||||||
| 108 | |||||||||||||
| 109 | The location of the resource for which permission is requested. | ||||||||||||
| 110 | |||||||||||||
| 111 | =cut | ||||||||||||
| 112 | |||||||||||||
| 113 | has location => | ||||||||||||
| 114 | is => 'rw', | ||||||||||||
| 115 | 0 | ▶ | default => sub { [] }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||
| 116 | |||||||||||||
| 117 | =head3 granted | ||||||||||||
| 118 | |||||||||||||
| 119 | Whether or not the permission has been explicitly granted. The value of this is used for overload behaviour. | ||||||||||||
| 120 | |||||||||||||
| 121 | Please do not explicitly set this. Use C<grant> instead. | ||||||||||||
| 122 | |||||||||||||
| 123 | =cut | ||||||||||||
| 124 | |||||||||||||
| 125 | has granted => | ||||||||||||
| 126 | is => 'rw', | ||||||||||||
| 127 | 7 | ▶ | default => sub { 0 }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 128 | |||||||||||||
| 129 | =head3 denied | ||||||||||||
| 130 | |||||||||||||
| 131 | Whether the permission has been explicitly denied. | ||||||||||||
| 132 | |||||||||||||
| 133 | Please do not explicitly set this. Use C<deny> instead. | ||||||||||||
| 134 | |||||||||||||
| 135 | =cut | ||||||||||||
| 136 | |||||||||||||
| 137 | has denied => | ||||||||||||
| 138 | is => 'rw', | ||||||||||||
| 139 | 7 | ▶ | default => sub { 0 }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 140 | |||||||||||||
| 141 | =head3 reason | ||||||||||||
| 142 | |||||||||||||
| 143 | The reason for the grant or denial of permission. | ||||||||||||
| 144 | |||||||||||||
| 145 | Please do not explicitly set this. Use C<grant> or C<deny> instead. | ||||||||||||
| 146 | |||||||||||||
| 147 | =cut | ||||||||||||
| 148 | |||||||||||||
| 149 | has reason => | ||||||||||||
| 150 | is => 'rw', | ||||||||||||
| 151 | 7 | ▶ | default => sub { '' }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 152 | |||||||||||||
| 153 | =head3 stack_trace | ||||||||||||
| 154 | |||||||||||||
| 155 | The stack trace at the point of grant or denial of permission. | ||||||||||||
| 156 | |||||||||||||
| 157 | Please do not explicitly set this. Use C<grant> or C<deny> instead. | ||||||||||||
| 158 | |||||||||||||
| 159 | =cut | ||||||||||||
| 160 | |||||||||||||
| 161 | has stack_trace => | ||||||||||||
| 162 | is => 'rw', | ||||||||||||
| 163 | 7 | ▶ | default => sub { '' }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 164 | |||||||||||||
| 165 | =head1 SEE ALSO | ||||||||||||
| 166 | |||||||||||||
| 167 | =over | ||||||||||||
| 168 | |||||||||||||
| 169 | =item * L<Articulate::Authorisation> | ||||||||||||
| 170 | |||||||||||||
| 171 | =item * L<Articulate::Credentials> (which performs a similar function for L<Articulate::Authentication>) | ||||||||||||
| 172 | |||||||||||||
| 173 | =back | ||||||||||||
| 174 | |||||||||||||
| 175 | =cut | ||||||||||||
| 176 | |||||||||||||
| 177 | |||||||||||||
| 178 | 11 | 1; | |||||||||||