authenticator.php 3.8 KB

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