EloquentUserProvider.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace Illuminate\Auth;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Contracts\Auth\UserProvider;
  5. use Illuminate\Contracts\Hashing\Hasher as HasherContract;
  6. use Illuminate\Contracts\Auth\Authenticatable as UserContract;
  7. class EloquentUserProvider implements UserProvider
  8. {
  9. /**
  10. * The hasher implementation.
  11. *
  12. * @var \Illuminate\Contracts\Hashing\Hasher
  13. */
  14. protected $hasher;
  15. /**
  16. * The Eloquent user model.
  17. *
  18. * @var string
  19. */
  20. protected $model;
  21. /**
  22. * Create a new database user provider.
  23. *
  24. * @param \Illuminate\Contracts\Hashing\Hasher $hasher
  25. * @param string $model
  26. * @return void
  27. */
  28. public function __construct(HasherContract $hasher, $model)
  29. {
  30. $this->model = $model;
  31. $this->hasher = $hasher;
  32. }
  33. /**
  34. * Retrieve a user by their unique identifier.
  35. *
  36. * @param mixed $identifier
  37. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  38. */
  39. public function retrieveById($identifier)
  40. {
  41. $model = $this->createModel();
  42. return $model->newQuery()
  43. ->where($model->getAuthIdentifierName(), $identifier)
  44. ->first();
  45. }
  46. /**
  47. * Retrieve a user by their unique identifier and "remember me" token.
  48. *
  49. * @param mixed $identifier
  50. * @param string $token
  51. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  52. */
  53. public function retrieveByToken($identifier, $token)
  54. {
  55. $model = $this->createModel();
  56. $model = $model->where($model->getAuthIdentifierName(), $identifier)->first();
  57. if (! $model) {
  58. return null;
  59. }
  60. $rememberToken = $model->getRememberToken();
  61. return $rememberToken && hash_equals($rememberToken, $token) ? $model : null;
  62. }
  63. /**
  64. * Update the "remember me" token for the given user in storage.
  65. *
  66. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  67. * @param string $token
  68. * @return void
  69. */
  70. public function updateRememberToken(UserContract $user, $token)
  71. {
  72. $user->setRememberToken($token);
  73. $timestamps = $user->timestamps;
  74. $user->timestamps = false;
  75. $user->save();
  76. $user->timestamps = $timestamps;
  77. }
  78. /**
  79. * Retrieve a user by the given credentials.
  80. *
  81. * @param array $credentials
  82. * @return \Illuminate\Contracts\Auth\Authenticatable|null
  83. */
  84. public function retrieveByCredentials(array $credentials)
  85. {
  86. if (empty($credentials) ||
  87. (count($credentials) === 1 &&
  88. array_key_exists('password', $credentials))) {
  89. return;
  90. }
  91. // First we will add each credential element to the query as a where clause.
  92. // Then we can execute the query and, if we found a user, return it in a
  93. // Eloquent User "model" that will be utilized by the Guard instances.
  94. $query = $this->createModel()->newQuery();
  95. foreach ($credentials as $key => $value) {
  96. if (! Str::contains($key, 'password')) {
  97. $query->where($key, $value);
  98. }
  99. }
  100. return $query->first();
  101. }
  102. /**
  103. * Validate a user against the given credentials.
  104. *
  105. * @param \Illuminate\Contracts\Auth\Authenticatable $user
  106. * @param array $credentials
  107. * @return bool
  108. */
  109. public function validateCredentials(UserContract $user, array $credentials)
  110. {
  111. $plain = $credentials['password'];
  112. return $this->hasher->check($plain, $user->getAuthPassword());
  113. }
  114. /**
  115. * Create a new instance of the model.
  116. *
  117. * @return \Illuminate\Database\Eloquent\Model
  118. */
  119. public function createModel()
  120. {
  121. $class = '\\'.ltrim($this->model, '\\');
  122. return new $class;
  123. }
  124. /**
  125. * Gets the hasher implementation.
  126. *
  127. * @return \Illuminate\Contracts\Hashing\Hasher
  128. */
  129. public function getHasher()
  130. {
  131. return $this->hasher;
  132. }
  133. /**
  134. * Sets the hasher implementation.
  135. *
  136. * @param \Illuminate\Contracts\Hashing\Hasher $hasher
  137. * @return $this
  138. */
  139. public function setHasher(HasherContract $hasher)
  140. {
  141. $this->hasher = $hasher;
  142. return $this;
  143. }
  144. /**
  145. * Gets the name of the Eloquent user model.
  146. *
  147. * @return string
  148. */
  149. public function getModel()
  150. {
  151. return $this->model;
  152. }
  153. /**
  154. * Sets the name of the Eloquent user model.
  155. *
  156. * @param string $model
  157. * @return $this
  158. */
  159. public function setModel($model)
  160. {
  161. $this->model = $model;
  162. return $this;
  163. }
  164. }