auth.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php namespace Laravel;
  2. class Auth {
  3. /**
  4. * The current user of the application.
  5. *
  6. * If no user is logged in, this will be NULL. Otherwise, it will contain the result
  7. * of the "by_id" closure in the authentication configuration file.
  8. *
  9. * Typically, the user should be accessed via the "user" method.
  10. *
  11. * @var object
  12. */
  13. public $user;
  14. /**
  15. * The session driver being used by the Auth instance.
  16. *
  17. * @var Session\Driver
  18. */
  19. protected $session;
  20. /**
  21. * The key used to store the user ID in the session.
  22. *
  23. * @var string
  24. */
  25. protected static $key = 'laravel_user_id';
  26. /**
  27. * Create a new Auth class instance.
  28. *
  29. * @param Session\Driver $session_driver
  30. * @return void
  31. */
  32. public function __construct(Session\Driver $driver)
  33. {
  34. $this->session = $driver;
  35. }
  36. /**
  37. * Determine if the current user of the application is authenticated.
  38. *
  39. * @see login()
  40. * @return bool
  41. */
  42. public function check()
  43. {
  44. return ! is_null($this->user());
  45. }
  46. /**
  47. * Get the current user of the application.
  48. *
  49. * To retrieve the user, the user ID stored in the session will be passed to
  50. * the "by_id" closure in the authentication configuration file. The result
  51. * of the closure will be cached and returned.
  52. *
  53. * <code>
  54. * $email = Auth::user()->email;
  55. * </code>
  56. *
  57. * @return object
  58. */
  59. public function user()
  60. {
  61. if (is_null($this->user) and $this->session->has(static::$key))
  62. {
  63. $this->user = call_user_func(Config::get('auth.by_id'), $this->session->get(static::$key));
  64. }
  65. return $this->user;
  66. }
  67. /**
  68. * Attempt to log a user into your application.
  69. *
  70. * If the user credentials are valid. The user's ID will be stored in the session and the
  71. * user will be considered "logged in" on subsequent requests to the application.
  72. *
  73. * The password passed to the method should be plain text, as it will be hashed
  74. * by the Hash class when authenticating.
  75. *
  76. * <code>
  77. * if (Auth::login('email@example.com', 'password'))
  78. * {
  79. * // The credentials are valid and the user is now logged in.
  80. * }
  81. * </code>
  82. *
  83. * @param string $username
  84. * @param string $password
  85. * @return bool
  86. */
  87. public function login($username, $password)
  88. {
  89. if ( ! is_null($user = call_user_func(Config::get('auth.by_username'), $username)))
  90. {
  91. if (Hash::check($password, $user->password))
  92. {
  93. $this->remember($user);
  94. return true;
  95. }
  96. }
  97. return false;
  98. }
  99. /**
  100. * Log a user into your application.
  101. *
  102. * The user's ID will be stored in the session and the user will be considered
  103. * "logged in" on subsequent requests to your application. This method is called
  104. * by the login method after determining a user's credentials are valid.
  105. *
  106. * Note: The user given to this method should be an object having an "id" property.
  107. *
  108. * @param object $user
  109. * @return void
  110. */
  111. public function remember($user)
  112. {
  113. $this->user = $user;
  114. $this->session->put(static::$key, $user->id);
  115. }
  116. /**
  117. * Log the user out of your application.
  118. *
  119. * The user ID will be removed from the session and the user will no longer
  120. * be considered logged in on subsequent requests to your application.
  121. *
  122. * @return void
  123. */
  124. public function logout()
  125. {
  126. $this->user = null;
  127. $this->session->forget(static::$key);
  128. }
  129. /**
  130. * Pass all other methods to a generic Auth instance.
  131. *
  132. * This provides a convenient API for working with the default Auth configuration.
  133. *
  134. * <code>
  135. * // Get the current user of your application
  136. * $user = Auth::user();
  137. *
  138. * // Equivalent call using make method
  139. * $user = Auth::make()->user();
  140. * </code>
  141. */
  142. public static function __callStatic($method, $parameters)
  143. {
  144. return call_user_func_array(array(new static(Session::driver()), $method), $parameters);
  145. }
  146. }