auth.php 4.5 KB

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