DatabaseUserProvider.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace Illuminate\Auth;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Contracts\Auth\UserProvider;
  5. use Illuminate\Database\ConnectionInterface;
  6. use Illuminate\Contracts\Hashing\Hasher as HasherContract;
  7. use Illuminate\Contracts\Auth\Authenticatable as UserContract;
  8. class DatabaseUserProvider implements UserProvider
  9. {
  10. /**
  11. * The active database connection.
  12. *
  13. * @var \Illuminate\Database\ConnectionInterface
  14. */
  15. protected $conn;
  16. /**
  17. * The hasher implementation.
  18. *
  19. * @var \Illuminate\Contracts\Hashing\Hasher
  20. */
  21. protected $hasher;
  22. /**
  23. * The table containing the users.
  24. *
  25. * @var string
  26. */
  27. protected $table;
  28. /**
  29. * Create a new database user provider.
  30. *
  31. * @param \Illuminate\Database\ConnectionInterface $conn
  32. * @param \Illuminate\Contracts\Hashing\Hasher $hasher
  33. * @param string $table
  34. * @return void
  35. */
  36. public function __construct(ConnectionInterface $conn, HasherContract $hasher, $table)
  37. {
  38. $this->conn = $conn;
  39. $this->table = $table;
  40. $this->hasher = $hasher;
  41. }
  42. /**
  43. * Retrieve a user by their unique identifier.
  44. *
  45. * @param mixed $identifier
  46. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  47. */
  48. public function retrieveById($identifier)
  49. {
  50. $user = $this->conn->table($this->table)->find($identifier);
  51. return $this->getGenericUser($user);
  52. }
  53. /**
  54. * Retrieve a user by their unique identifier and "remember me" token.
  55. *
  56. * @param mixed $identifier
  57. * @param string $token
  58. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  59. */
  60. public function retrieveByToken($identifier, $token)
  61. {
  62. $user = $this->getGenericUser(
  63. $this->conn->table($this->table)->find($identifier)
  64. );
  65. return $user && $user->getRememberToken() && hash_equals($user->getRememberToken(), $token)
  66. ? $user : null;
  67. }
  68. /**
  69. * Update the "remember me" token for the given user in storage.
  70. *
  71. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  72. * @param string $token
  73. * @return void
  74. */
  75. public function updateRememberToken(UserContract $user, $token)
  76. {
  77. $this->conn->table($this->table)
  78. ->where($user->getAuthIdentifierName(), $user->getAuthIdentifier())
  79. ->update([$user->getRememberTokenName() => $token]);
  80. }
  81. /**
  82. * Retrieve a user by the given credentials.
  83. *
  84. * @param array $credentials
  85. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  86. */
  87. public function retrieveByCredentials(array $credentials)
  88. {
  89. if (empty($credentials) ||
  90. (count($credentials) === 1 &&
  91. array_key_exists('password', $credentials))) {
  92. return;
  93. }
  94. // First we will add each credential element to the query as a where clause.
  95. // Then we can execute the query and, if we found a user, return it in a
  96. // generic "user" object that will be utilized by the Guard instances.
  97. $query = $this->conn->table($this->table);
  98. foreach ($credentials as $key => $value) {
  99. if (! Str::contains($key, 'password')) {
  100. $query->where($key, $value);
  101. }
  102. }
  103. // Now we are ready to execute the query to see if we have an user matching
  104. // the given credentials. If not, we will just return nulls and indicate
  105. // that there are no matching users for these given credential arrays.
  106. $user = $query->first();
  107. return $this->getGenericUser($user);
  108. }
  109. /**
  110. * Get the generic user.
  111. *
  112. * @param mixed $user
  113. * @return \Illuminate\Auth\GenericUser|null
  114. */
  115. protected function getGenericUser($user)
  116. {
  117. if (! is_null($user)) {
  118. return new GenericUser((array) $user);
  119. }
  120. }
  121. /**
  122. * Validate a user against the given credentials.
  123. *
  124. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  125. * @param array $credentials
  126. * @return bool
  127. */
  128. public function validateCredentials(UserContract $user, array $credentials)
  129. {
  130. return $this->hasher->check(
  131. $credentials['password'], $user->getAuthPassword()
  132. );
  133. }
  134. }