| 1 | package Articulate::Authorisation::Preconfigured; | ||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Articulate::Syntax qw( new_location_specification ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Articulate::Authorisation::Preconfigured - allow access to users in | ||||||||||
| 11 | your config | ||||||||||
| 12 | |||||||||||
| 13 | =cut | ||||||||||
| 14 | |||||||||||
| 15 | =head1 CONFIGURATION | ||||||||||
| 16 | |||||||||||
| 17 | Put this in your config: | ||||||||||
| 18 | |||||||||||
| 19 | components: | ||||||||||
| 20 | authorisation: | ||||||||||
| 21 | Articulate::Authorisation: | ||||||||||
| 22 | rules: | ||||||||||
| 23 | - class: Articulate::Authorisation::Preconfigured | ||||||||||
| 24 | rules: | ||||||||||
| 25 | zone/public: | ||||||||||
| 26 | "[guest]": | ||||||||||
| 27 | read: 1 | ||||||||||
| 28 | admin: 1 | ||||||||||
| 29 | |||||||||||
| 30 | =head1 ATTRIBUTES | ||||||||||
| 31 | |||||||||||
| 32 | =head3 rules | ||||||||||
| 33 | |||||||||||
| 34 | The rules used to determine whether or not requests are authorised. | ||||||||||
| 35 | Defaults to C<{}>. | ||||||||||
| 36 | |||||||||||
| 37 | =cut | ||||||||||
| 38 | |||||||||||
| 39 | has rules => ( | ||||||||||
| 40 | is => 'rw', | ||||||||||
| 41 | 0 | ▶ | default => sub { {} } | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 42 | 1 | ); | |||||||||
| 43 | |||||||||||
| 44 | =head1 METHODS | ||||||||||
| 45 | |||||||||||
| 46 | =head3 new | ||||||||||
| 47 | |||||||||||
| 48 | No surprises here. | ||||||||||
| 49 | |||||||||||
| 50 | =head3 permitted | ||||||||||
| 51 | |||||||||||
| 52 | Goes through each of the locations in 'rules' (in ascending order of | ||||||||||
| 53 | length) and if the location in the permission request begins with that | ||||||||||
| 54 | rule, then look at the contents. | ||||||||||
| 55 | |||||||||||
| 56 | We then expect a hash of user ids, or C<[guest]> for users not logged | ||||||||||
| 57 | in. Their values should be 0 (for deny), 1 (for grant), or a hash of | ||||||||||
| 58 | verbs to grant/deny. | ||||||||||
| 59 | |||||||||||
| 60 | This is preconfigured access, so fine for a small personal or static | ||||||||||
| 61 | site, but if you have open sign-up or changing requirements then you | ||||||||||
| 62 | will probably find changing the config file and reloading the app gets | ||||||||||
| 63 | tedious after a while. | ||||||||||
| 64 | |||||||||||
| 65 | =cut | ||||||||||
| 66 | |||||||||||
| 67 | 0 | ▶ | sub permitted { | ||||||||
|
Subroutine
permitted: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||
| 68 | 0 | my $self = shift; | |||||||||
| 69 | 0 | my $permission = shift; | |||||||||
| 70 | 0 | my $user_id = $permission->user_id; | |||||||||
| 71 | 0 | my $location = $permission->location; | |||||||||
| 72 | 0 | my $verb = $permission->verb; | |||||||||
| 73 | 0 | my $rules = $self->rules; | |||||||||
| 74 | 0 | my $access = undef; | |||||||||
| 75 | |||||||||||
| 76 | 0 | foreach my $rule_location ( sort { $#$a <=> $#$b } | |||||||||
| 77 | 0 | map { new_location_specification $_ } keys %$rules ) | |||||||||
| 78 | { | ||||||||||
| 79 | 0 | ▶ | if ( $rule_location->matches_ancestor_of($location) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 0 | ▶ | if ( grep { $_ eq $user_id } keys %{ $rules->{$rule_location} } ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 81 | 0 | ▶ | if ( ref $rules->{$rule_location}->{$user_id} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 0 | ▶ | if ( exists $rules->{$rule_location}->{$user_id}->{$verb} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | 0 | my $value = !!$rules->{$rule_location}->{$user_id}->{$verb}; | |||||||||
| 84 | 0 | ▶ | return $permission->deny("User cannot $verb $rule_location") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | unless $value; | ||||||||||
| 86 | 0 | $access = "User can $verb $rule_location"; | |||||||||
| 87 | } | ||||||||||
| 88 | } | ||||||||||
| 89 | else { | ||||||||||
| 90 | 0 | my $value = !!$rules->{$rule_location}->{$user_id}; | |||||||||
| 91 | 0 | ▶ | return $permission->deny("User cannot access $rule_location at all") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | unless $value; | ||||||||||
| 93 | 0 | $access = "User can access $rule_location"; | |||||||||
| 94 | } | ||||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | } | ||||||||||
| 98 | 0 | ▶ | if ( defined $access ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | 0 | return $permission->grant($access); | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 0 | return $permission; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 1 | 1; | |||||||||