/usr/share/php/kohana2/modules/auth/models/auth_user.php is in libkohana2-modules-php 2.3.4-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | <?php defined('SYSPATH') OR die('No direct access allowed.');
class Auth_User_Model extends ORM {
// Relationships
protected $has_many = array('user_tokens');
protected $has_and_belongs_to_many = array('roles');
// Columns to ignore
protected $ignored_columns = array('password_confirm');
public function __set($key, $value)
{
if ($key === 'password')
{
// Use Auth to hash the password
$value = Auth::instance()->hash_password($value);
}
parent::__set($key, $value);
}
/**
* Validates and optionally saves a new user record from an array.
*
* @param array values to check
* @param boolean save the record when validation succeeds
* @return boolean
*/
public function validate(array & $array, $save = FALSE)
{
$array = Validation::factory($array)
->pre_filter('trim')
->add_rules('email', 'required', 'length[4,127]', 'valid::email', array($this, 'email_available'))
->add_rules('username', 'required', 'length[4,32]', 'chars[a-zA-Z0-9_.]', array($this, 'username_available'))
->add_rules('password', 'required', 'length[5,42]')
->add_rules('password_confirm', 'matches[password]');
return parent::validate($array, $save);
}
/**
* Validates login information from an array, and optionally redirects
* after a successful login.
*
* @param array values to check
* @param string URI or URL to redirect to
* @return boolean
*/
public function login(array & $array, $redirect = FALSE)
{
$array = Validation::factory($array)
->pre_filter('trim')
->add_rules('username', 'required', 'length[4,127]')
->add_rules('password', 'required', 'length[5,42]');
// Login starts out invalid
$status = FALSE;
if ($array->validate())
{
// Attempt to load the user
$this->find($array['username']);
if ($this->loaded AND Auth::instance()->login($this, $array['password']))
{
if (is_string($redirect))
{
// Redirect after a successful login
url::redirect($redirect);
}
// Login is successful
$status = TRUE;
}
else
{
$array->add_error('username', 'invalid');
}
}
return $status;
}
/**
* Validates an array for a matching password and password_confirm field.
*
* @param array values to check
* @param string save the user if
* @return boolean
*/
public function change_password(array & $array, $save = FALSE)
{
$array = Validation::factory($array)
->pre_filter('trim')
->add_rules('password', 'required', 'length[5,127]')
->add_rules('password_confirm', 'matches[password]');
if ($status = $array->validate())
{
// Change the password
$this->password = $array['password'];
if ($save !== FALSE AND $status = $this->save())
{
if (is_string($save))
{
// Redirect to the success page
url::redirect($save);
}
}
}
return $status;
}
/**
* Tests if a username exists in the database. This can be used as a
* Valdidation rule.
*
* @param mixed id to check
* @return boolean
*
*/
public function username_exists($id)
{
return $this->unique_key_exists($id);
}
/**
* Does the reverse of unique_key_exists() by returning TRUE if user id is available
* Validation rule.
*
* @param mixed id to check
* @return boolean
*/
public function username_available($username)
{
return ! $this->unique_key_exists($username);
}
/**
* Does the reverse of unique_key_exists() by returning TRUE if email is available
* Validation Rule
*
* @param string $email
* @return void
*/
public function email_available($email)
{
return ! $this->unique_key_exists($email);
}
/**
* Tests if a unique key value exists in the database
*
* @param mixed value the value to test
* @return boolean
*/
public function unique_key_exists($value)
{
return (bool) $this->db
->where($this->unique_key($value), $value)
->count_records($this->table_name);
}
/**
* Allows a model to be loaded by username or email address.
*/
public function unique_key($id)
{
if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id))
{
return valid::email($id) ? 'email' : 'username';
}
return parent::unique_key($id);
}
} // End Auth User Model
|