File Coverage

blib/lib/Authorization/RBAC/Backend/DBIx.pm
Criterion Covered Total %
statement 34 35 97.1
branch 10 14 71.4
condition 4 5 80.0
subroutine 5 5 100.0
pod 2 2 100.0
total 55 61 90.1


line stmt bran cond sub pod time code
1             package Authorization::RBAC::Backend::DBIx;
2             $Authorization::RBAC::Backend::DBIx::VERSION = '0.10';
3 2     2   3879 use Moose::Role;
  2         282  
  2         15  
4 2     2   6979 use Carp qw/croak/;
  2         3  
  2         106  
5              
6 2     2   818 use FindBin '$Bin';
  2         1379  
  2         884  
7             require UNIVERSAL::require;
8              
9              
10             has typeobjs => (
11             is => 'rw',
12             default => sub{ return shift->schema->resultset('Typeobj')->search; }
13             );
14              
15             has permissions => (
16             is => 'rw',
17             default => sub{ return shift->schema->resultset('Permission')->search; }
18             );
19              
20              
21             sub get_operations{
22 43     43 1 50 my ($self, $operations) = @_;
23              
24 43         38 my @ops;
25 43         70 foreach my $op ( @$operations ) {
26 3         92 my $op_rs = $self->schema->resultset('Operation')->search({ name => $op})->single;
27 3 50       3938 $self->_log("'$op' operation was not found in the database !!!")
28             if ! $op_rs;
29 3 50       93 push( @ops, $op_rs ) if $op_rs;
30             }
31 43         90 return @ops;
32             }
33              
34             sub get_permission{
35 64     64 1 151 my ($self, $role, $op, $obj) = @_;
36              
37 64         83 my $typeobj = ref($obj);
38 64         189 $typeobj =~ s/.*:://;
39 64         1524 my $typeobj_rs = $self->schema->resultset('Typeobj')->search({ name => $typeobj})->single;
40 64 50       82331 if ( ! $typeobj_rs ) {
41 0         0 croak "'$typeobj' is unknown in the TypeObj table !";
42             }
43              
44 64         3673 my $permission = $self->schema->resultset('Permission')->search({ role_id => $role->id,
45             typeobj_id => $typeobj_rs->id,
46             obj_id => $obj->id,
47             operation_id => $op->id
48             })->single;
49              
50 64   100     111285 my $parent_field = $self->config->{typeobj}->{$typeobj}->{parent_field} || 'parent';
51              
52 64 100       201 if ( $permission ) {
    100          
53 46         764 return ($permission->value, $permission->inheritable);
54             }
55             # Search permission on parents
56             elsif ( $obj->can( $parent_field) ) {
57              
58 17 100       245 if ( $obj->$parent_field ){
59              
60 13         259 my $typeobj_parent = ref($obj->$parent_field);
61 13         88 $typeobj_parent =~ s/.*:://;
62 13         177 $self->_log(" [??] Search inherited permissions on parents ${typeobj_parent}_" . $obj->$parent_field->id . "...");
63 13         174 my ( $result, $inheritable) = $self->get_permission($role, $op, $obj->$parent_field);
64 13 50 66     2973 if ( $inheritable || ! $result ) {
65 13         28 return ($result, $inheritable);
66             }
67             }
68             }
69             # No permission and no parent =>
70             else {
71 1         4 $self->_log(" No permission found :(");
72 1         2 return 0;
73             }
74             }
75              
76              
77             =head1 NAME
78              
79             Authorization::RBAC::Backend::DBIx - Backend 'DBIx' for Authorization::RBAC
80              
81             =head1 VERSION
82              
83             version 0.10
84              
85             =head1 CONFIGURATION
86              
87             use Catalyst qw/
88             Authorization::Roles
89             Authorization::RBAC
90             /;
91              
92             # in your config
93             Authorization::RBAC:
94             debug: 0
95             backend:
96             name: DBIx
97             model: Model::RBAC
98              
99             =head2 REQUIRED SCHEMA
100              
101             See t/lib/Schema/RBAC/Result/
102              
103             User -> UserRole -> Role
104              
105             Role -> Permission -> Object ( -> TypeObj )
106             -> Operation
107              
108             =head1 PROVIDED METHODS
109              
110             =head2 get_operations( $operations )
111              
112             =head2 get_permission( $role, $op, $obj )
113              
114             =head1 AUTHOR
115              
116             Daniel Brosseau, C<< <dab at catapulse.org> >>
117              
118             =head1 LICENSE AND COPYRIGHT
119              
120             Copyright 2011 Daniel Brosseau.
121              
122             This program is free software; you can redistribute it and/or modify it
123             under the terms of either: the GNU General Public License as published
124             by the Free Software Foundation; or the Artistic License.
125              
126             See http://dev.perl.org/licenses/ for more information.
127              
128              
129             =cut
130              
131             1;