PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/ekiwookie/juss
PHP | 291 lines | 151 code | 45 blank | 95 comment | 22 complexity | 2192ac04057df2dc73c26b0d1b89733d 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-2012 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 $remember 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->pk(),
  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 $user username string, or user ORM object
  103. * @param boolean $mark_session_as_forced 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 $default if no user is currently logged in.
  156. *
  157. * @param mixed $default to return in case user isn't logged in
  158. * @return mixed
  159. */
  160. public function get_user($default = NULL)
  161. {
  162. $user = parent::get_user($default);
  163. if ($user === $default)
  164. {
  165. // check for "remembered" login
  166. if (($user = $this->auto_login()) === FALSE)
  167. return $default;
  168. }
  169. return $user;
  170. }
  171. /**
  172. * Log a user out and remove any autologin cookies.
  173. *
  174. * @param boolean $destroy completely destroy the session
  175. * @param boolean $logout_all remove all tokens for user
  176. * @return boolean
  177. */
  178. public function logout($destroy = FALSE, $logout_all = FALSE)
  179. {
  180. // Set by force_login()
  181. $this->_session->delete('auth_forced');
  182. if ($token = Cookie::get('authautologin'))
  183. {
  184. // Delete the autologin cookie to prevent re-login
  185. Cookie::delete('authautologin');
  186. // Clear the autologin token from the database
  187. $token = ORM::factory('user_token', array('token' => $token));
  188. if ($token->loaded() AND $logout_all)
  189. {
  190. // Delete all user tokens. This isn't the most elegant solution but does the job
  191. $tokens = ORM::factory('user_token')->where('user_id','=',$token->user_id)->find_all();
  192. foreach ($tokens as $_token)
  193. {
  194. $_token->delete();
  195. }
  196. }
  197. elseif ($token->loaded())
  198. {
  199. $token->delete();
  200. }
  201. }
  202. return parent::logout($destroy);
  203. }
  204. /**
  205. * Get the stored password for a username.
  206. *
  207. * @param mixed $user username string, or user ORM object
  208. * @return string
  209. */
  210. public function password($user)
  211. {
  212. if ( ! is_object($user))
  213. {
  214. $username = $user;
  215. // Load the user
  216. $user = ORM::factory('user');
  217. $user->where($user->unique_key($username), '=', $username)->find();
  218. }
  219. return $user->password;
  220. }
  221. /**
  222. * Complete the login for a user by incrementing the logins and setting
  223. * session data: user_id, username, roles.
  224. *
  225. * @param object $user user ORM object
  226. * @return void
  227. */
  228. protected function complete_login($user)
  229. {
  230. $user->complete_login();
  231. return parent::complete_login($user);
  232. }
  233. /**
  234. * Compare password with original (hashed). Works for current (logged in) user
  235. *
  236. * @param string $password
  237. * @return boolean
  238. */
  239. public function check_password($password)
  240. {
  241. $user = $this->get_user();
  242. if ( ! $user)
  243. return FALSE;
  244. return ($this->hash($password) === $user->password);
  245. }
  246. } // End Auth ORM