Skip to content
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

[5.6] Allow array/collections in Auth::attempt method #24620

Merged
merged 5 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 12 additions & 1 deletion src/Illuminate/Auth/DatabaseUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Auth;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
Expand Down Expand Up @@ -110,7 +111,17 @@ 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 ($value instanceof Arrayable || $value instanceof Collection) {
$value = $value->toArray();
VinceG marked this conversation as resolved.
Show resolved Hide resolved
}

if (is_array($value)) {
$query->whereIn($key, $value);
} else {
$query->where($key, $value);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/Illuminate/Auth/EloquentUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Auth;

use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
Expand Down Expand Up @@ -113,7 +114,17 @@ 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 ($value instanceof Arrayable || $value instanceof Collection) {
VinceG marked this conversation as resolved.
Show resolved Hide resolved
$value = $value->toArray();
}

if (is_array($value)) {
$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