PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/auth/classes/kohana/auth.php

https://bitbucket.org/Ahineya/trn_dev
PHP | 169 lines | 71 code | 27 blank | 71 comment | 5 complexity | afa09dafcf51a5d9f3be4394cc39de53 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * User authorization library. Handles user login and logout, as well as secure
  4. * password hashing.
  5. *
  6. * @package Kohana/Auth
  7. * @author Kohana Team
  8. * @copyright (c) 2007-2010 Kohana Team
  9. * @license http://kohanaframework.org/license
  10. */
  11. abstract class Kohana_Auth {
  12. // Auth instances
  13. protected static $_instance;
  14. /**
  15. * Singleton pattern
  16. *
  17. * @return Auth
  18. */
  19. public static function instance()
  20. {
  21. if ( ! isset(Auth::$_instance))
  22. {
  23. // Load the configuration for this type
  24. $config = Kohana::$config->load('auth');
  25. if ( ! $type = $config->get('driver'))
  26. {
  27. $type = 'file';
  28. }
  29. // Set the session class name
  30. $class = 'Auth_'.ucfirst($type);
  31. // Create a new session instance
  32. Auth::$_instance = new $class($config);
  33. }
  34. return Auth::$_instance;
  35. }
  36. protected $_session;
  37. protected $_config;
  38. /**
  39. * Loads Session and configuration options.
  40. *
  41. * @return void
  42. */
  43. public function __construct($config = array())
  44. {
  45. // Save the config in the object
  46. $this->_config = $config;
  47. $this->_session = Session::instance($this->_config['session_type']);
  48. }
  49. abstract protected function _login($username, $password, $remember);
  50. abstract public function password($username);
  51. abstract public function check_password($password);
  52. /**
  53. * Gets the currently logged in user from the session.
  54. * Returns NULL if no user is currently logged in.
  55. *
  56. * @return mixed
  57. */
  58. public function get_user($default = NULL)
  59. {
  60. return $this->_session->get($this->_config['session_key'], $default);
  61. }
  62. /**
  63. * Attempt to log in a user by using an ORM object and plain-text password.
  64. *
  65. * @param string username to log in
  66. * @param string password to check against
  67. * @param boolean enable autologin
  68. * @return boolean
  69. */
  70. public function login($username, $password, $remember = FALSE)
  71. {
  72. if (empty($password))
  73. return FALSE;
  74. return $this->_login($username, $password, $remember);
  75. }
  76. /**
  77. * Log out a user by removing the related session variables.
  78. *
  79. * @param boolean completely destroy the session
  80. * @param boolean remove all tokens for user
  81. * @return boolean
  82. */
  83. public function logout($destroy = FALSE, $logout_all = FALSE)
  84. {
  85. if ($destroy === TRUE)
  86. {
  87. // Destroy the session completely
  88. $this->_session->destroy();
  89. }
  90. else
  91. {
  92. // Remove the user from the session
  93. $this->_session->delete($this->_config['session_key']);
  94. // Regenerate session_id
  95. $this->_session->regenerate();
  96. }
  97. // Double check
  98. return ! $this->logged_in();
  99. }
  100. /**
  101. * Check if there is an active session. Optionally allows checking for a
  102. * specific role.
  103. *
  104. * @param string role name
  105. * @return mixed
  106. */
  107. public function logged_in($role = NULL)
  108. {
  109. return ($this->get_user() !== NULL);
  110. }
  111. /**
  112. * Creates a hashed hmac password from a plaintext password. This
  113. * method is deprecated, [Auth::hash] should be used instead.
  114. *
  115. * @deprecated
  116. * @param string plaintext password
  117. */
  118. public function hash_password($password)
  119. {
  120. return $this->hash($password);
  121. }
  122. /**
  123. * Perform a hmac hash, using the configured method.
  124. *
  125. * @param string string to hash
  126. * @return string
  127. */
  128. public function hash($str)
  129. {
  130. if ( ! $this->_config['hash_key'])
  131. throw new Kohana_Exception('A valid hash key must be set in your auth config.');
  132. return hash_hmac($this->_config['hash_method'], $str, $this->_config['hash_key']);
  133. }
  134. protected function complete_login($user)
  135. {
  136. // Regenerate session_id
  137. $this->_session->regenerate();
  138. // Store username in session
  139. $this->_session->set($this->_config['session_key'], $user);
  140. return TRUE;
  141. }
  142. } // End Auth