This class is a transparent base class for Auth_File and should not be accessed directly.
File Auth driver. [!!] this Auth driver does not support roles nor autologin.
Class declared in MODPATH/auth/classes/Kohana/Auth/File.php on line 12.
$_configNULL
$_instanceNULL
$_sessionNULL
$_usersNULL
Constructor loads the user list into the class.
public function __construct($config = [])
{
parent::__construct($config);
// Load user list
$this->_users = Arr::get($config, 'users', []);
}
Compare password with original (plain text). Works for current (logged in) user
string
$password
required - Password boolean
public function check_password($password)
{
$username = $this->get_user();
if ($username === false) {
return false;
}
return ($password === $this->password($username));
}
Forces a user to be logged in, without specifying a password.
mixed
$username
required - Username boolean
public function force_login($username)
{
// Complete the login
return $this->complete_login($username);
}
Get the stored password for a username.
mixed
$username
required - Username string
public function password($username)
{
return Arr::get($this->_users, $username, false);
}
Gets the currently logged in user from the session. Returns null if no user is currently logged in.
mixed
$default
= NULL - Default value to return if the user is currently not logged in. mixed
public function get_user($default = null)
{
return $this->_session->get($this->_config['session_key'], $default);
}
Perform a hmac hash, using the configured method.
string
$str
required - String to hash string
public function hash($str)
{
if (!$this->_config['hash_key'])
throw new Kohana_Exception('A valid hash key must be set in your auth config.');
return hash_hmac($this->_config['hash_method'], $str, $this->_config['hash_key']);
}
Singleton pattern
Auth
public static function instance()
{
if (!isset(Auth::$_instance)) {
// Load the configuration for this type
$config = Kohana::$config->load('auth');
if (!$type = $config->get('driver')) {
$type = 'file';
}
// Set the session class name
$class = 'Auth_' . ucfirst($type);
// Create a new session instance
Auth::$_instance = new $class($config);
}
return Auth::$_instance;
}
Check if there is an active session. Optionally allows checking for a specific role.
string
$role
= NULL - Role name mixed
public function logged_in($role = null)
{
return ($this->get_user() !== null);
}
Attempt to log in a user by using an ORM object and plain-text password.
string
$username
required - Username to log in string
$password
required - Password to check against boolean
$remember
= bool FALSE - Enable autologin boolean
public function login($username, $password, $remember = false)
{
if (empty($password))
return false;
return $this->_login($username, $password, $remember);
}
Log out a user by removing the related session variables.
boolean
$destroy
= bool FALSE - Completely destroy the session boolean
$logout_all
= bool FALSE - Remove all tokens for user boolean
public function logout($destroy = false, $logout_all = false)
{
if ($destroy === true) {
// Destroy the session completely
$this->_session->destroy();
} else {
// Remove the user from the session
$this->_session->delete($this->_config['session_key']);
// Regenerate session_id
$this->_session->regenerate();
}
// Double check
return !$this->logged_in();
}
Logs a user in.
string
$username
required - Username string
$password
required - Password boolean
$remember
required - Enable autologin (not supported) boolean
protected function _login($username, $password, $remember)
{
if (is_string($password)) {
// Create a hashed password
$password = $this->hash($password);
}
if (isset($this->_users[$username]) AND $this->_users[$username] === $password) {
// Complete the login
return $this->complete_login($username);
}
// Login failed
return false;
}
protected function complete_login($user)
{
// Regenerate session_id
$this->_session->regenerate();
// Store username in session
$this->_session->set($this->_config['session_key'], $user);
return true;
}