| 1 | package OpusVL::AppKit::RolesFor::Schema::AppKitAuthDB::Result::User; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | sub setup_authdb | ||||||||||||||||||||||||||||
| 8 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
setup_authdb: called CC=1 SCAR=0.6
Pod
documented
| |||||||||||||||||||||||||||||
| 9 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | $class->load_components("EncodedColumn"); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # Alter the password column to enable encoded password.. | ||||||||||||||||||||||||||||
| 14 | 1 | $class->add_columns | |||||||||||||||||||||||||||
| 15 | ( | ||||||||||||||||||||||||||||
| 16 | "+password", | ||||||||||||||||||||||||||||
| 17 | { | ||||||||||||||||||||||||||||
| 18 | encode_column => 1, | ||||||||||||||||||||||||||||
| 19 | encode_class => 'Crypt::Eksblowfish::Bcrypt', | ||||||||||||||||||||||||||||
| 20 | encode_args => { key_nul => 0, cost => 8 }, | ||||||||||||||||||||||||||||
| 21 | encode_check_method => '_local_check_password', | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | $class->many_to_many( roles => 'users_roles', 'role' ); | |||||||||||||||||||||||||||
| 26 | 0 | $class->many_to_many( parameters => 'users_parameters', 'parameter' ); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sub check_password | ||||||||||||||||||||||||||||
| 31 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
check_password: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | # FIXME: is our database inconsistent? | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | return 0 unless $self->status eq 'active' || $self->status eq 'enabled'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->status eq "active" or $self->status eq "enabled"
MC/DC0%0/2
$self->status eq "active" or $self->status eq "enabled"
$self->status eq "active"
$self->status eq "enabled"
Truth table0%0/3
A or B
A= $self->status eq "active"B= $self->status eq "enabled"
| |||||||||||||||||||||||||||||
| 35 | 0 | my $schema = $self->result_source->schema; | |||||||||||||||||||||||||||
| 36 | # see if the schema has been given a method for | ||||||||||||||||||||||||||||
| 37 | # checking the password | ||||||||||||||||||||||||||||
| 38 | 0 | my $result; | |||||||||||||||||||||||||||
| 39 | 0 | ▶ | if($schema->can('password_check') && $schema->password_check) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$schema->can("password_check") and $schema->password_check
MC/DC0%0/2
$schema->can("password_check") and $schema->password_check
$schema->can("password_check")
$schema->password_check
Truth table0%0/3
A and B
A= $schema->can("password_check")B= $schema->password_check
| |||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||
| 41 | # look up ldap password. | ||||||||||||||||||||||||||||
| 42 | 0 | $result = $schema->password_check->check_password($self->username, @_); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else | ||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 0 | $result = $self->_local_check_password(@_); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 0 | ▶ | if($result) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | { | ||||||||||||||||||||||||||||
| 50 | 0 | $self->successful_login; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else | ||||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 0 | $self->failed_login; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | return $result; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | sub failed_login | ||||||||||||||||||||||||||||
| 60 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
failed_login: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 0 | $self->update({ last_failed_login => DateTime->now() }); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | sub successful_login | ||||||||||||||||||||||||||||
| 66 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
successful_login: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 67 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | 0 | $self->update({ last_login => DateTime->now() }); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub getdata | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getdata: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 0 | my ($key) = @_; | |||||||||||||||||||||||||||
| 77 | 0 | my $data = $self->find_related( 'users_data', { key => $key } ); | |||||||||||||||||||||||||||
| 78 | 0 | ▶ | return undef unless $data; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | return $data->value; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub setdata | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
setdata: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 86 | 0 | my ($key, $value) = @_; | |||||||||||||||||||||||||||
| 87 | 0 | my $data = $self->find_or_create_related( 'users_data', { key => $key } ); | |||||||||||||||||||||||||||
| 88 | 0 | $data->update( { value => $value } ); | |||||||||||||||||||||||||||
| 89 | 0 | return 1; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub disable | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
disable: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | ▶ | if ( $self->status ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 0 | $self->update( { status => 'disabled' } ); | |||||||||||||||||||||||||||
| 100 | 0 | return 1; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | return 0; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | sub enable | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
enable: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 108 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | if ( $self->status ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 0 | $self->update( { status => 'enabled' } ); | |||||||||||||||||||||||||||
| 113 | 0 | return 1; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | return 0; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub params_hash | ||||||||||||||||||||||||||||
| 120 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
params_hash: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my %hash; | |||||||||||||||||||||||||||
| 124 | 0 | foreach my $rp ( $self->users_parameters ) | |||||||||||||||||||||||||||
| 125 | { | ||||||||||||||||||||||||||||
| 126 | 0 | ▶ | next unless defined $rp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | next unless defined $rp->parameter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $hash{ $rp->parameter->parameter } = $rp->value; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | return \%hash; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub set_param_by_name | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
set_param_by_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 138 | 0 | my ( $param_name, $param_value ) = @_; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | # find the param.. | ||||||||||||||||||||||||||||
| 141 | 0 | my $param = $self->result_source->schema->resultset('Parameter')->find( { parameter => $param_name } ); | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # return undef, if we could find the param.. | ||||||||||||||||||||||||||||
| 144 | 0 | ▶ | return undef unless $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | # add to users parameter... | ||||||||||||||||||||||||||||
| 147 | 0 | $self->update_or_create_related( 'users_parameters', { value => $param_value, parameter_id => $param->id } ); | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | return 1; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | sub get_parameter_value_by_name | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_parameter_value_by_name: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 155 | 0 | my $val = $self->get_parameter_by_name(@_); | |||||||||||||||||||||||||||
| 156 | 0 | ▶ | return unless $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 0 | return $val->value; | |||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub get_parameter_by_name | ||||||||||||||||||||||||||||
| 161 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_parameter_by_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 162 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 163 | 0 | my $name = shift; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | my $param = $self->users_parameters->find({ 'parameter.parameter' => $name }, { join => [ 'parameter' ] }); | |||||||||||||||||||||||||||
| 166 | 0 | return $param; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub delete_param_by_name | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
delete_param_by_name: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 173 | 0 | my ( $param_name ) = @_; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # FIXME: this code blows, use get_parameter_by_name instead. | ||||||||||||||||||||||||||||
| 176 | # find the param.. | ||||||||||||||||||||||||||||
| 177 | 0 | my $param = $self->result_source->schema->resultset('Parameter')->find( { parameter => $param_name } ); | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # return undef, if we could find the param.. | ||||||||||||||||||||||||||||
| 180 | 0 | ▶ | return undef unless $param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # delete to users parameter... | ||||||||||||||||||||||||||||
| 183 | 0 | $self->delete_related( 'users_parameters', { parameter_id => $param->id } ); | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 0 | return 1; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub roles_modifiable | ||||||||||||||||||||||||||||
| 190 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
roles_modifiable: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 192 | 0 | my $schema = $self->result_source->schema; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # check to see if any of the current roles allow access to all | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | if (grep { $_ } map { $_->can_change_any_role } $self->roles->all) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | { | ||||||||||||||||||||||||||||
| 197 | 0 | return $schema->resultset('Role'); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | 0 | my $allowed_roles = $self->roles->search_related('roles_allowed_roles'); | |||||||||||||||||||||||||||
| 200 | 0 | ▶ | if($allowed_roles->count == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | { | ||||||||||||||||||||||||||||
| 202 | # check to see if any allowed roles are setup | ||||||||||||||||||||||||||||
| 203 | # if not return all roles. | ||||||||||||||||||||||||||||
| 204 | 0 | ▶ | if($schema->resultset('RoleAllowed')->count == 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$schema->resultset("RoleAllowed")->count == 0 and $schema->resultset("RoleAdmin")->count == 0
MC/DC0%0/2
$schema->resultset("RoleAllowed")->count == 0 and $schema->resultset("RoleAdmin")->count == 0
$schema->resultset("RoleAllowed")->count == 0
$schema->resultset("RoleAdmin")->count == 0
Truth table0%0/3
A and B
A= $schema->resultset("RoleAllowed")->count == 0B= $schema->resultset("RoleAdmin")->count == 0
| |||||||||||||||||||||||||||||
| 205 | && $schema->resultset('RoleAdmin')->count == 0) | ||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||
| 207 | 0 | return $schema->resultset('Role'); | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | 0 | my $roles = $schema->resultset('Role')->search({ id => { in => $allowed_roles->get_column('role_allowed')->as_query }}); | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 0 | return $roles; | |||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | sub can_modify_user | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
can_modify_user: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 218 | 0 | my ($self, $username) = @_; | |||||||||||||||||||||||||||
| 219 | 0 | my $schema = $self->result_source->schema; | |||||||||||||||||||||||||||
| 220 | 0 | my $other_user = $schema->resultset('User')->find({ username => $username}); | |||||||||||||||||||||||||||
| 221 | 0 | ▶ | die "Unable to find user '$username'" unless $other_user; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | 0 | my @roles = $other_user->roles->all; | |||||||||||||||||||||||||||
| 223 | 0 | my @allowed = $self->roles_modifiable->all; | |||||||||||||||||||||||||||
| 224 | 0 | my %allowed_hash = map { $_->role => 1 } @allowed; | |||||||||||||||||||||||||||
| 225 | 0 | for my $role (@roles) | |||||||||||||||||||||||||||
| 226 | { | ||||||||||||||||||||||||||||
| 227 | 0 | ▶ | return 0 unless $allowed_hash{$role->role}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | 0 | return 1; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 1 | 1; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | __END__ | ||||||||||||||||||||||||||||