Modules

Kohana_Auth_ORM
extends Auth
extends Kohana_Auth

ORM Auth driver.

package
Kohana/Auth
author
Kohana Team
copyright
© 2007-2010 Kohana Team
license
http://kohanaframework.org/license

Class declared in MODPATH/auth/classes/kohana/auth/orm.php on line 10.

Properties

protected $_config

protected static $_instance

NULL

protected $_session

Methods

public auto_login( ) (defined in Kohana_Auth_ORM)

Logs a user in, based on the authautologin cookie.

Return Values

  • mixed

Source Code

public function auto_login()
{
	if ($token = Cookie::get($this->_config['autologin_key']))
	{
		// Load the token and user
		$token = ORM::factory('user_token', array('token' => $token));

		if ($token->loaded() AND $token->user->loaded())
		{
			if ($token->user_agent === sha1(Request::$user_agent))
			{
				// Save the token to create a new unique token
				$token->save();

				// Set the new token
				Cookie::set($this->_config['autologin_key'], $token->token, $token->expires - time());

				// Complete the login with the found data
				$this->complete_login($token->user);

				// Automatic login was successful
				return $token->user;
			}

			// Token is invalid
			$token->delete();
		}
	}

	return FALSE;
}

public check_password( string $password ) (defined in Kohana_Auth_ORM)

Compare password with original (hashed). Works for current (logged in) user

Parameters

  • string $password required - $password

Return Values

  • boolean

Source Code

public function check_password($password)
{
	$user = $this->get_user();

	if ($user === FALSE)
	{
		// nothing to compare
		return FALSE;
	}

	$hash = $this->hash_password($password, $this->find_salt($user->password));

	return $hash == $user->password;
}

public force_login( mixed $user [, boolean $mark_session_as_forced = bool FALSE ] ) (defined in Kohana_Auth_ORM)

Forces a user to be logged in, without specifying a password.

Parameters

  • mixed $user required - Username string, or user ORM object
  • boolean $mark_session_as_forced = bool FALSE - Mark the session as forced

Return Values

  • boolean

Source Code

public function force_login($user, $mark_session_as_forced = FALSE)
{
	if ( ! is_object($user))
	{
		$username = $user;

		// Load the user
		$user = ORM::factory('user');
		$user->where($user->unique_key($username), '=', $username)->find();
	}

	if ($mark_session_as_forced === TRUE)
	{
		// Mark the session as forced, to prevent users from changing account information
		$this->_session->set($this->_config['forced_key'], TRUE);
	}

	// Run the standard completion
	$this->complete_login($user);
}

public get_user( ) (defined in Kohana_Auth_ORM)

Gets the currently logged in user from the session (with auto_login check). Returns FALSE if no user is currently logged in.

Return Values

  • mixed

Source Code

public function get_user()
{
	$user = parent::get_user();

	if ($user === FALSE)
	{
		// check for "remembered" login
		$user = $this->auto_login();
	}

	return $user;
}

public logged_in( [ mixed $role = NULL , boolean $all_required = bool TRUE ] ) (defined in Kohana_Auth_ORM)

Checks if a session is active.

Parameters

  • mixed $role = NULL - Role name string, role ORM object, or array with role names
  • boolean $all_required = bool TRUE - Check user for every role applied (TRUE, by default) or if any?

Return Values

  • boolean

Source Code

public function logged_in($role = NULL, $all_required = TRUE)
{
	$status = FALSE;

	// Get the user from the session
	$user = $this->get_user();

	if (is_object($user) AND $user instanceof Model_User AND $user->loaded())
	{
		// Everything is okay so far
		$status = TRUE;

		if ( ! empty($role))
		{
			// Multiple roles to check
			if (is_array($role))
			{
				// set initial status
				$status = (bool) $all_required;

				// Check each role
				foreach ($role as $_role)
				{
					if ( ! is_object($_role))
					{
						$_role = ORM::factory('role', array('name' => $_role));
					}

					// If the user doesn't have the role
					if ( ! $user->has('roles', $_role))
					{
						// Set the status false and get outta here
						$status = FALSE;
						if ($all_required)
						{
							break;
						}
					}
				   elseif ( ! $all_required )
				   {
					   $status = TRUE;
					   break;
				   }
				}
			}
			// Single role to check
			else
			{
				if ( ! is_object($role))
				{
					// Load the role
					$role = ORM::factory('role', array('name' => $role));
				}

				// Check that the user has the given role
				$status = $user->has('roles', $role);
			}
		}
	}

	return $status;
}

public logout( [ boolean $destroy = bool FALSE , boolean $logout_all = bool FALSE ] ) (defined in Kohana_Auth_ORM)

Log a user out and remove any autologin cookies.

Parameters

  • boolean $destroy = bool FALSE - Completely destroy the session
  • boolean $logout_all = bool FALSE - Remove all tokens for user

Return Values

  • boolean

Source Code

public function logout($destroy = FALSE, $logout_all = FALSE)
{
	// Set by force_login()
	$this->_session->delete($this->_config['forced_key']);

	if ($token = Cookie::get($this->_config['autologin_key']))
	{
		// Delete the autologin cookie to prevent re-login
		Cookie::delete($this->_config['autologin_key']);

		// Clear the autologin token from the database
		$token = ORM::factory('user_token', array('token' => $token));

		if ($token->loaded() AND $logout_all)
		{
			ORM::factory('user_token')->where('user_id', '=', $token->user_id)->delete_all();
		}
		elseif ($token->loaded())
		{
			$token->delete();
		}
	}

	return parent::logout($destroy);
}

public password( mixed $user ) (defined in Kohana_Auth_ORM)

Get the stored password for a username.

Parameters

  • mixed $user required - Username string, or user ORM object

Return Values

  • string

Source Code

public function password($user)
{
	if ( ! is_object($user))
	{
		$username = $user;

		// Load the user
		$user = ORM::factory('user');
		$user->where($user->unique_key($username), '=', $username)->find();
	}

	return $user->password;
}

public remember( [ Model_User $user = NULL ] ) (defined in Kohana_Auth_ORM)

Remember user (create token and save it in cookie)

Parameters

  • Model_User $user = NULL - $user

Return Values

  • boolean

Source Code

public function remember($user = NULL)
{
	if (is_null($user))
	{
		$user = $this->get_user();
	}
	if ( ! $user)
	{
		return FALSE;
	}

	// Create a new autologin token
	$token = ORM::factory('user_token');

	// Set token data
	$token->user_id = $user->id;
	$token->expires = time() + $this->_config['lifetime'];
	$token->save();

	// Set the autologin cookie
	Cookie::set($this->_config['autologin_key'], $token->token, $this->_config['lifetime']);

	return TRUE;
}

public __construct( ) (defined in Kohana_Auth)

Loads Session and configuration options.

Return Values

  • void

Source Code

public function __construct($config = array())
{
	// Clean up the salt pattern and split it into an array
	$config['salt_pattern'] = preg_split('/,\s*/', Kohana::config('auth')->get('salt_pattern'));

	// Save the config in the object
	$this->_config = $config;

	$this->_session = Session::instance();
}

public static factory( ) (defined in Kohana_Auth)

Create an instance of Auth.

Return Values

  • Auth

Source Code

public static function factory($config = array())
{
	return new Auth($config);
}

public find_salt( string $password ) (defined in Kohana_Auth)

Finds the salt from a password, based on the configured salt pattern.

Parameters

  • string $password required - Hashed password

Return Values

  • string

Source Code

public function find_salt($password)
{
	$salt = '';

	foreach ($this->_config['salt_pattern'] as $i => $offset)
	{
		// Find salt characters, take a good long look...
		$salt .= substr($password, $offset + $i, 1);
	}

	return $salt;
}

public hash( string $str ) (defined in Kohana_Auth)

Perform a hash, using the configured method.

Parameters

  • string $str required - String to hash

Return Values

  • string

Source Code

public function hash($str)
{
	return hash($this->_config['hash_method'], $str);
}

public hash_password( string $password [, $salt = bool FALSE ] ) (defined in Kohana_Auth)

Creates a hashed password from a plaintext password, inserting salt based on the configured salt pattern.

Parameters

  • string $password required - Plaintext password
  • unknown $salt = bool FALSE

Return Values

  • string - Hashed password string

Source Code

public function hash_password($password, $salt = FALSE)
{
	if ($salt === FALSE)
	{
		// Create a salt seed, same length as the number of offsets in the pattern
		$salt = substr($this->hash(uniqid(NULL, TRUE)), 0, count($this->_config['salt_pattern']));
	}

	// Password hash that the salt will be inserted into
	$hash = $this->hash($salt.$password);

	// Change salt to an array
	$salt = str_split($salt, 1);

	// Returned password
	$password = '';

	// Used to calculate the length of splits
	$last_offset = 0;

	foreach ($this->_config['salt_pattern'] as $offset)
	{
		// Split a new part of the hash off
		$part = substr($hash, 0, $offset - $last_offset);

		// Cut the current part out of the hash
		$hash = substr($hash, $offset - $last_offset);

		// Add the part to the password, appending the salt character
		$password .= $part.array_shift($salt);

		// Set the last offset to the current offset
		$last_offset = $offset;
	}

	// Return the password, with the remaining hash appended
	return $password.$hash;
}

public static instance( ) (defined in Kohana_Auth)

Singleton pattern

Return Values

  • Auth

Source Code

public static function instance()
{
	if ( ! isset(Auth::$_instance))
	{
		// Load the configuration for this type
		$config = Kohana::config('auth');

		if ( ! $type = $config->get('driver'))
		{
			$type = 'ORM';
		}

		// Set the session class name
		$class = 'Auth_'.ucfirst($type);

		// Create a new session instance
		Auth::$_instance = new $class($config);
	}

	return Auth::$_instance;
}

public login( string $username , string $password [, boolean $remember = bool FALSE ] ) (defined in Kohana_Auth)

Attempt to log in a user by using an ORM object and plain-text password.

Parameters

  • string $username required - Username to log in
  • string $password required - Password to check against
  • boolean $remember = bool FALSE - Enable autologin

Return Values

  • boolean

Source Code

public function login($username, $password, $remember = FALSE)
{
	if (empty($password))
		return FALSE;

	if (is_string($password))
	{
		// Get the salt from the stored password
		$salt = $this->find_salt($this->password($username));

		// Create a hashed password using the salt from the stored password
		$password = $this->hash_password($password, $salt);
	}

	return $this->_login($username, $password, $remember);
}

protected _login( string $user , string $password , boolean $remember ) (defined in Kohana_Auth_ORM)

Logs a user in.

Parameters

  • string $user required - Username
  • string $password required - Password
  • boolean $remember required - Enable autologin

Return Values

  • boolean

Source Code

protected function _login($user, $password, $remember)
{
	if ( ! is_object($user))
	{
		$username = $user;

		// Load the user
		$user = ORM::factory('user');
		$user->where($user->unique_key($username), '=', $username)->find();
	}

	// If the passwords match, perform a login
	if ($user->has('roles', ORM::factory('role', array('name' => 'login'))) AND $user->password === $password)
	{
		if ($remember === TRUE)
		{
			$this->remember($user);
		}

		// Finish the login
		$this->complete_login($user);

		return TRUE;
	}

	// Login failed
	return FALSE;
}

protected complete_login( object $user ) (defined in Kohana_Auth_ORM)

Complete the login for a user by incrementing the logins and setting session data: user_id, username, roles.

Parameters

  • object $user required - User ORM object

Return Values

  • void

Source Code

protected function complete_login($user)
{
	$user->complete_login();

	return parent::complete_login($user);
}