File Coverage

blib/lib/Articulate/Permission.pm
Criterion Covered Total %
statement 26 34 76.4
branch n/a
condition 2 2 100.0
subroutine 9 10 90.0
pod 3 3 100.0
total 40 49 81.6


line stmt bran cond sub pod time code
1             package Articulate::Permission;
2 12     12   912 use strict;
  12         17  
  12         606  
3 12     12   55 use warnings;
  12         16  
  12         290  
4              
5 12     12   50 use Moo;
  12         14  
  12         66  
6 12     12   3996 use Devel::StackTrace;
  12         6813  
  12         749  
7              
8 12     12   87 use overload bool => sub { shift->granted }, '0+' => sub { shift->granted };
  12     1   28  
  12         106  
  27         1193  
  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     12   2034 use Exporter::Declare;
  12         36218  
  12         74  
27             default_exports qw(permission);
28              
29             sub permission {
30 7   100 7 1 1725 __PACKAGE__->new(
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 and C and populates the stack trace.
52              
53             =cut
54              
55             sub grant {
56 6     6 1 9 my $self = shift;
57 6         7 my $reason = shift;
58              
59             # die if granted or denied are already set?
60 6         18 $self->granted(1);
61 6         16 $self->reason($reason);
62 6         37 $self->stack_trace( Devel::StackTrace->new );
63 6         1087 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 and C and populates the stack trace.
71              
72             =cut
73              
74             sub deny {
75 0     0 1   my $self = shift;
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             default => sub { undef },
97             );
98              
99             =head3 verb
100              
101             The action being performed, e.g. C, C, 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             default => sub { 'error' },
110             );
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             default => sub { [] },
121             );
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 instead.
128              
129             =cut
130              
131             has granted => (
132             is => 'rw',
133             default => sub { 0 },
134             );
135              
136             =head3 denied
137              
138             Whether the permission has been explicitly denied.
139              
140             Please do not explicitly set this. Use C instead.
141              
142             =cut
143              
144             has denied => (
145             is => 'rw',
146             default => sub { 0 }
147             );
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 or C instead.
154              
155             =cut
156              
157             has reason => (
158             is => 'rw',
159             default => sub { '' }
160             );
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 or C instead.
167              
168             =cut
169              
170             has stack_trace => (
171             is => 'rw',
172             default => sub { '' }
173             );
174              
175             =head1 SEE ALSO
176              
177             =over
178              
179             =item * L
180              
181             =item * L (which performs a similar function for L)
182              
183             =back
184              
185             =cut
186              
187             1;