Skip to content

Commit

Permalink
Merge branch 'refactor_user_providing_in_guards' of https://github.co…
Browse files Browse the repository at this point in the history
…m/evsign/framework into evsign-refactor_user_providing_in_guards
  • Loading branch information
taylorotwell committed Apr 24, 2017
2 parents 4c13c7e + 7503a9a commit fc4b7b9
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 35 deletions.
7 changes: 4 additions & 3 deletions src/Illuminate/Auth/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Auth;

use Closure;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use Illuminate\Contracts\Auth\Factory as FactoryContract;

Expand Down Expand Up @@ -120,7 +121,7 @@ protected function callCustomCreator($name, array $config)
*/
public function createSessionDriver($name, $config)
{
$provider = $this->createUserProvider($config['provider']);
$provider = $this->createUserProvider(Arr::get($config, 'provider'));

$guard = new SessionGuard($name, $provider, $this->app['session.store']);

Expand Down Expand Up @@ -155,7 +156,7 @@ public function createTokenDriver($name, $config)
// that takes an API token field from the request and matches it to the
// user in the database or another persistence layer where users are.
$guard = new TokenGuard(
$this->createUserProvider($config['provider']),
$this->createUserProvider(Arr::get($config, 'provider')),
$this->app['request']
);

Expand Down Expand Up @@ -223,7 +224,7 @@ public function setDefaultDriver($name)
public function viaRequest($driver, callable $callback)
{
return $this->extend($driver, function () use ($callback) {
$guard = new RequestGuard($callback, $this->app['request']);
$guard = new RequestGuard($callback, $this->app['request'], $this->createUserProvider());

$this->app->refresh('request', $guard, 'setRequest');

Expand Down
26 changes: 20 additions & 6 deletions src/Illuminate/Auth/CreatesUserProviders.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Auth;

use Illuminate\Support\Arr;
use InvalidArgumentException;

trait CreatesUserProviders
Expand All @@ -16,31 +17,44 @@ trait CreatesUserProviders
/**
* Create the user provider implementation for the driver.
*
* @param string $provider
* @param string|null $provider
* @return \Illuminate\Contracts\Auth\UserProvider
*
* @throws \InvalidArgumentException
*/
public function createUserProvider($provider)
public function createUserProvider($provider = null)
{
$provider = $provider ?: $this->getDefaultUserProvider();

$config = $this->app['config']['auth.providers.'.$provider];
$driver = Arr::get($config, 'driver');

if (isset($this->customProviderCreators[$config['driver']])) {
if (isset($this->customProviderCreators[$driver])) {
return call_user_func(
$this->customProviderCreators[$config['driver']], $this->app, $config
$this->customProviderCreators[$driver], $this->app, $config
);
}

switch ($config['driver']) {
switch ($driver) {
case 'database':
return $this->createDatabaseProvider($config);
case 'eloquent':
return $this->createEloquentProvider($config);
default:
throw new InvalidArgumentException("Authentication user provider [{$config['driver']}] is not defined.");
throw new InvalidArgumentException("Authentication user provider [{$driver}] is not defined.");
}
}

/**
* Get the default user provider name.
*
* @return string
*/
public function getDefaultUserProvider()
{
return $this->app['config']['auth.defaults.provider'];
}

/**
* Create an instance of the database user provider.
*
Expand Down
22 changes: 22 additions & 0 deletions src/Illuminate/Auth/GuardHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Auth;

use Illuminate\Contracts\Auth\UserProvider;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

/**
Expand Down Expand Up @@ -83,4 +84,25 @@ public function setUser(AuthenticatableContract $user)

return $this;
}

/**
* Get the user provider used by the guard.
*
* @return \Illuminate\Contracts\Auth\UserProvider
*/
public function getProvider()
{
return $this->provider;
}

/**
* Set the user provider used by the guard.
*
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @return void
*/
public function setProvider(UserProvider $provider)
{
$this->provider = $provider;
}
}
3 changes: 2 additions & 1 deletion src/Illuminate/Auth/Passwords/PasswordBrokerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Auth\Passwords;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Contracts\Auth\PasswordBrokerFactory as FactoryContract;
Expand Down Expand Up @@ -69,7 +70,7 @@ protected function resolve($name)
// aggregate service of sorts providing a convenient interface for resets.
return new PasswordBroker(
$this->createTokenRepository($config),
$this->app['auth']->createUserProvider($config['provider'])
$this->app['auth']->createUserProvider(Arr::get($config, 'provider'))
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/Illuminate/Auth/RequestGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\UserProvider;

class RequestGuard implements Guard
{
Expand All @@ -28,12 +29,14 @@ class RequestGuard implements Guard
*
* @param callable $callback
* @param \Illuminate\Http\Request $request
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @return void
*/
public function __construct(callable $callback, Request $request)
public function __construct(callable $callback, Request $request, UserProvider $provider)
{
$this->request = $request;
$this->callback = $callback;
$this->provider = $provider;
}

/**
Expand All @@ -51,7 +54,7 @@ public function user()
}

return $this->user = call_user_func(
$this->callback, $this->request
$this->callback, $this->request, $this->getProvider()
);
}

Expand All @@ -64,7 +67,7 @@ public function user()
public function validate(array $credentials = [])
{
return ! is_null((new static(
$this->callback, $credentials['request']
$this->callback, $credentials['request'], $this->getProvider()
))->user());
}

Expand Down
21 changes: 0 additions & 21 deletions src/Illuminate/Auth/SessionGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,27 +702,6 @@ public function getSession()
return $this->session;
}

/**
* Get the user provider used by the guard.
*
* @return \Illuminate\Contracts\Auth\UserProvider
*/
public function getProvider()
{
return $this->provider;
}

/**
* Set the user provider used by the guard.
*
* @param \Illuminate\Contracts\Auth\UserProvider $provider
* @return void
*/
public function setProvider(UserProvider $provider)
{
$this->provider = $provider;
}

/**
* Return the currently cached user.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/Auth/AuthenticateMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Auth\AuthManager;
use Illuminate\Auth\RequestGuard;
use Illuminate\Container\Container;
use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Config\Repository as Config;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\Middleware\Authenticate;
Expand Down Expand Up @@ -166,7 +167,7 @@ protected function createAuthDriver($authenticated)
{
return new RequestGuard(function () use ($authenticated) {
return $authenticated ? new stdClass : null;
}, m::mock(Request::class));
}, m::mock(Request::class), m::mock(EloquentUserProvider::class));
}

/**
Expand Down

0 comments on commit fc4b7b9

Please sign in to comment.