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 11     11   963 use strict;
  11         17  
  11         421  
3 11     11   52 use warnings;
  11         15  
  11         287  
4              
5 11     11   50 use Moo;
  11         17  
  11         68  
6 11     11   4281 use Devel::StackTrace;
  11         9390  
  11         739  
7              
8 11     11   78 use overload bool => sub { shift->granted }, '0+' => sub { shift->granted };
  11     27   26  
  11         108  
  27         1330  
  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     11   2455 use Exporter::Declare;
  11         58854  
  11         57  
27             default_exports qw(permission);
28              
29              
30             sub permission {
31 7   100 7 1 2956 __PACKAGE__->new( {
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 and C and populates the stack trace.
51              
52             =cut
53              
54             sub grant {
55 6     6 1 11 my $self = shift;
56 6         12 my $reason = shift;
57             # die if granted or denied are already set?
58 6         30 $self->granted(1);
59 6         21 $self->reason($reason);
60 6         115 $self->stack_trace(Devel::StackTrace->new);
61 6         1605 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 and C and populates the stack trace.
69              
70             =cut
71              
72             sub deny {
73 0     0 1   my $self = shift;
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             default => sub { undef };
94              
95             =head3 verb
96              
97             The action being performed, e.g. C, C, 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             default => sub { 'error' };
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             default => sub { [] };
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 instead.
122              
123             =cut
124              
125             has granted =>
126             is => 'rw',
127             default => sub { 0 };
128              
129             =head3 denied
130              
131             Whether the permission has been explicitly denied.
132              
133             Please do not explicitly set this. Use C instead.
134              
135             =cut
136              
137             has denied =>
138             is => 'rw',
139             default => sub { 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 or C instead.
146              
147             =cut
148              
149             has reason =>
150             is => 'rw',
151             default => sub { '' };
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 or C instead.
158              
159             =cut
160              
161             has stack_trace =>
162             is => 'rw',
163             default => sub { '' };
164              
165             =head1 SEE ALSO
166              
167             =over
168              
169             =item * L
170              
171             =item * L (which performs a similar function for L)
172              
173             =back
174              
175             =cut
176              
177              
178             1;