RBAC/DB.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 26 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 107 SCAR 53.5
CC
14
Cov
0%
revoke_roles
20.0
get_roles_by_rights
20.0
get_roles_rights
12.0
CRAP 210.0
1 package QBit::Application::Model::RBAC::DB;
2 $QBit::Application::Model::RBAC::DB::VERSION = '0.002';
3 use qbit;
4  
5 use base qw(QBit::Application::Model::RBAC);
6  
7 __PACKAGE__->model_accessors(db => 'QBit::Application::Model::DB::RBAC');
8  
9 sub get_roles {
10 my ($self, %opts) = @_;
11  
12 return $self->db->roles->get_all($opts{'ids'} ? (filter => {id => $opts{'ids'}}) : ());
13 }
14  
15 sub role_add {
16 my ($self, %role) = @_;
17  
18 throw Exception::Denied unless $self->check_rights('rbac_role_add');
19  
20 return $self->db->roles->add(\%role);
21 }
22  
23 sub set_roles_rights {
24 my ($self, $data) = @_;
25  
26 throw Exception::Denied unless $self->check_rights('rbac_assign_rigth_to_role');
27  
28 $self->db->transaction(
29 sub {
30 $self->db->role_rights->truncate();
31 $self->db->role_rights->add_multi($data);
32 }
33 );
34 }
35  
36 sub set_user_role {
37 my ($self, $user_id, $role_id) = @_;
38  
39 throw Exception::Denied unless $self->check_rights('rbac_user_role_set');
40  
41 $self->db->user_role->replace({user_id => $user_id, role_id => $role_id});
42 }
43  
44 sub get_roles_rights {
45 my ($self, %opts) = @_;
46  
47 $opts{'fields'} = [qw(right role_id)] unless exists($opts{'fields'});
48  
49 my $filter = $self->db->filter();
50 $filter->and({role_id => $opts{'role_id'}}) if exists($opts{'role_id'});
51  
52 return $self->db->role_rights->get_all(fields => $opts{'fields'}, filter => $filter);
53 }
54  
55 sub get_cur_user_roles {
56 my ($self) = @_;
57  
58 my $cur_user = $self->get_option('cur_user');
59 return undef unless defined($cur_user);
60  
61 return $self->get_roles_by_user_id($cur_user->{'id'});
62 }
63  
64 sub get_roles_by_user_id {
65 my ($self, $user_id) = @_;
66  
67 my $roles = $self->db->user_role->get_all(
68 fields => [qw(role_id)],
69 filter => {user_id => $user_id}
70 );
71  
72 $roles = $self->get_roles(ids => [map {$_->{'role_id'}} @$roles]);
73  
74 my %hash_roles = map {$_->{id} => $_} @$roles;
75  
76 return \%hash_roles;
77 }
78  
79 sub revoke_roles {
80 my ($self, $user_id, $roles) = @_;
81  
82 throw Exception::Denied unless $self->check_rights('rbac_revoke_roles');
83  
84 throw Exception::BadArguments(gettext("Expected that 'user_id' is not to be undefined")) unless defined($user_id);
85 throw Exception::BadArguments(gettext("Expected that 'role_id' is not to be undefined")) unless defined($roles);
86  
87 $self->db->user_role->delete({user_id => $user_id, role_id => $roles});
88 }
89  
90 sub get_roles_by_rights {
91 my ($self, $rights) = @_;
92  
93 throw Exception::BadArguments(gettext("Expected that 'rights' is not to be undefined")) unless defined($rights);
94  
95 if (ref($rights) eq '') {
96 $rights = [$rights];
97 } elsif (ref($rights) ne 'ARRAY') {
98 throw Exception::BadArguments(gettext("Expected that 'rights' is a ref to an ARRAY or string"));
99 }
100  
101 my $roles = $self->db->role_rights->get_all(fields => ['role_id'], filter => {right => $rights}, distinct => TRUE);
102  
103 return [map {$_->{'role_id'}} @$roles];
104 }
105  
106 TRUE;
107  
108 __END__