Skip to content

Commit

Permalink
[5.6] Allow array/collections in Auth::attempt method (#24620)
Browse files Browse the repository at this point in the history
* Allow array/collections in Auth::attempt method

* style ci changes

* added use statement

* Minor changes

* style ci
  • Loading branch information
VinceG authored and taylorotwell committed Jun 20, 2018
1 parent c7a74b1 commit abf1e2c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Illuminate/Auth/DatabaseUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
if (Str::contains($key, 'password')) {
continue;
}

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

use Illuminate\Support\Str;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;

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

foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'password')) {
if (Str::contains($key, 'password')) {
continue;
}

if (is_array($value) || $value instanceof Arrayable) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Auth/AuthGuardTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ public function testBasicWithExtraConditions()
$guard->basic('email', ['active' => 1]);
}

public function testBasicWithExtraArrayConditions()
{
list($session, $provider, $request, $cookie) = $this->getMocks();
$guard = m::mock('Illuminate\Auth\SessionGuard[check,attempt]', ['default', $provider, $session]);
$guard->shouldReceive('check')->once()->andReturn(false);
$guard->shouldReceive('attempt')->once()->with(['email' => 'foo@bar.com', 'password' => 'secret', 'active' => 1, 'type' => [1, 2, 3]])->andReturn(true);
$request = \Symfony\Component\HttpFoundation\Request::create('/', 'GET', [], [], [], ['PHP_AUTH_USER' => 'foo@bar.com', 'PHP_AUTH_PW' => 'secret']);
$guard->setRequest($request);

$guard->basic('email', ['active' => 1, 'type' => [1, 2, 3]]);
}

public function testAttemptCallsRetrieveByCredentials()
{
$guard = $this->getGuard();
Expand Down

0 comments on commit abf1e2c

Please sign in to comment.