| 1 | package QBit::Application::Model::RBAC::DB; | ||
| 2 | $QBit::Application::Model::RBAC::DB::VERSION = '0.003'; | ||
| 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->cur_user(); | ||
| 59 | |||
| 60 | return %$cur_user ? $self->get_roles_by_user_id($cur_user->{'id'}) : {}; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub get_roles_by_user_id { | ||
| 64 | my ($self, $user_id) = @_; | ||
| 65 | |||
| 66 | my $roles = $self->db->user_role->get_all( | ||
| 67 | fields => [qw(role_id)], | ||
| 68 | filter => {user_id => $user_id} | ||
| 69 | ); | ||
| 70 | |||
| 71 | $roles = $self->get_roles(ids => [map {$_->{'role_id'}} @$roles]); | ||
| 72 | |||
| 73 | my %hash_roles = map {$_->{id} => $_} @$roles; | ||
| 74 | |||
| 75 | return \%hash_roles; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub revoke_roles { | ||
| 79 | my ($self, $user_id, $roles) = @_; | ||
| 80 | |||
| 81 | throw Exception::Denied unless $self->check_rights('rbac_revoke_roles'); | ||
| 82 | |||
| 83 | throw Exception::BadArguments(gettext("Expected that 'user_id' is not to be undefined")) unless defined($user_id); | ||
| 84 | throw Exception::BadArguments(gettext("Expected that 'role_id' is not to be undefined")) unless defined($roles); | ||
| 85 | |||
| 86 | $self->db->user_role->delete({user_id => $user_id, role_id => $roles}); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub get_roles_by_rights { | ||
| 90 | my ($self, $rights) = @_; | ||
| 91 | |||
| 92 | throw Exception::BadArguments(gettext("Expected that 'rights' is not to be undefined")) unless defined($rights); | ||
| 93 | |||
| 94 | if (ref($rights) eq '') { | ||
| 95 | $rights = [$rights]; | ||
| 96 | } elsif (ref($rights) ne 'ARRAY') { | ||
| 97 | throw Exception::BadArguments(gettext("Expected that 'rights' is a ref to an ARRAY or string")); | ||
| 98 | } | ||
| 99 | |||
| 100 | my $roles = $self->db->role_rights->get_all(fields => ['role_id'], filter => {right => $rights}, distinct => TRUE); | ||
| 101 | |||
| 102 | return [map {$_->{'role_id'}} @$roles]; | ||
| 103 | } | ||
| 104 | |||
| 105 | TRUE; | ||
| 106 | |||
| 107 | __END__ |