Skip to content

Commit

Permalink
[8.x] Auth: Allows to use a callback in credentials array (#39420)
Browse files Browse the repository at this point in the history
* Allows to use a callback on auth using a credentials array.

* Replaced callable check for Closure.
  • Loading branch information
DarkGhostHunter authored Nov 1, 2021
1 parent 44f9749 commit c0b7719
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Illuminate/Auth/DatabaseUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Auth;

use Closure;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
Expand Down Expand Up @@ -117,6 +118,8 @@ public function retrieveByCredentials(array $credentials)

if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} elseif ($value instanceof Closure) {
$value($query);
} else {
$query->where($key, $value);
}
Expand Down
3 changes: 3 additions & 0 deletions src/Illuminate/Auth/EloquentUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Auth;

use Closure;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
Expand Down Expand Up @@ -123,6 +124,8 @@ public function retrieveByCredentials(array $credentials)

if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} elseif ($value instanceof Closure) {
$value($query);
} else {
$query->where($key, $value);
}
Expand Down
20 changes: 20 additions & 0 deletions tests/Auth/AuthDatabaseUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ public function testRetrieveByCredentialsReturnsUserWhenUserIsFound()
$this->assertSame('taylor', $user->name);
}

public function testRetrieveByCredentialsAcceptsCallback()
{
$conn = m::mock(Connection::class);
$conn->shouldReceive('table')->once()->with('foo')->andReturn($conn);
$conn->shouldReceive('where')->once()->with('username', 'dayle');
$conn->shouldReceive('whereIn')->once()->with('group', ['one', 'two']);
$conn->shouldReceive('first')->once()->andReturn(['id' => 1, 'name' => 'taylor']);
$hasher = m::mock(Hasher::class);
$provider = new DatabaseUserProvider($conn, $hasher, 'foo');

$user = $provider->retrieveByCredentials([function ($builder) {
$builder->where('username', 'dayle');
$builder->whereIn('group', ['one', 'two']);
}]);

$this->assertInstanceOf(GenericUser::class, $user);
$this->assertSame(1, $user->getAuthIdentifier());
$this->assertSame('taylor', $user->name);
}

public function testRetrieveByCredentialsReturnsNullWhenUserIsFound()
{
$conn = m::mock(Connection::class);
Expand Down
17 changes: 17 additions & 0 deletions tests/Auth/AuthEloquentUserProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,23 @@ public function testRetrieveByCredentialsReturnsUser()
$this->assertSame('bar', $user);
}

public function testRetrieveByCredentialsAcceptsCallback()
{
$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);
$user = $provider->retrieveByCredentials([function ($builder) {
$builder->where('username', 'dayle');
$builder->whereIn('group', ['one', 'two']);
}]);

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

public function testCredentialValidation()
{
$hasher = m::mock(Hasher::class);
Expand Down

0 comments on commit c0b7719

Please sign in to comment.