auth.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. $recaller = explode('|', Crypter::decrypt($recaller));
  78. // We'll pass the ID that was stored in the cookie into the same user
  79. // Closure that is used by the "user" method. If the method returns
  80. // a user, we will log them into the application.
  81. $user = call_user_func(Config::get('auth.user'), $recaller[0]);
  82. if ( ! is_null($user))
  83. {
  84. static::login($user);
  85. return $user;
  86. }
  87. }
  88. /**
  89. * Attempt to log a user into the application.
  90. *
  91. * <code>
  92. * // Attempt to log a user into the application
  93. * $success = Auth::attempt('username', 'password');
  94. *
  95. * // Attempt to login a user and set the "remember me" cookie
  96. * Auth::attempt('username', 'password', true);
  97. * </code>
  98. *
  99. * @param string $username
  100. * @param string $password
  101. * @param bool $remember
  102. * @return bool
  103. */
  104. public static function attempt($username, $password = null, $remember = false)
  105. {
  106. $config = Config::get('auth');
  107. // When attempting to login the user, we will call the "attempt" closure
  108. // from the configuration file. This gives the developer the freedom to
  109. // authenticate based on the needs of their application, even allowing
  110. // the user of third-party providers.
  111. $user = call_user_func($config['attempt'], $username, $password);
  112. if (is_null($user)) return false;
  113. static::login($user, $remember);
  114. return true;
  115. }
  116. /**
  117. * Log a user into the application.
  118. *
  119. * <code>
  120. * // Login the user with an ID of 15
  121. * Auth::login(15);
  122. *
  123. * // Login a user by passing a user object
  124. * Auth::login($user);
  125. *
  126. * // Login a user and set a "remember me" cookie
  127. * Auth::login($user, true);
  128. * </code>
  129. *
  130. * @param object|int $user
  131. * @param bool $remember
  132. * @return void
  133. */
  134. public static function login($user, $remember = false)
  135. {
  136. $id = (is_object($user)) ? $user->id : (int) $user;
  137. if ($remember) static::remember($id);
  138. Session::put(Auth::user_key, $id);
  139. }
  140. /**
  141. * Set a cookie so that the user is "remembered".
  142. *
  143. * @param string $id
  144. * @return void
  145. */
  146. protected static function remember($id)
  147. {
  148. $recaller = Crypter::encrypt($id.'|'.Str::random(40));
  149. // This method assumes the "remember me" cookie should have the same
  150. // configuration as the session cookie. Since this cookie, like the
  151. // session cookie, should be kept very secure, it's probably safe.
  152. // to assume the cookie settings are the same.
  153. $config = Config::get('session');
  154. extract($config, EXTR_SKIP);
  155. $cookie = Config::get('auth.cookie');
  156. Cookie::forever($cookie, $recaller, $path, $domain, $secure);
  157. }
  158. /**
  159. * Log the current user out of the application.
  160. *
  161. * @return void
  162. */
  163. public static function logout()
  164. {
  165. // We will call the "logout" closure first, which gives the developer
  166. // the chance to do any clean-up or before the user is logged out of
  167. // the application. No action is taken by default.
  168. call_user_func(Config::get('auth.logout'), static::user());
  169. static::$user = null;
  170. $config = Config::get('session');
  171. extract($config, EXTR_SKIP);
  172. // When forgetting the cookie, we need to also pass in the path and
  173. // domain that would have been used when the cookie was originally
  174. // set by the framework, otherwise it will not be deleted.
  175. $cookie = Config::get('auth.cookie');
  176. Cookie::forget($cookie, $path, $domain, $secure);
  177. Session::forget(Auth::user_key);
  178. }
  179. }