Skip to content

Commit abf1e2c

Browse files
VinceGtaylorotwell
authored andcommitted
[5.6] Allow array/collections in Auth::attempt method (#24620)
* Allow array/collections in Auth::attempt method * style ci changes * added use statement * Minor changes * style ci
1 parent c7a74b1 commit abf1e2c

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

src/Illuminate/Auth/DatabaseUserProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Illuminate\Contracts\Auth\UserProvider;
7+
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Database\ConnectionInterface;
89
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
910
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
@@ -110,7 +111,13 @@ public function retrieveByCredentials(array $credentials)
110111
$query = $this->conn->table($this->table);
111112

112113
foreach ($credentials as $key => $value) {
113-
if (! Str::contains($key, 'password')) {
114+
if (Str::contains($key, 'password')) {
115+
continue;
116+
}
117+
118+
if (is_array($value) || $value instanceof Arrayable) {
119+
$query->whereIn($key, $value);
120+
} else {
114121
$query->where($key, $value);
115122
}
116123
}

src/Illuminate/Auth/EloquentUserProvider.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Illuminate\Contracts\Auth\UserProvider;
7+
use Illuminate\Contracts\Support\Arrayable;
78
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
89
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
910

@@ -113,7 +114,13 @@ public function retrieveByCredentials(array $credentials)
113114
$query = $this->createModel()->newQuery();
114115

115116
foreach ($credentials as $key => $value) {
116-
if (! Str::contains($key, 'password')) {
117+
if (Str::contains($key, 'password')) {
118+
continue;
119+
}
120+
121+
if (is_array($value) || $value instanceof Arrayable) {
122+
$query->whereIn($key, $value);
123+
} else {
117124
$query->where($key, $value);
118125
}
119126
}

tests/Auth/AuthGuardTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ public function testBasicWithExtraConditions()
6666
$guard->basic('email', ['active' => 1]);
6767
}
6868

69+
public function testBasicWithExtraArrayConditions()
70+
{
71+
list($session, $provider, $request, $cookie) = $this->getMocks();
72+
$guard = m::mock('Illuminate\Auth\SessionGuard[check,attempt]', ['default', $provider, $session]);
73+
$guard->shouldReceive('check')->once()->andReturn(false);
74+
$guard->shouldReceive('attempt')->once()->with(['email' => '[email protected]', 'password' => 'secret', 'active' => 1, 'type' => [1, 2, 3]])->andReturn(true);
75+
$request = \Symfony\Component\HttpFoundation\Request::create('/', 'GET', [], [], [], ['PHP_AUTH_USER' => '[email protected]', 'PHP_AUTH_PW' => 'secret']);
76+
$guard->setRequest($request);
77+
78+
$guard->basic('email', ['active' => 1, 'type' => [1, 2, 3]]);
79+
}
80+
6981
public function testAttemptCallsRetrieveByCredentials()
7082
{
7183
$guard = $this->getGuard();

0 commit comments

Comments
 (0)