auth.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace Laravel\Security;
  2. use Laravel\Str;
  3. use Laravel\Config;
  4. use Laravel\Cookie;
  5. use Laravel\Session;
  6. class Auth {
  7. /**
  8. * The current user of the application.
  9. *
  10. * @var object
  11. */
  12. protected static $user;
  13. /**
  14. * The key used when storing the user ID in the session.
  15. *
  16. * @var string
  17. */
  18. const user_key = 'laravel_user_id';
  19. /**
  20. * The key used when setting the "remember me" cookie.
  21. *
  22. * @var string
  23. */
  24. const remember_key = 'laravel_remember';
  25. /**
  26. * Determine if the user of the application is not logged in.
  27. *
  28. * This method is the inverse of the "check" method.
  29. *
  30. * @return bool
  31. */
  32. public static function guest()
  33. {
  34. return ! static::check();
  35. }
  36. /**
  37. * Determine if the user of the application is logged in.
  38. *
  39. * @return bool
  40. */
  41. public static function check()
  42. {
  43. return ! is_null(static::user());
  44. }
  45. /**
  46. * Get the current user of the application.
  47. *
  48. * This method will call the "user" closure in the auth configuration file.
  49. * If the user is not authenticated, null will be returned by the methd.
  50. *
  51. * If no user exists in the session, the method will check for a "remember me"
  52. * cookie and attempt to login the user based on the value of that cookie.
  53. *
  54. * <code>
  55. * // Get the current user of the application
  56. * $user = Auth::user();
  57. *
  58. * // Access a property on the current user of the application
  59. * $email = Auth::user()->email;
  60. * </code>
  61. *
  62. * @return object
  63. */
  64. public static function user()
  65. {
  66. if ( ! is_null(static::$user)) return static::$user;
  67. static::$user = call_user_func(Config::get('auth.user'), Session::get(Auth::user_key));
  68. if (is_null(static::$user) and ! is_null($cookie = Cookie::get(Auth::remember_key)))
  69. {
  70. static::$user = static::recall($cookie);
  71. }
  72. return static::$user;
  73. }
  74. /**
  75. * Attempt to login a user based on a long-lived "remember me" cookie.
  76. *
  77. * We should be able to trust the cookie is valid, since all cookies
  78. * set by Laravel include a fingerprint hash. So, the cookie should
  79. * be safe to use within this method.
  80. *
  81. * @param string $cookie
  82. * @return mixed
  83. */
  84. protected static function recall($cookie)
  85. {
  86. $cookie = explode('|', Crypter::decrypt($cookie));
  87. if ( ! is_null($user = call_user_func(Config::get('auth.user'), $cookie[0])))
  88. {
  89. static::login($user);
  90. return $user;
  91. }
  92. }
  93. /**
  94. * Attempt to log a user into the application.
  95. *
  96. * If the credentials are valid, the user will be logged into the application
  97. * and their user ID will be stored in the session via the "login" method.
  98. *
  99. * The user may also be "remembered", which will keep the user logged into the
  100. * application for one year or until they logout. The user is remembered via
  101. * an encrypted cookie.
  102. *
  103. * @param string $username
  104. * @param string $password
  105. * @param bool $remember
  106. * @return bool
  107. */
  108. public static function attempt($username, $password = null, $remember = false)
  109. {
  110. $config = Config::get('auth');
  111. $user = call_user_func($config['attempt'], $username, $password, $config);
  112. if ( ! is_null($user))
  113. {
  114. static::login($user, $remember);
  115. return true;
  116. }
  117. return false;
  118. }
  119. /**
  120. * Log a user into the application.
  121. *
  122. * @param object $user
  123. * @param bool $remember
  124. * @return void
  125. */
  126. public static function login($user, $remember = false)
  127. {
  128. static::$user = $user;
  129. if ($remember) static::remember($user->id);
  130. Session::put(Auth::user_key, $user->id);
  131. }
  132. /**
  133. * Set a cookie so that users are "remembered" and don't need to login.
  134. *
  135. * @param string $id
  136. * @return void
  137. */
  138. protected static function remember($id)
  139. {
  140. $cookie = Crypter::encrypt($id.'|'.Str::random(40));
  141. // This method assumes the "remember me" cookie should have the same
  142. // configuration as the session cookie. Since this cookie, like the
  143. // session cookie, should be kept very secure, it's probably safe
  144. // to assume the settings are the same.
  145. $config = Config::get('session');
  146. extract($config, EXTR_SKIP);
  147. Cookie::forever(Auth::remember_key, $cookie, $path, $domain, $secure);
  148. }
  149. /**
  150. * Log the current user out of the application.
  151. *
  152. * The "logout" closure in the authenciation configuration file will be
  153. * called. All authentication cookies will be deleted and the user ID
  154. * will be removed from the session.
  155. *
  156. * @return void
  157. */
  158. public static function logout()
  159. {
  160. call_user_func(Config::get('auth.logout'), static::user());
  161. static::$user = null;
  162. Cookie::forget(Auth::user_key);
  163. Cookie::forget(Auth::remember_key);
  164. Session::forget(Auth::user_key);
  165. }
  166. }