auth.php 4.3 KB

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