| 1 | package Authorization::AccessControl::Grant 0.04; | ||
| 2 | 7 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 7 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # ABSTRACT: Encapsulation of the parameters of a privilege grant | ||
| 6 | |||
| 7 | 0 | ▶ | use Data::Compare; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use Readonly; | ||
| 9 | use Scalar::Util qw(looks_like_number); | ||
| 10 | |||
| 11 | use experimental qw(signatures); | ||
| 12 | |||
| 13 | use overload | ||
| 14 | '""' => 'to_string'; | ||
| 15 | |||
| 16 | sub new($class, %params) { | ||
| 17 | my $role = delete($params{role}); | ||
| 18 | my $resource = delete($params{resource}); | ||
| 19 | my $action = delete($params{action}); | ||
| 20 | my $restrictions = delete($params{restrictions}); | ||
| 21 | $restrictions = {} unless (defined($restrictions)); | ||
| 22 | |||
| 23 | die("Unsupported params: ", join(', ', keys(%params))) if (keys(%params)); | ||
| 24 | die("Role must be a non-empty string") if (defined($role) && (ref($role) || $role eq '')); | ||
| 25 | die("Resource is required") unless ($resource && !ref($resource)); | ||
| 26 | die("Action is required") unless ($action && !ref($action)); | ||
| 27 | die("Restrictions must be a HashRef") unless (defined($restrictions) && ref($restrictions) eq 'HASH'); | ||
| 28 | |||
| 29 | Readonly::Scalar my $data => { | ||
| 30 | _role => $role, | ||
| 31 | _resource => $resource, | ||
| 32 | _action => $action, | ||
| 33 | _restrictions => $restrictions | ||
| 34 | }; | ||
| 35 | |||
| 36 | bless($data, $class); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub to_string($self, @params) { | ||
| 40 | my $role = $self->{_role} ? '[' . $self->{_role} . '] ' : ''; | ||
| 41 | my $restrictions = ''; | ||
| 42 | foreach (keys($self->{_restrictions}->%*)) { | ||
| 43 | my $v; | ||
| 44 | if ($self->{_restrictions}->{$_}) {$v = $self->{_restrictions}->{$_}} | ||
| 45 | elsif (looks_like_number($self->{_restrictions}->{$_})) {$v = 0} | ||
| 46 | else {$v = 'false'} | ||
| 47 | $restrictions .= "$_=$v,"; | ||
| 48 | } | ||
| 49 | chop($restrictions); | ||
| 50 | $role . $self->{_resource} . ' => ' . $self->{_action} . '(' . $restrictions . ')'; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub role($self) { | ||
| 54 | $self->{_role}; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub resource($self) { | ||
| 58 | $self->{_resource}; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub action($self) { | ||
| 62 | $self->{_action}; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub restrictions($self) { | ||
| 66 | $self->{_restrictions}; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _satisfies_role($self, @roles) { | ||
| 70 | return 1 unless ($self->{_role}); | ||
| 71 | return (grep {$_ eq $self->{_role}} @roles) > 0; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub _satisfies_resource($self, $resource) { | ||
| 75 | return 0 unless (defined($resource)); | ||
| 76 | return $self->{_resource} eq $resource; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _satisfies_action($self, $action) { | ||
| 80 | return 0 unless (defined($action)); | ||
| 81 | return $self->{_action} eq $action; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _satisfies_restrictions($self, $attributes) { | ||
| 85 | my %attrs = $attributes->%*; | ||
| 86 | delete($attrs{$_}) foreach (grep {!exists($self->{_restrictions}->{$_})} keys(%attrs)); | ||
| 87 | my $v = Compare($self->{_restrictions}, \%attrs); | ||
| 88 | return $v; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub is_equal($self, $priv) { | ||
| 92 | return 0 unless (($self->role // '') eq ($priv->role // '')); | ||
| 93 | return 0 unless ($self->resource eq $priv->resource); | ||
| 94 | return 0 unless ($self->action eq $priv->action); | ||
| 95 | return 0 unless (Compare($self->restrictions, $priv->restrictions)); | ||
| 96 | return 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub accepts($self, %params) { | ||
| 100 | my ($roles, $resource, $action, $attributes) = @params{qw(roles resource action attributes)}; | ||
| 101 | |||
| 102 | return 0 unless ($self->_satisfies_resource($resource)); | ||
| 103 | return 0 unless ($self->_satisfies_action($action)); | ||
| 104 | return 0 unless ($self->_satisfies_role(($roles // [])->@*)); | ||
| 105 | return 0 unless ($self->_satisfies_restrictions($attributes // {})); | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | |||
| 109 | =head1 NAME | ||
| 110 | |||
| 111 | Authorization::AccessControl::Grant - Encapsulation of the parameters of a privilege grant | ||
| 112 | |||
| 113 | =head1 SYNOPSIS | ||
| 114 | |||
| 115 | use Authorization::AccessControl::Grant; | ||
| 116 | |||
| 117 | my $grant = Authorization::AccessControl::Grant->new( | ||
| 118 | resource => 'Book', | ||
| 119 | action => 'read', | ||
| 120 | ); | ||
| 121 | |||
| 122 | $grant->accepts(resource => 'Book', action => 'read'); | ||
| 123 | |||
| 124 | =head1 DESCRIPTION | ||
| 125 | |||
| 126 | This is a simple class to encapsulate the properties of a privilege grant: | ||
| 127 | resource, action, roles, and restrictions, with the latter two optional. Methods | ||
| 128 | are available for checking all properties at once (L</accepts>) and determining | ||
| 129 | if another grant is exactly equal (used for duplicate detection) (L<is_equal>). | ||
| 130 | |||
| 131 | Grant instances are immutable: none of their properties may be altered after | ||
| 132 | object creation. | ||
| 133 | |||
| 134 | =head1 METHODS | ||
| 135 | |||
| 136 | =head2 new | ||
| 137 | |||
| 138 | Authorization::AccessControl::Grant->new( %params ) | ||
| 139 | |||
| 140 | Creates a new privilege grant instance. Normally, you should use | ||
| 141 | L<Authorization::AccessControl::ACL/grant> rather than this constructor | ||
| 142 | directly, to create and "register" instances. C<resource>, C<action>, C<role>, | ||
| 143 | and C<restrictions> keys are respected in C<%params> | ||
| 144 | |||
| 145 | =head2 role | ||
| 146 | |||
| 147 | Accessor for the C<role> property | ||
| 148 | |||
| 149 | =head2 resource | ||
| 150 | |||
| 151 | Accessor for the C<resource> property | ||
| 152 | |||
| 153 | =head2 action | ||
| 154 | |||
| 155 | Accessor for the C<action> property | ||
| 156 | |||
| 157 | =head2 restrictions | ||
| 158 | |||
| 159 | Accessor for the C<restrictions> property | ||
| 160 | |||
| 161 | =head2 is_equal | ||
| 162 | |||
| 163 | $grant1->is_equal($grant2) | ||
| 164 | |||
| 165 | Returns true if all properties of both grants are exactly the same, false | ||
| 166 | otherwise | ||
| 167 | |||
| 168 | =head2 accepts | ||
| 169 | |||
| 170 | $grant->accepts( %params ) | ||
| 171 | |||
| 172 | Returns true if the parameters meet all of the requirements of the grant, false | ||
| 173 | otherwise. Specifically, this means that C<resource> and C<action> must match | ||
| 174 | exactly, the grant's C<role> (if set) must be contained within the C<roles> | ||
| 175 | ArrayRef, and every item in the grant's C<restrictions> must be matched by a | ||
| 176 | corresponding entry with the same value in the C<attributes> HashRef | ||
| 177 | |||
| 178 | =head1 AUTHOR | ||
| 179 | |||
| 180 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 181 | |||
| 182 | =head1 LICENSE | ||
| 183 | |||
| 184 | Copyright (c) 2024 Mark Tyrrell | ||
| 185 | |||
| 186 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 187 | of this software and associated documentation files (the "Software"), to deal | ||
| 188 | in the Software without restriction, including without limitation the rights | ||
| 189 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 190 | copies of the Software, and to permit persons to whom the Software is | ||
| 191 | furnished to do so, subject to the following conditions: | ||
| 192 | |||
| 193 | The above copyright notice and this permission notice shall be included in all | ||
| 194 | copies or substantial portions of the Software. | ||
| 195 | |||
| 196 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 197 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 198 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 199 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 200 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 201 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 202 | SOFTWARE. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | 1; | ||
| 207 | |||
| 208 | __END__ | ||