| 1 | package Crop::Rights; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | Crop::Rights - Role-based access and rights management for Crop framework | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use Crop::Rights; | ||
| 12 | # ...usage... | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | Crop::Rights provides role-based access and rights management for the Crop framework. | ||
| 17 | |||
| 18 | =head1 AUTHORS | ||
| 19 | |||
| 20 | Euvgenio (Core Developer) | ||
| 21 | |||
| 22 | Alex (Contributor) | ||
| 23 | |||
| 24 | =head1 COPYRIGHT AND LICENSE | ||
| 25 | |||
| 26 | Apache 2.0 | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | use base qw/ Crop::Object /; | ||
| 31 | |||
| 32 | =begin nd | ||
| 33 | Class: Crop::Rights | ||
| 34 | Interface to the Rights-system. | ||
| 35 | |||
| 36 | Calculates actual privileges at the last moment when they used. | ||
| 37 | =cut | ||
| 38 | |||
| 39 | use v5.14; | ||
| 40 | use warnings; | ||
| 41 | |||
| 42 | use Crop::Rights::Role; | ||
| 43 | |||
| 44 | use Crop::Debug; | ||
| 45 | |||
| 46 | =begin nd | ||
| 47 | Variable: our %Attributes | ||
| 48 | Class attributes: | ||
| 49 | |||
| 50 | priv - hash of privileges; {Crop}{READ} = <sign>; where <sign>=-1 negative role, <sign>=1 positive; <sign> is NOT implemented | ||
| 51 | roles_todo - array of delegated roles; only names | ||
| 52 | role - hash of all the roles <Crop::Rights::Role> by name | ||
| 53 | =cut | ||
| 54 | our %Attributes = ( | ||
| 55 | priv => {default => {}}, | ||
| 56 | roles_todo => {default => []}, | ||
| 57 | role => {default => {}}, | ||
| 58 | ); | ||
| 59 | |||
| 60 | =begin nd | ||
| 61 | Method: add_role (@roles) | ||
| 62 | Add roles. | ||
| 63 | |||
| 64 | Parameters: | ||
| 65 | @roles - names to add | ||
| 66 | =cut | ||
| 67 | sub add_role { | ||
| 68 | my ($self, @roles) = @_; | ||
| 69 | |||
| 70 | for (@roles) { | ||
| 71 | next if exists $self->{role}{$_}; | ||
| 72 | |||
| 73 | push @{$self->{roles_todo}}, $_; | ||
| 74 | $self->{role}{$_} = undef; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | =begin nd | ||
| 79 | Method: is_ok (%priv) | ||
| 80 | Check required privileges to actual rights. | ||
| 81 | |||
| 82 | Parameters: | ||
| 83 | %priv - required privileges | ||
| 84 | |||
| 85 | Returns: | ||
| 86 | true - if granted rights satisfy required privileges | ||
| 87 | false - otherwise | ||
| 88 | =cut | ||
| 89 | sub ok { | ||
| 90 | my ($self, %priv) = @_; | ||
| 91 | |||
| 92 | # debug 'RIGHTS_OK_SELF=', $self; | ||
| 93 | |||
| 94 | if (@{$self->{roles_todo}}) { | ||
| 95 | my $role = Crop::Rights::Role->All( | ||
| 96 | name => $self->{roles_todo}, | ||
| 97 | active => 1, | ||
| 98 | EXT => ['privileges' => [qw/ realm perm /]] | ||
| 99 | ); | ||
| 100 | |||
| 101 | # debug 'ROLE=', $role; | ||
| 102 | |||
| 103 | for ($role->List) { | ||
| 104 | for ($_->privileges->List) { | ||
| 105 | $self->{priv}{$_->realm->name}{$_->perm->code} = 1 unless exists $self->{priv}{$_->realm->name}{$_->perm->code}; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | $self->{roles_todo} = []; | ||
| 110 | } | ||
| 111 | # debug 'RIGHTS_OK_SELF=', $self; | ||
| 112 | |||
| 113 | while (my ($perm, $realm) = each %priv) { | ||
| 114 | # debug "realm=$realm; perm=$perm"; | ||
| 115 | return unless exists $self->{priv}{$realm}{$perm}; | ||
| 116 | } | ||
| 117 | |||
| 118 | 1; | ||
| 119 | } | ||
| 120 | |||
| 121 | 1; |