| 1 | package ETLp::Audit::Browser::Model::User; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | class ETLp::Audit::Browser::Model::User with(ETLp::Role::Config, ETLp::Role::Schema, ETLp::Role::Audit, ETLp::Role::Browser) { | ||
| 6 | |||
| 7 | use Crypt::PasswdMD5 'unix_md5_crypt'; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | method get_user_by_username(Str $username) { | ||
| 11 | return $self->EtlpUser->single({username => $username}); | ||
| 12 | } | ||
| 13 | |||
| 14 | method get_user(Maybe[Int] $user_id?) { | ||
| 15 | return $self->EtlpUser->find($user_id); | ||
| 16 | } | ||
| 17 | |||
| 18 | method get_users(Int :$page = 1) { | ||
| 19 | return $self->EtlpUser->search( | ||
| 20 | undef, | ||
| 21 | { | ||
| 22 | page => $page, | ||
| 23 | order_by => 'first_name, last_name', | ||
| 24 | rows => $self->pagesize, | ||
| 25 | } | ||
| 26 | ); | ||
| 27 | } | ||
| 28 | |||
| 29 | method save(HashRef $params) { | ||
| 30 | delete $params->{password2} if $params->{password2}; | ||
| 31 | $params->{password} = $self->encrypt_password($params->{password}) | ||
| 32 | if $params->{password}; | ||
| 33 | |||
| 34 | $params->{admin} = 0 unless $params->{admin}; | ||
| 35 | $params->{active} = 0 unless $params->{active}; | ||
| 36 | |||
| 37 | $self->logger->debug('Paramters to save: ' . Dumper($params)); | ||
| 38 | return $self->EtlpUser->update_or_create($params); | ||
| 39 | } | ||
| 40 | |||
| 41 | method generate_salt { | ||
| 42 | my @chars = ('.', '/', 0 .. 9, 'A' .. 'Z', 'a' .. 'z'); | ||
| 43 | my $length = rand(8); | ||
| 44 | my $salt; | ||
| 45 | |||
| 46 | $salt .= (@chars)[rand @chars] for (1 .. $length); | ||
| 47 | |||
| 48 | return $salt; | ||
| 49 | } | ||
| 50 | |||
| 51 | method encrypt_password(Str $password) { | ||
| 52 | return unix_md5_crypt($password, $self->generate_salt); | ||
| 53 | } | ||
| 54 | |||
| 55 | method check_password(Str $plaintext_password, Str $encrypted_password) { | ||
| 56 | my $salt = (split(/\$/, $encrypted_password))[2]; | ||
| 57 | return (unix_md5_crypt($plaintext_password, $salt) eq | ||
| 58 | $encrypted_password); | ||
| 59 | } | ||
| 60 | |||
| 61 | method update_password(Str $password, Int $user_id) { | ||
| 62 | my $user = $self->EtlpUser->find($user_id); | ||
| 63 | $user->password($self->encrypt_password($password)); | ||
| 64 | $user->update; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | =head1 NAME | ||
| 69 | |||
| 70 | ETLp::Audit::Browser::Model::User - Model Class for interacting | ||
| 71 | with Runtime FileProcess User Records | ||
| 72 | |||
| 73 | =head1 SYNOPSIS | ||
| 74 | |||
| 75 | use ETLp::Audit::Browser::Model::User; | ||
| 76 | |||
| 77 | my $model = ETLp::Audit::Browser::Model::User->new(); | ||
| 78 | my $processes = $model->get_user_by_username('jbloggs'); | ||
| 79 | |||
| 80 | =head1 METHODS | ||
| 81 | |||
| 82 | =head2 get_user_by_username | ||
| 83 | |||
| 84 | Returns an etlp_user record given the username | ||
| 85 | |||
| 86 | =head3 Parameters | ||
| 87 | |||
| 88 | * username. Required. The name of the user | ||
| 89 | |||
| 90 | =head3 Returns | ||
| 91 | |||
| 92 | * A DBIx::Class record | ||
| 93 | |||
| 94 | =head2 get_user | ||
| 95 | |||
| 96 | Returns an etlp_user record given the user id | ||
| 97 | |||
| 98 | =head3 Parameters | ||
| 99 | |||
| 100 | * username. Required. The name of the user | ||
| 101 | |||
| 102 | =head3 Returns | ||
| 103 | |||
| 104 | * A DBIx::Class record | ||
| 105 | |||
| 106 | =head2 get_users | ||
| 107 | |||
| 108 | Returns the users one page at a time | ||
| 109 | |||
| 110 | =head3 Parameters | ||
| 111 | |||
| 112 | * page. optional. The page being requested. Defaults to 1 | ||
| 113 | |||
| 114 | =head3 Returns | ||
| 115 | |||
| 116 | * A DBIx::Class resultset | ||
| 117 | |||
| 118 | =head2 save | ||
| 119 | |||
| 120 | Saves user input | ||
| 121 | |||
| 122 | =head3 Parameters | ||
| 123 | |||
| 124 | A hashref consisting of | ||
| 125 | |||
| 126 | * user_id | ||
| 127 | * username | ||
| 128 | * first_name | ||
| 129 | * last_name | ||
| 130 | * password (optional), unencrypted | ||
| 131 | * password2 (optional), unencrypted | ||
| 132 | * email_address (optional) | ||
| 133 | * admin (optional, 1= admin 0 = not admin) | ||
| 134 | * active (optional, 1 = active, 0 = inactive) | ||
| 135 | |||
| 136 | =head3 Returns | ||
| 137 | |||
| 138 | * A DBIx::Class resultset | ||
| 139 | |||
| 140 | =head2 generate_salt | ||
| 141 | |||
| 142 | Returns a salt that is used in the password encrytion routine | ||
| 143 | |||
| 144 | =head3 Parameters | ||
| 145 | |||
| 146 | * None | ||
| 147 | |||
| 148 | head3 Returns | ||
| 149 | |||
| 150 | * Up to eight characters (from Lower and upper case letters, | ||
| 151 | digits period, slash) | ||
| 152 | |||
| 153 | =head2 encrypt_password | ||
| 154 | |||
| 155 | Encrypts plaint test text password | ||
| 156 | |||
| 157 | =head3 Parameters | ||
| 158 | |||
| 159 | * password. String. | ||
| 160 | |||
| 161 | =head3 Returns | ||
| 162 | |||
| 163 | * encrypted password | ||
| 164 | |||
| 165 | =head2 check_password | ||
| 166 | |||
| 167 | Checks whether the exncrypted plaintext password will match the actual | ||
| 168 | encrypted password | ||
| 169 | |||
| 170 | =head3 Parameters | ||
| 171 | |||
| 172 | * paintext_password. String. Mandatory. | ||
| 173 | * encrypted_password. String. Mandatory. The encrypted password | ||
| 174 | |||
| 175 | =head3 Returns | ||
| 176 | |||
| 177 | * Void | ||
| 178 | |||
| 179 | =head2 update_password | ||
| 180 | |||
| 181 | Update a user's password | ||
| 182 | |||
| 183 | =head3 Parameters | ||
| 184 | |||
| 185 | * password. String. Mandatory. The plaintext password | ||
| 186 | * user_id. Integer. Mandatory. The user updating this record. | ||
| 187 | |||
| 188 | =head3 Returns | ||
| 189 | |||
| 190 | * Void | ||
| 191 | |||
| 192 | =head1 LICENSE AND COPYRIGHT | ||
| 193 | |||
| 194 | Copyright 2010 Redbone Systems Ltd | ||
| 195 | |||
| 196 | This program is free software; you can redistribute it and/or modify it | ||
| 197 | under the terms of either: the GNU General Public License as published | ||
| 198 | by the Free Software Foundation; or the Artistic License. | ||
| 199 | |||
| 200 | The terms are in the LICENSE file that accompanies this application | ||
| 201 | |||
| 202 | =cut |