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