| 1 | package OpusVL::AppKit::RolesFor::Schema::AppKitAuthDB::Result::User; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Moose::Role; | ||
| 5 | |||
| 6 | |||
| 7 | sub setup_authdb | ||
| 8 | { | ||
| 9 | my $class = shift; | ||
| 10 | |||
| 11 | $class->load_components("EncodedColumn"); | ||
| 12 | |||
| 13 | # Alter the password column to enable encoded password.. | ||
| 14 | $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 | $class->many_to_many( roles => 'users_roles', 'role' ); | ||
| 26 | $class->many_to_many( parameters => 'users_parameters', 'parameter' ); | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | sub check_password | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | # FIXME: is our database inconsistent? | ||
| 34 | return 0 unless $self->status eq 'active' || $self->status eq 'enabled'; | ||
| 35 | my $schema = $self->result_source->schema; | ||
| 36 | # see if the schema has been given a method for | ||
| 37 | # checking the password | ||
| 38 | my $result; | ||
| 39 | if($schema->can('password_check') && $schema->password_check) | ||
| 40 | { | ||
| 41 | # look up ldap password. | ||
| 42 | $result = $schema->password_check->check_password($self->username, @_); | ||
| 43 | } | ||
| 44 | else | ||
| 45 | { | ||
| 46 | $result = $self->_local_check_password(@_); | ||
| 47 | } | ||
| 48 | if($result) | ||
| 49 | { | ||
| 50 | $self->successful_login; | ||
| 51 | } | ||
| 52 | else | ||
| 53 | { | ||
| 54 | $self->failed_login; | ||
| 55 | } | ||
| 56 | return $result; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub failed_login | ||
| 60 | { | ||
| 61 | my $self = shift; | ||
| 62 | $self->update({ last_failed_login => DateTime->now() }); | ||
| 63 | } | ||
| 64 | |||
| 65 | sub successful_login | ||
| 66 | { | ||
| 67 | my $self = shift; | ||
| 68 | $self->update({ last_login => DateTime->now() }); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | |||
| 73 | sub getdata | ||
| 74 | { | ||
| 75 | my $self = shift; | ||
| 76 | my ($key) = @_; | ||
| 77 | my $data = $self->find_related( 'users_data', { key => $key } ); | ||
| 78 | return undef unless $data; | ||
| 79 | return $data->value; | ||
| 80 | } | ||
| 81 | |||
| 82 | |||
| 83 | sub setdata | ||
| 84 | { | ||
| 85 | my $self = shift; | ||
| 86 | my ($key, $value) = @_; | ||
| 87 | my $data = $self->find_or_create_related( 'users_data', { key => $key } ); | ||
| 88 | $data->update( { value => $value } ); | ||
| 89 | return 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | sub disable | ||
| 94 | { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | if ( $self->status ) | ||
| 98 | { | ||
| 99 | $self->update( { status => 'disabled' } ); | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | sub enable | ||
| 107 | { | ||
| 108 | my $self = shift; | ||
| 109 | |||
| 110 | if ( $self->status ) | ||
| 111 | { | ||
| 112 | $self->update( { status => 'enabled' } ); | ||
| 113 | return 1; | ||
| 114 | } | ||
| 115 | return 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub params_hash | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | my %hash; | ||
| 124 | foreach my $rp ( $self->users_parameters ) | ||
| 125 | { | ||
| 126 | next unless defined $rp; | ||
| 127 | next unless defined $rp->parameter; | ||
| 128 | $hash{ $rp->parameter->parameter } = $rp->value; | ||
| 129 | } | ||
| 130 | |||
| 131 | return \%hash; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub set_param_by_name | ||
| 136 | { | ||
| 137 | my $self = shift; | ||
| 138 | my ( $param_name, $param_value ) = @_; | ||
| 139 | |||
| 140 | # find the param.. | ||
| 141 | my $param = $self->result_source->schema->resultset('Parameter')->find( { parameter => $param_name } ); | ||
| 142 | |||
| 143 | # return undef, if we could find the param.. | ||
| 144 | return undef unless $param; | ||
| 145 | |||
| 146 | # add to users parameter... | ||
| 147 | $self->update_or_create_related( 'users_parameters', { value => $param_value, parameter_id => $param->id } ); | ||
| 148 | |||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub get_parameter_value_by_name | ||
| 153 | { | ||
| 154 | my $self = shift; | ||
| 155 | my $val = $self->get_parameter_by_name(@_); | ||
| 156 | return unless $val; | ||
| 157 | return $val->value; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub get_parameter_by_name | ||
| 161 | { | ||
| 162 | my $self = shift; | ||
| 163 | my $name = shift; | ||
| 164 | |||
| 165 | my $param = $self->users_parameters->find({ 'parameter.parameter' => $name }, { join => [ 'parameter' ] }); | ||
| 166 | return $param; | ||
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | sub delete_param_by_name | ||
| 171 | { | ||
| 172 | my $self = shift; | ||
| 173 | my ( $param_name ) = @_; | ||
| 174 | |||
| 175 | # FIXME: this code blows, use get_parameter_by_name instead. | ||
| 176 | # find the param.. | ||
| 177 | my $param = $self->result_source->schema->resultset('Parameter')->find( { parameter => $param_name } ); | ||
| 178 | |||
| 179 | # return undef, if we could find the param.. | ||
| 180 | return undef unless $param; | ||
| 181 | |||
| 182 | # delete to users parameter... | ||
| 183 | $self->delete_related( 'users_parameters', { parameter_id => $param->id } ); | ||
| 184 | |||
| 185 | return 1; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub roles_modifiable | ||
| 190 | { | ||
| 191 | my $self = shift; | ||
| 192 | my $schema = $self->result_source->schema; | ||
| 193 | |||
| 194 | # check to see if any of the current roles allow access to all | ||
| 195 | if (grep { $_ } map { $_->can_change_any_role } $self->roles->all) | ||
| 196 | { | ||
| 197 | return $schema->resultset('Role'); | ||
| 198 | } | ||
| 199 | my $allowed_roles = $self->roles->search_related('roles_allowed_roles'); | ||
| 200 | if($allowed_roles->count == 0) | ||
| 201 | { | ||
| 202 | # check to see if any allowed roles are setup | ||
| 203 | # if not return all roles. | ||
| 204 | if($schema->resultset('RoleAllowed')->count == 0 | ||
| 205 | && $schema->resultset('RoleAdmin')->count == 0) | ||
| 206 | { | ||
| 207 | return $schema->resultset('Role'); | ||
| 208 | } | ||
| 209 | } | ||
| 210 | my $roles = $schema->resultset('Role')->search({ id => { in => $allowed_roles->get_column('role_allowed')->as_query }}); | ||
| 211 | |||
| 212 | return $roles; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | sub can_modify_user | ||
| 217 | { | ||
| 218 | my ($self, $username) = @_; | ||
| 219 | my $schema = $self->result_source->schema; | ||
| 220 | my $other_user = $schema->resultset('User')->find({ username => $username}); | ||
| 221 | die "Unable to find user '$username'" unless $other_user; | ||
| 222 | my @roles = $other_user->roles->all; | ||
| 223 | my @allowed = $self->roles_modifiable->all; | ||
| 224 | my %allowed_hash = map { $_->role => 1 } @allowed; | ||
| 225 | for my $role (@roles) | ||
| 226 | { | ||
| 227 | return 0 unless $allowed_hash{$role->role}; | ||
| 228 | } | ||
| 229 | return 1; | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | __END__ | ||
| 235 | |||
| 236 | =pod | ||
| 237 | |||
| 238 | =encoding UTF-8 | ||
| 239 | |||
| 240 | =head1 NAME | ||
| 241 | |||
| 242 | OpusVL::AppKit::RolesFor::Schema::AppKitAuthDB::Result::User | ||
| 243 | |||
| 244 | =head1 VERSION | ||
| 245 | |||
| 246 | version 2 | ||
| 247 | |||
| 248 | =head2 setup_authdb | ||
| 249 | |||
| 250 | This need to be called as the User result class is being setup to | ||
| 251 | finish the table setup. | ||
| 252 | |||
| 253 | =head2 check_password | ||
| 254 | |||
| 255 | The check_password function is usually called by Catalyst to determine | ||
| 256 | if the password is correct for a user. It returns 0 for false and 1 for | ||
| 257 | true. If the database schema has a check_password function that is used, | ||
| 258 | otherwise the standard Bcrypt function is used to check the hash stored | ||
| 259 | in the database. | ||
| 260 | |||
| 261 | =head2 getdata | ||
| 262 | |||
| 263 | =head2 setdata | ||
| 264 | |||
| 265 | =head2 disable | ||
| 266 | |||
| 267 | Disables a users account. | ||
| 268 | |||
| 269 | =head2 enable | ||
| 270 | |||
| 271 | Enables a users account. | ||
| 272 | |||
| 273 | =head2 params_hash | ||
| 274 | |||
| 275 | Finds all a users parameters, matches them with the value and returns a nice Hash ref. | ||
| 276 | |||
| 277 | =head2 set_param_by_name | ||
| 278 | |||
| 279 | Sets a users parameter by the parameter name. | ||
| 280 | Returns: | ||
| 281 | undef - if the param could be found by name. | ||
| 282 | 1 - if the param was set successfully. | ||
| 283 | |||
| 284 | =head2 delete_param_by_name | ||
| 285 | |||
| 286 | Deltes a users parameter by the parameter name. | ||
| 287 | Returns: | ||
| 288 | undef - if the param could be found by name. | ||
| 289 | 1 - if the param was deleted successfully. | ||
| 290 | |||
| 291 | =head2 roles_allowed | ||
| 292 | |||
| 293 | Returns the list of roles this user is allowed to modify. | ||
| 294 | |||
| 295 | =head2 can_modify_user | ||
| 296 | |||
| 297 | This method returns true if the user is allowed to modify the user in question. | ||
| 298 | |||
| 299 | It determines this by checking the roles the current user is allowed to modify | ||
| 300 | to the roles the other user has. If it's not allowed to modify a role that user | ||
| 301 | has then it will return false. | ||
| 302 | |||
| 303 | $user->can_modify_user('colin'); | ||
| 304 | |||
| 305 | =head1 AUTHOR | ||
| 306 | |||
| 307 | Colin Newell <colin@opusvl.com> | ||
| 308 | |||
| 309 | =head1 COPYRIGHT AND LICENSE | ||
| 310 | |||
| 311 | This software is copyright (c) 2016 by OpusVL. | ||
| 312 | |||
| 313 | This is free software; you can redistribute it and/or modify it under | ||
| 314 | the same terms as the Perl 5 programming language system itself. | ||
| 315 | |||
| 316 | =cut |