auth.php 5.8 KB

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