Skip to content

[9.x] Adds Eloquent User Provider query handler #44226

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

Merged
merged 3 commits into from
Sep 21, 2022
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
36 changes: 35 additions & 1 deletion src/Illuminate/Auth/EloquentUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ class EloquentUserProvider implements UserProvider
*/
protected $model;

/**
* The callback that may modify the user retrieval queries.
*
* @var (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null
*/
protected $queryCallback;

/**
* Create a new database user provider.
*
Expand Down Expand Up @@ -155,9 +162,13 @@ public function validateCredentials(UserContract $user, array $credentials)
*/
protected function newModelQuery($model = null)
{
return is_null($model)
$query = is_null($model)
? $this->createModel()->newQuery()
: $model->newQuery();

with($query, $this->queryCallback);

return $query;
}

/**
Expand Down Expand Up @@ -217,4 +228,27 @@ public function setModel($model)

return $this;
}

/**
* Get the callback that modifies the query before retrieving users.
*
* @return \Closure|null
*/
public function getQueryCallback()
{
return $this->queryCallback;
}

/**
* Sets the callback to modify the query before retrieving users.
*
* @param (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null $queryCallback
* @return $this
*/
public function withQuery($queryCallback = null)
{
$this->queryCallback = $queryCallback;

return $this;
}
}
22 changes: 22 additions & 0 deletions tests/Auth/AuthEloquentUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ public function testModelsCanBeCreated()
$this->assertInstanceOf(EloquentProviderUserStub::class, $model);
}

public function testRegistersQueryHandler()
{
$callback = function ($builder) {
$builder->whereIn('group', ['one', 'two']);
};

$provider = $this->getProviderMock();
$mock = m::mock(stdClass::class);
$mock->shouldReceive('newQuery')->once()->andReturn($mock);
$mock->shouldReceive('where')->once()->with('username', 'dayle');
$mock->shouldReceive('whereIn')->once()->with('group', ['one', 'two']);
$mock->shouldReceive('first')->once()->andReturn('bar');
$provider->expects($this->once())->method('createModel')->willReturn($mock);
$provider->withQuery($callback);
$user = $provider->retrieveByCredentials([function ($builder) {
$builder->where('username', 'dayle');
}]);

$this->assertSame('bar', $user);
$this->assertSame($callback, $provider->getQueryCallback());
}

protected function getProviderMock()
{
$hasher = m::mock(Hasher::class);
Expand Down