Skip to content

Commit c0b7719

Browse files
[8.x] Auth: Allows to use a callback in credentials array (#39420)
* Allows to use a callback on auth using a credentials array. * Replaced callable check for Closure.
1 parent 44f9749 commit c0b7719

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

src/Illuminate/Auth/DatabaseUserProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Auth;
44

5+
use Closure;
56
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
67
use Illuminate\Contracts\Auth\UserProvider;
78
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
@@ -117,6 +118,8 @@ public function retrieveByCredentials(array $credentials)
117118

118119
if (is_array($value) || $value instanceof Arrayable) {
119120
$query->whereIn($key, $value);
121+
} elseif ($value instanceof Closure) {
122+
$value($query);
120123
} else {
121124
$query->where($key, $value);
122125
}

src/Illuminate/Auth/EloquentUserProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Auth;
44

5+
use Closure;
56
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
67
use Illuminate\Contracts\Auth\UserProvider;
78
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
@@ -123,6 +124,8 @@ public function retrieveByCredentials(array $credentials)
123124

124125
if (is_array($value) || $value instanceof Arrayable) {
125126
$query->whereIn($key, $value);
127+
} elseif ($value instanceof Closure) {
128+
$value($query);
126129
} else {
127130
$query->where($key, $value);
128131
}

tests/Auth/AuthDatabaseUserProviderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,26 @@ public function testRetrieveByCredentialsReturnsUserWhenUserIsFound()
102102
$this->assertSame('taylor', $user->name);
103103
}
104104

105+
public function testRetrieveByCredentialsAcceptsCallback()
106+
{
107+
$conn = m::mock(Connection::class);
108+
$conn->shouldReceive('table')->once()->with('foo')->andReturn($conn);
109+
$conn->shouldReceive('where')->once()->with('username', 'dayle');
110+
$conn->shouldReceive('whereIn')->once()->with('group', ['one', 'two']);
111+
$conn->shouldReceive('first')->once()->andReturn(['id' => 1, 'name' => 'taylor']);
112+
$hasher = m::mock(Hasher::class);
113+
$provider = new DatabaseUserProvider($conn, $hasher, 'foo');
114+
115+
$user = $provider->retrieveByCredentials([function ($builder) {
116+
$builder->where('username', 'dayle');
117+
$builder->whereIn('group', ['one', 'two']);
118+
}]);
119+
120+
$this->assertInstanceOf(GenericUser::class, $user);
121+
$this->assertSame(1, $user->getAuthIdentifier());
122+
$this->assertSame('taylor', $user->name);
123+
}
124+
105125
public function testRetrieveByCredentialsReturnsNullWhenUserIsFound()
106126
{
107127
$conn = m::mock(Connection::class);

tests/Auth/AuthEloquentUserProviderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ public function testRetrieveByCredentialsReturnsUser()
100100
$this->assertSame('bar', $user);
101101
}
102102

103+
public function testRetrieveByCredentialsAcceptsCallback()
104+
{
105+
$provider = $this->getProviderMock();
106+
$mock = m::mock(stdClass::class);
107+
$mock->shouldReceive('newQuery')->once()->andReturn($mock);
108+
$mock->shouldReceive('where')->once()->with('username', 'dayle');
109+
$mock->shouldReceive('whereIn')->once()->with('group', ['one', 'two']);
110+
$mock->shouldReceive('first')->once()->andReturn('bar');
111+
$provider->expects($this->once())->method('createModel')->willReturn($mock);
112+
$user = $provider->retrieveByCredentials([function ($builder) {
113+
$builder->where('username', 'dayle');
114+
$builder->whereIn('group', ['one', 'two']);
115+
}]);
116+
117+
$this->assertSame('bar', $user);
118+
}
119+
103120
public function testCredentialValidation()
104121
{
105122
$hasher = m::mock(Hasher::class);

0 commit comments

Comments
 (0)