auth.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php namespace Laravel;
  2. class Auth {
  3. /**
  4. * The current user of the application.
  5. *
  6. * @var object
  7. */
  8. public static $user;
  9. /**
  10. * The key used when storing the user ID in the session.
  11. *
  12. * @var string
  13. */
  14. const user_key = 'laravel_user_id';
  15. /**
  16. * The key used when setting the "remember me" cookie.
  17. *
  18. * @var string
  19. */
  20. const remember_key = 'laravel_remember';
  21. /**
  22. * Determine if the user of the application is not logged in.
  23. *
  24. * This method is the inverse of the "check" method.
  25. *
  26. * @return bool
  27. */
  28. public static function guest()
  29. {
  30. return ! static::check();
  31. }
  32. /**
  33. * Determine if the user of the application is logged in.
  34. *
  35. * @return bool
  36. */
  37. public static function check()
  38. {
  39. return ! is_null(static::user());
  40. }
  41. /**
  42. * Get the current user of the application.
  43. *
  44. * <code>
  45. * // Get the current user of the application
  46. * $user = Auth::user();
  47. *
  48. * // Access a property on the current user of the application
  49. * $email = Auth::user()->email;
  50. * </code>
  51. *
  52. * @return object|null
  53. */
  54. public static function user()
  55. {
  56. if ( ! is_null(static::$user)) return static::$user;
  57. $id = IoC::core('session')->get(Auth::user_key);
  58. // To retrieve the user, we'll first attempt to use the "user" Closure
  59. // defined in the auth configuration file, passing in the ID. The user
  60. // Closure gives the developer a ton of freedom surrounding how the
  61. // user is actually retrieved.
  62. $config = Config::get('auth');
  63. static::$user = call_user_func($config['user'], $id);
  64. // If the user wasn't found in the database but a "remember me" cookie
  65. // exists, we'll attempt to recall the user based on the cookie value.
  66. // Since all cookies contain a fingerprint hash verifying that they
  67. // haven't changed, we can trust it.
  68. $recaller = Cookie::get(Auth::remember_key);
  69. if (is_null(static::$user) and ! is_null($recaller))
  70. {
  71. static::$user = static::recall($recaller);
  72. }
  73. return static::$user;
  74. }
  75. /**
  76. * Attempt to login a user based on a long-lived "remember me" cookie.
  77. *
  78. * @param string $recaller
  79. * @return mixed
  80. */
  81. protected static function recall($recaller)
  82. {
  83. // When the remember me cookie is stored, it is encrypted and contains
  84. // the user's ID and a long, random string. The segments are separated
  85. // by a pipe character so we'll explode on that.
  86. $recaller = explode('|', Crypter::decrypt($recaller));
  87. // We'll pass the ID that was stored in the cookie into the same user
  88. // Closure that is used by the "user" method. If the method returns
  89. // a user, we will log them into the application.
  90. $user = call_user_func(Config::get('auth.user'), $recaller[0]);
  91. if ( ! is_null($user))
  92. {
  93. static::login($user);
  94. return $user;
  95. }
  96. }
  97. /**
  98. * Attempt to log a user into the application.
  99. *
  100. * <code>
  101. * // Attempt to log a user into the application
  102. * $success = Auth::attempt('username', 'password');
  103. *
  104. * // Attempt to login a user and set the "remember me" cookie
  105. * Auth::attempt('username', 'password', true);
  106. * </code>
  107. *
  108. * @param string $username
  109. * @param string $password
  110. * @param bool $remember
  111. * @return bool
  112. */
  113. public static function attempt($username, $password = null, $remember = false)
  114. {
  115. $config = Config::get('auth');
  116. // When attempting to login the user, we will call the "attempt" closure
  117. // from the configuration file. This gives the developer the freedom to
  118. // authenticate based on the needs of their application.
  119. //
  120. // All of the password hashing and checking and left totally up to the
  121. // developer, as this gives them the freedom to use any hashing scheme
  122. // or authentication provider they wish.
  123. $user = call_user_func($config['attempt'], $username, $password);
  124. // If the user credentials were authenticated by the closure, we will
  125. // log the user into the application, which will store their user ID
  126. // in the session for subsequent requests.
  127. if (is_null($user)) return false;
  128. static::login($user, $remember);
  129. return true;
  130. }
  131. /**
  132. * Log a user into the application.
  133. *
  134. * <code>
  135. * // Login the user with an ID of 15
  136. * Auth::login(15);
  137. *
  138. * // Login a user by passing a user object
  139. * Auth::login($user);
  140. *
  141. * // Login a user and set a "remember me" cookie
  142. * Auth::login($user, true);
  143. * </code>
  144. *
  145. * @param object|int $user
  146. * @param bool $remember
  147. * @return void
  148. */
  149. public static function login($user, $remember = false)
  150. {
  151. $id = (is_object($user)) ? $user->id : (int) $user;
  152. if ($remember) static::remember($id);
  153. Session::put(Auth::user_key, $id);
  154. }
  155. /**
  156. * Set a cookie so that the user is "remembered".
  157. *
  158. * @param string $id
  159. * @return void
  160. */
  161. protected static function remember($id)
  162. {
  163. $recaller = Crypter::encrypt($id.'|'.Str::random(40));
  164. // This method assumes the "remember me" cookie should have the same
  165. // configuration as the session cookie. Since this cookie, like the
  166. // session cookie, should be kept very secure, it's probably safe.
  167. // to assume the cookie settings are the same.
  168. $config = Config::get('session');
  169. extract($config, EXTR_SKIP);
  170. Cookie::forever(Auth::remember_key, $recaller, $path, $domain, $secure);
  171. }
  172. /**
  173. * Log the current user out of the application.
  174. *
  175. * @return void
  176. */
  177. public static function logout()
  178. {
  179. // We will call the "logout" closure first, which gives the developer
  180. // the chance to do any clean-up or before the user is logged out of
  181. // the application. No action is taken by default.
  182. call_user_func(Config::get('auth.logout'), static::user());
  183. static::$user = null;
  184. $config = Config::get('session');
  185. extract($config, EXTR_SKIP);
  186. // When forgetting the cookie, we need to also pass in the path and
  187. // domain that would have been used when the cookie was originally
  188. // set by the framework, otherwise it will not be deleted.
  189. Cookie::forget(Auth::remember_key, $path, $domain, $secure);
  190. Session::forget(Auth::user_key);
  191. }
  192. }