PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/Ahineya/trn_dev
PHP | 283 lines | 146 code | 44 blank | 93 comment | 21 complexity | 04d1a4b744c71f016ea043970a997429 MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2011 Kohana Team
  8. * @license http://kohanaframework.org/license
  9. */
  10. class Kohana_Auth_ORM extends Auth {
  11. /**
  12. * Checks if a session is active.
  13. *
  14. * @param mixed $role Role name string, role ORM object, or array with role names
  15. * @return boolean
  16. */
  17. public function logged_in($role = NULL)
  18. {
  19. // Get the user from the session
  20. $user = $this->get_user();
  21. if ( ! $user)
  22. return FALSE;
  23. if ($user instanceof Model_User AND $user->loaded())
  24. {
  25. // If we don't have a roll no further checking is needed
  26. if ( ! $role)
  27. return TRUE;
  28. if (is_array($role))
  29. {
  30. // Get all the roles
  31. $roles = ORM::factory('role')
  32. ->where('name', 'IN', $role)
  33. ->find_all()
  34. ->as_array(NULL, 'id');
  35. // Make sure all the roles are valid ones
  36. if (count($roles) !== count($role))
  37. return FALSE;
  38. }
  39. else
  40. {
  41. if ( ! is_object($role))
  42. {
  43. // Load the role
  44. $roles = ORM::factory('role', array('name' => $role));
  45. if ( ! $roles->loaded())
  46. return FALSE;
  47. }
  48. }
  49. return $user->has('roles', $roles);
  50. }
  51. }
  52. /**
  53. * Logs a user in.
  54. *
  55. * @param string username
  56. * @param string password
  57. * @param boolean enable autologin
  58. * @return boolean
  59. */
  60. protected function _login($user, $password, $remember)
  61. {
  62. if ( ! is_object($user))
  63. {
  64. $username = $user;
  65. // Load the user
  66. $user = ORM::factory('user');
  67. $user->where($user->unique_key($username), '=', $username)->find();
  68. }
  69. if (is_string($password))
  70. {
  71. // Create a hashed password
  72. $password = $this->hash($password);
  73. }
  74. // If the passwords match, perform a login
  75. if ($user->has('roles', ORM::factory('role', array('name' => 'login'))) AND $user->password === $password)
  76. {
  77. if ($remember === TRUE)
  78. {
  79. // Token data
  80. $data = array(
  81. 'user_id' => $user->id,
  82. 'expires' => time() + $this->_config['lifetime'],
  83. 'user_agent' => sha1(Request::$user_agent),
  84. );
  85. // Create a new autologin token
  86. $token = ORM::factory('user_token')
  87. ->values($data)
  88. ->create();
  89. // Set the autologin cookie
  90. Cookie::set('authautologin', $token->token, $this->_config['lifetime']);
  91. }
  92. // Finish the login
  93. $this->complete_login($user);
  94. return TRUE;
  95. }
  96. // Login failed
  97. return FALSE;
  98. }
  99. /**
  100. * Forces a user to be logged in, without specifying a password.
  101. *
  102. * @param mixed username string, or user ORM object
  103. * @param boolean mark the session as forced
  104. * @return boolean
  105. */
  106. public function force_login($user, $mark_session_as_forced = FALSE)
  107. {
  108. if ( ! is_object($user))
  109. {
  110. $username = $user;
  111. // Load the user
  112. $user = ORM::factory('user');
  113. $user->where($user->unique_key($username), '=', $username)->find();
  114. }
  115. if ($mark_session_as_forced === TRUE)
  116. {
  117. // Mark the session as forced, to prevent users from changing account information
  118. $this->_session->set('auth_forced', TRUE);
  119. }
  120. // Run the standard completion
  121. $this->complete_login($user);
  122. }
  123. /**
  124. * Logs a user in, based on the authautologin cookie.
  125. *
  126. * @return mixed
  127. */
  128. public function auto_login()
  129. {
  130. if ($token = Cookie::get('authautologin'))
  131. {
  132. // Load the token and user
  133. $token = ORM::factory('user_token', array('token' => $token));
  134. if ($token->loaded() AND $token->user->loaded())
  135. {
  136. if ($token->user_agent === sha1(Request::$user_agent))
  137. {
  138. // Save the token to create a new unique token
  139. $token->save();
  140. // Set the new token
  141. Cookie::set('authautologin', $token->token, $token->expires - time());
  142. // Complete the login with the found data
  143. $this->complete_login($token->user);
  144. // Automatic login was successful
  145. return $token->user;
  146. }
  147. // Token is invalid
  148. $token->delete();
  149. }
  150. }
  151. return FALSE;
  152. }
  153. /**
  154. * Gets the currently logged in user from the session (with auto_login check).
  155. * Returns FALSE if no user is currently logged in.
  156. *
  157. * @return mixed
  158. */
  159. public function get_user($default = NULL)
  160. {
  161. $user = parent::get_user($default);
  162. if ( ! $user)
  163. {
  164. // check for "remembered" login
  165. $user = $this->auto_login();
  166. }
  167. return $user;
  168. }
  169. /**
  170. * Log a user out and remove any autologin cookies.
  171. *
  172. * @param boolean completely destroy the session
  173. * @param boolean remove all tokens for user
  174. * @return boolean
  175. */
  176. public function logout($destroy = FALSE, $logout_all = FALSE)
  177. {
  178. // Set by force_login()
  179. $this->_session->delete('auth_forced');
  180. if ($token = Cookie::get('authautologin'))
  181. {
  182. // Delete the autologin cookie to prevent re-login
  183. Cookie::delete('authautologin');
  184. // Clear the autologin token from the database
  185. $token = ORM::factory('user_token', array('token' => $token));
  186. if ($token->loaded() AND $logout_all)
  187. {
  188. ORM::factory('user_token')->where('user_id', '=', $token->user_id)->delete_all();
  189. }
  190. elseif ($token->loaded())
  191. {
  192. $token->delete();
  193. }
  194. }
  195. return parent::logout($destroy);
  196. }
  197. /**
  198. * Get the stored password for a username.
  199. *
  200. * @param mixed username string, or user ORM object
  201. * @return string
  202. */
  203. public function password($user)
  204. {
  205. if ( ! is_object($user))
  206. {
  207. $username = $user;
  208. // Load the user
  209. $user = ORM::factory('user');
  210. $user->where($user->unique_key($username), '=', $username)->find();
  211. }
  212. return $user->password;
  213. }
  214. /**
  215. * Complete the login for a user by incrementing the logins and setting
  216. * session data: user_id, username, roles.
  217. *
  218. * @param object user ORM object
  219. * @return void
  220. */
  221. protected function complete_login($user)
  222. {
  223. $user->complete_login();
  224. return parent::complete_login($user);
  225. }
  226. /**
  227. * Compare password with original (hashed). Works for current (logged in) user
  228. *
  229. * @param string $password
  230. * @return boolean
  231. */
  232. public function check_password($password)
  233. {
  234. $user = $this->get_user();
  235. if ( ! $user)
  236. return FALSE;
  237. return ($this->hash($password) === $user->password);
  238. }
  239. } // End Auth ORM