authenticator.php 3.2 KB

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