Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.8] Method on EloquentUserProvider to create query builder instance #27734

Merged
merged 4 commits into from
Mar 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/Illuminate/Auth/EloquentUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function retrieveById($identifier)
{
$model = $this->createModel();

return $model->newQuery()
return $this->modelQuery($model)
->where($model->getAuthIdentifierName(), $identifier)
->first();
}
Expand All @@ -63,15 +63,15 @@ public function retrieveByToken($identifier, $token)
{
$model = $this->createModel();

$model = $model->where($model->getAuthIdentifierName(), $identifier)->first();
$retrievedModel = $this->modelQuery($model)->where($model->getAuthIdentifierName(), $identifier)->first();

if (! $model) {
if (! $retrievedModel) {
return null;
}

$rememberToken = $model->getRememberToken();
$rememberToken = $retrievedModel->getRememberToken();

return $rememberToken && hash_equals($rememberToken, $token) ? $model : null;
return $rememberToken && hash_equals($rememberToken, $token) ? $retrievedModel : null;
}

/**
Expand Down Expand Up @@ -111,7 +111,7 @@ public function retrieveByCredentials(array $credentials)
// First we will add each credential element to the query as a where clause.
// Then we can execute the query and, if we found a user, return it in a
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();
$query = $this->modelQuery();

foreach ($credentials as $key => $value) {
if (Str::contains($key, 'password')) {
Expand Down Expand Up @@ -154,6 +154,21 @@ public function createModel()
return new $class;
}

/**
* Get a new query builder for the model instance.
*
* @param \Illuminate\Database\Eloquent\Model|null $model
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function modelQuery($model = null)
{
if (is_null($model)) {
$model = $this->createModel();
}

return $model->newQuery();
}

/**
* Gets the hasher implementation.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/Auth/AuthEloquentUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testRetrieveByTokenReturnsUser()

$provider = $this->getProviderMock();
$mock = m::mock(stdClass::class);
$mock->shouldReceive('newQuery')->once()->andReturn($mock);
$mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
$mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
$mock->shouldReceive('first')->once()->andReturn($mockUser);
Expand All @@ -50,6 +51,7 @@ public function testRetrieveTokenWithBadIdentifierReturnsNull()
{
$provider = $this->getProviderMock();
$mock = m::mock(stdClass::class);
$mock->shouldReceive('newQuery')->once()->andReturn($mock);
$mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
$mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
$mock->shouldReceive('first')->once()->andReturn(null);
Expand All @@ -66,6 +68,7 @@ public function testRetrieveByBadTokenReturnsNull()

$provider = $this->getProviderMock();
$mock = m::mock(stdClass::class);
$mock->shouldReceive('newQuery')->once()->andReturn($mock);
$mock->shouldReceive('getAuthIdentifierName')->once()->andReturn('id');
$mock->shouldReceive('where')->once()->with('id', 1)->andReturn($mock);
$mock->shouldReceive('first')->once()->andReturn($mockUser);
Expand Down