File Auth driver
Class declared in MODPATH/user/classes/auth/file.php on line 3.
object $_configKohana config object
string $_instanceAuth instances
NULL
object $_sessionKohana session object
$_usersConstructor loads the user list into the class.
public function __construct($config_name = 'auth')
{
$config = Kohana::$config->load($config_name);
parent::__construct($config_name);
// Load user list
$this->_users = Arr::get($config, 'users', array());
}
Compare password with original (plain text). Works for current (logged in) user
string
$password
required - $passwordbooleanpublic 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 - Usernamebooleanpublic function force_login($username)
{
// Complete the login
return $this->complete_login($username);
}
Get the stored password for a username.
mixed
$username
required - Usernamestringpublic function password($username)
{
return Arr::get($this->_users, $username, FALSE);
}
Get 3rd party provider used to sign in
stringpublic function get_provider() {
return $this->_session->get($this->_config['session_key'] . '_provider', null);
}
Gets the currently logged in user from the session. Returns NULL if no user is currently logged in.
mixedpublic 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 hashstringpublic function hash($str)
{
if ( ! $this->_config['hash_key'])
throw new Gleez_Exception('A valid hash key must be set in your auth config.');
return hash_hmac($this->_config['hash_method'], $str, $this->_config['hash_key']);
}
Creates a hashed hmac password from a plaintext password. This method is deprecated, Auth::hash should be used instead.
string
$password
required - Plaintext passwordpublic function hash_password($password)
{
return $this->hash($password);
}
Singleton pattern
Authpublic 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 auth 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 namemixedpublic function logged_in($role = NULL)
{
//return ($this->get_user() !== NULL);
return ($this->get_user() !== NULL);
}
Checks if a user logged in via an OAuth provider.
string
$provider
= NULL - Provider name (e.g. 'twitter', 'google', etc.)booleanpublic function logged_in_oauth($provider = NULL)
{
// For starters, the user needs to be logged in
if ( ! parent::logged_in())
return FALSE;
// Get the user from the session.
// Because parent::logged_in returned TRUE, we know this is a valid user ORM object.
$user = $this->get_user();
if ($provider !== NULL)
{
// Check for one specific OAuth provider
$provider = $provider.'_id';
//return ! empty($user->$provider);
}
// Otherwise, just check the password field.
// We don't store passwords for OAuth users.
//return empty($user->pass);
}
Attempt to log in a user by using an ORM object and plain-text password.
string
$username
required - Username to log instring
$password
required - Password to check againstboolean
$remember
= bool FALSE - Enable autologinbooleanpublic function login($username, $password, $remember = FALSE)
{
if (empty($password))
return FALSE;
if (is_string($password))
{
// Create a hashed password
//$password = $this->hash($password); //Support for old (Drupal md5 password sum)
}
return $this->_login($username, $password, $remember);
}
Log out a user by removing the related session variables.
boolean
$destroy
= bool FALSE - Completely destroy the sessionboolean
$logout_all
= bool FALSE - Remove all tokens for userbooleanpublic 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();
}
Allows a model use email, username and OAuth provider id as unique identifiers for login
string
$value
required - Unique valuestring
$oauth_provider
= NULL - OAuth provider namestring - Field namepublic function unique_key($value, $oauth_provider = NULL)
{
if ($oauth_provider)
{
return $oauth_provider.'_id';
}
return Valid::email($value) ? 'mail' : 'name';
}
Logs a user in.
string
$username
required - Usernamestring
$password
required - Passwordboolean
$remember
required - Enable autologin (not supported)booleanprotected function _login($username, $password, $remember)
{
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;
}