diff --git a/src/Illuminate/Auth/EloquentUserProvider.php b/src/Illuminate/Auth/EloquentUserProvider.php index 23b5b792cf1..1ab9b3cad02 100755 --- a/src/Illuminate/Auth/EloquentUserProvider.php +++ b/src/Illuminate/Auth/EloquentUserProvider.php @@ -47,7 +47,7 @@ public function retrieveById($identifier) { $model = $this->createModel(); - return $model->newQuery() + return $this->modelQuery($model) ->where($model->getAuthIdentifierName(), $identifier) ->first(); } @@ -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; } /** @@ -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')) { @@ -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. * diff --git a/tests/Auth/AuthEloquentUserProviderTest.php b/tests/Auth/AuthEloquentUserProviderTest.php index 4a01b8e1284..a8c7c24efa8 100755 --- a/tests/Auth/AuthEloquentUserProviderTest.php +++ b/tests/Auth/AuthEloquentUserProviderTest.php @@ -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); @@ -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); @@ -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);