Skip to content

Commit

Permalink
Merge branch '5.8' of https://github.com/epalmans/framework into epal…
Browse files Browse the repository at this point in the history
…mans-5.8
  • Loading branch information
taylorotwell committed Mar 3, 2019
2 parents 0fbd795 + b896cce commit accb93b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
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

0 comments on commit accb93b

Please sign in to comment.