auth.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 the user was not found in the database, but a "remember me" cookie
  66. // exists, we will attempt to recall the user based on the cookie value.
  67. // Since all cookies contain a fingerprint hash verifying that the have
  68. // not been modified on the client, we should be able to trust it.
  69. $recaller = Cookie::get(Auth::remember_key);
  70. if (is_null(static::$user) and ! is_null($recaller))
  71. {
  72. static::$user = static::recall($recaller);
  73. }
  74. return static::$user;
  75. }
  76. /**
  77. * Attempt to login a user based on a long-lived "remember me" cookie.
  78. *
  79. * @param string $recaller
  80. * @return mixed
  81. */
  82. protected static function recall($recaller)
  83. {
  84. // When the "remember me" cookie is stored, it is encrypted and contains the
  85. // user's ID and a long, random string. The ID and string are separated by
  86. // a pipe character. Since we exploded the decrypted string, we can just
  87. // pass the first item in the array to the user Closure.
  88. $recaller = explode('|', Crypter::decrypt($recaller));
  89. if ( ! is_null($user = call_user_func(Config::get('auth.user'), $recaller[0])))
  90. {
  91. static::login($user);
  92. return $user;
  93. }
  94. }
  95. /**
  96. * Attempt to log a user into the application.
  97. *
  98. * If the credentials are valid, the user will be logged into the application
  99. * and their user ID will be stored in the session via the "login" method.
  100. *
  101. * The user may also be "remembered", which will keep the user logged into the
  102. * application for one year or until they logout. The user is remembered via
  103. * an encrypted cookie.
  104. *
  105. * @param string $username
  106. * @param string $password
  107. * @param bool $remember
  108. * @return bool
  109. */
  110. public static function attempt($username, $password = null, $remember = false)
  111. {
  112. $config = Config::get('auth');
  113. $user = call_user_func($config['attempt'], $username, $password, $config);
  114. if ( ! is_null($user))
  115. {
  116. static::login($user, $remember);
  117. return true;
  118. }
  119. return false;
  120. }
  121. /**
  122. * Log a user into the application.
  123. *
  124. * An object representing the user or an integer user ID may be given to the method.
  125. * If an object is given, the object must have an "id" property containing the user
  126. * ID as it is stored in the database.
  127. *
  128. * <code>
  129. * // Login a user by passing a user object
  130. * Auth::login($user);
  131. *
  132. * // Login the user with an ID of 15
  133. * Auth::login(15);
  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. IoC::core('session')->put(Auth::user_key, $id);
  148. }
  149. /**
  150. * Set a cookie so that users are "remembered" and don't need to login.
  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 settings are the same.
  162. $config = Config::get('session');
  163. extract($config, EXTR_SKIP);
  164. Cookie::forever(Auth::remember_key, $recaller, $path, $domain, $secure);
  165. }
  166. /**
  167. * Log the current user out of the application.
  168. *
  169. * The "logout" closure in the authenciation configuration file will be
  170. * called. All authentication cookies will be deleted and the user ID
  171. * will be removed from the session.
  172. *
  173. * @return void
  174. */
  175. public static function logout()
  176. {
  177. call_user_func(Config::get('auth.logout'), static::user());
  178. static::$user = null;
  179. Cookie::forget(Auth::user_key);
  180. Cookie::forget(Auth::remember_key);
  181. IoC::core('session')->forget(Auth::user_key);
  182. }
  183. }