/classes/kohana/auth/orm.php

https://github.com/jimktrains/kohana-auth · PHP · 300 lines · 164 code · 51 blank · 85 comment · 21 complexity · 5a3ab7328d8728e7374827c6a838a4c3 MD5 · raw file

  1. <?php defined('SYSPATH') or die('No direct access allowed.');
  2. /**
  3. * ORM Auth driver.
  4. *
  5. * @package Kohana/Auth
  6. * @author Kohana Team
  7. * @copyright (c) 2007-2010 Kohana Team
  8. * @license http://kohanaframework.org/license
  9. */
  10. class Kohana_Auth_ORM extends Auth {
  11. public function hash_password($password, $salt = FALSE)
  12. {
  13. $hash = $this->hash($password);
  14. return $hash;
  15. }
  16. public function hash($str)
  17. {
  18. return $this->_enc->hash($str);
  19. }
  20. public function login($username, $password, $remember = FALSE)
  21. {
  22. if (empty($password))
  23. return FALSE;
  24. return $this->_login($username, $password, $remember);
  25. }
  26. protected function _login($email, $password, $remember)
  27. {
  28. if ( ! is_object($user))
  29. {
  30. $username = $user;
  31. // Load the user
  32. $user = ORM::factory('user');
  33. $user->where('email', '=', $email)->find();
  34. }
  35. // If the passwords match, perform a login
  36. // if ($user->has('roles', ORM::factory('role', array('name' => 'login'))) AND
  37. // $this->_enc->compare_hash($password, $user->password)
  38. // )
  39. if($this->_enc->compare_hash($password, $user->password_hash))
  40. {
  41. if ($remember === TRUE)
  42. {
  43. // Create a new autologin token
  44. $token = ORM::factory('user_token');
  45. // Set token data
  46. $token->user_id = $user->id;
  47. $token->expires = time() + $this->_config['lifetime'];
  48. $token->save();
  49. // Set the autologin cookie
  50. Cookie::set('authautologin', $token->token, $this->_config['lifetime']);
  51. }
  52. // Finish the login
  53. $this->complete_login($user);
  54. return TRUE;
  55. }
  56. // Login failed
  57. return FALSE;
  58. }
  59. /**
  60. * Gets the currently logged in user from the session.
  61. * Returns FALSE if no user is currently logged in.
  62. *
  63. * @return mixed
  64. */
  65. public function get_user()
  66. {
  67. $user = $this->_session->get($this->_config['session_key'], FALSE);
  68. if(false!==$user)
  69. {
  70. $user = ORM::factory('User', $user);
  71. }else{
  72. $user = $this->auto_login();
  73. }
  74. return $user;
  75. }
  76. protected function complete_login($user)
  77. {
  78. $user->complete_login();
  79. // Regenerate session_id
  80. $this->_session->regenerate();
  81. // Store username in session
  82. $this->_session->set($this->_config['session_key'], $user->pk());
  83. return TRUE;
  84. }
  85. /**
  86. * Checks if a session is active.
  87. *
  88. * @param mixed role name string, role ORM object, or array with role names
  89. * @return boolean
  90. */
  91. public function logged_in($role = NULL)
  92. {
  93. $status = FALSE;
  94. // Get the user from the session
  95. $user = $this->get_user();
  96. if (is_object($user) AND $user instanceof Model_User AND $user->loaded())
  97. {
  98. // Everything is okay so far
  99. $status = TRUE;
  100. if ( ! empty($role))
  101. {
  102. // Multiple roles to check
  103. if (is_array($role))
  104. {
  105. // Check each role
  106. foreach ($role as $_role)
  107. {
  108. if ( ! is_object($_role))
  109. {
  110. $_role = ORM::factory('role', array('name' => $_role));
  111. }
  112. // If the user doesn't have the role
  113. if ( ! $user->has('roles', $_role))
  114. {
  115. // Set the status false and get outta here
  116. $status = FALSE;
  117. break;
  118. }
  119. }
  120. }
  121. // Single role to check
  122. else
  123. {
  124. if ( ! is_object($role))
  125. {
  126. // Load the role
  127. $role = ORM::factory('role', array('name' => $role));
  128. }
  129. // Check that the user has the given role
  130. $status = $user->has('roles', $role);
  131. }
  132. }
  133. }
  134. return $status;
  135. }
  136. /**
  137. * Forces a user to be logged in, without specifying a password.
  138. *
  139. * @param mixed username string, or user ORM object
  140. * @param boolean mark the session as forced
  141. * @return boolean
  142. */
  143. public function force_login($user, $mark_session_as_forced = FALSE)
  144. {
  145. if ( ! is_object($user))
  146. {
  147. $username = $user;
  148. // Load the user
  149. $user = ORM::factory('user');
  150. $user->where($user->unique_key($username), '=', $username)->find();
  151. }
  152. if ($mark_session_as_forced === TRUE)
  153. {
  154. // Mark the session as forced, to prevent users from changing account information
  155. $this->_session->set('auth_forced', TRUE);
  156. }
  157. // Run the standard completion
  158. $this->complete_login($user);
  159. }
  160. /**
  161. * Logs a user in, based on the authautologin cookie.
  162. *
  163. * @return mixed
  164. */
  165. public function auto_login()
  166. {
  167. if ($token = Cookie::get('authautologin'))
  168. {
  169. // Load the token and user
  170. $token = ORM::factory('user_token', array('token' => $token));
  171. if ($token->loaded() AND $token->user->loaded())
  172. {
  173. if ($token->user_agent === sha1(Request::$user_agent))
  174. {
  175. // Save the token to create a new unique token
  176. $token->save();
  177. // Set the new token
  178. Cookie::set('authautologin', $token->token, $token->expires - time());
  179. // Complete the login with the found data
  180. $this->complete_login($token->user);
  181. // Automatic login was successful
  182. return $token->user;
  183. }
  184. // Token is invalid
  185. $token->delete();
  186. }
  187. }
  188. return FALSE;
  189. }
  190. /**
  191. * Log a user out and remove any autologin cookies.
  192. *
  193. * @param boolean completely destroy the session
  194. * @param boolean remove all tokens for user
  195. * @return boolean
  196. */
  197. public function logout($destroy = FALSE, $logout_all = FALSE)
  198. {
  199. // Set by force_login()
  200. $this->_session->delete('auth_forced');
  201. if ($token = Cookie::get('authautologin'))
  202. {
  203. // Delete the autologin cookie to prevent re-login
  204. Cookie::delete('authautologin');
  205. // Clear the autologin token from the database
  206. $token = ORM::factory('user_token', array('token' => $token));
  207. if ($token->loaded() AND $logout_all)
  208. {
  209. ORM::factory('user_token')->where('user_id', '=', $token->user_id)->delete_all();
  210. }
  211. elseif ($token->loaded())
  212. {
  213. $token->delete();
  214. }
  215. }
  216. return parent::logout($destroy);
  217. }
  218. /**
  219. * Get the stored password for a username.
  220. *
  221. * @param mixed username string, or user ORM object
  222. * @return string
  223. */
  224. public function password($user)
  225. {
  226. if ( ! is_object($user))
  227. {
  228. $user = ORM::factory('user', $user);
  229. }
  230. return $user->password_hash;
  231. }
  232. /**
  233. * Compare password with original (hashed). Works for current (logged in) user
  234. *
  235. * @param string $password
  236. * @return boolean
  237. */
  238. public function check_password($password)
  239. {
  240. $user = $this->get_user();
  241. if ($user === FALSE)
  242. {
  243. // nothing to compare
  244. return FALSE;
  245. }
  246. $hash = $this->hash_password($password);
  247. return $hash == $user->password_hash;
  248. }
  249. } // End Auth ORM