From e27bd8a8afbd1e6db2918714ae30ab85f5867191 Mon Sep 17 00:00:00 2001 From: Alexandr Evsigneev Date: Tue, 18 Apr 2017 22:41:10 +0300 Subject: [PATCH] possibility to specify default user provider and passing user provider in request guard callback --- AuthManager.php | 7 ++++--- CreatesUserProviders.php | 26 ++++++++++++++++++++------ GuardHelpers.php | 22 ++++++++++++++++++++++ Passwords/PasswordBrokerManager.php | 3 ++- RequestGuard.php | 9 ++++++--- SessionGuard.php | 21 --------------------- 6 files changed, 54 insertions(+), 34 deletions(-) diff --git a/AuthManager.php b/AuthManager.php index 984467259..d059a45f9 100755 --- a/AuthManager.php +++ b/AuthManager.php @@ -3,6 +3,7 @@ namespace Illuminate\Auth; use Closure; +use Illuminate\Support\Arr; use InvalidArgumentException; use Illuminate\Contracts\Auth\Factory as FactoryContract; @@ -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']); @@ -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'] ); @@ -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'); diff --git a/CreatesUserProviders.php b/CreatesUserProviders.php index 715dee046..72eca2160 100644 --- a/CreatesUserProviders.php +++ b/CreatesUserProviders.php @@ -2,6 +2,7 @@ namespace Illuminate\Auth; +use Illuminate\Support\Arr; use InvalidArgumentException; trait CreatesUserProviders @@ -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. * diff --git a/GuardHelpers.php b/GuardHelpers.php index 5a227964d..9a682c3dc 100644 --- a/GuardHelpers.php +++ b/GuardHelpers.php @@ -2,6 +2,7 @@ namespace Illuminate\Auth; +use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; /** @@ -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; + } } diff --git a/Passwords/PasswordBrokerManager.php b/Passwords/PasswordBrokerManager.php index 0f3f26283..95d08ce21 100644 --- a/Passwords/PasswordBrokerManager.php +++ b/Passwords/PasswordBrokerManager.php @@ -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; @@ -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')) ); } diff --git a/RequestGuard.php b/RequestGuard.php index 30b3a1d77..c5e59dcaa 100644 --- a/RequestGuard.php +++ b/RequestGuard.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Illuminate\Contracts\Auth\Guard; +use Illuminate\Contracts\Auth\UserProvider; class RequestGuard implements Guard { @@ -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; } /** @@ -51,7 +54,7 @@ public function user() } return $this->user = call_user_func( - $this->callback, $this->request + $this->callback, $this->request, $this->getProvider() ); } @@ -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()); } diff --git a/SessionGuard.php b/SessionGuard.php index 39d71669f..21c5c32da 100644 --- a/SessionGuard.php +++ b/SessionGuard.php @@ -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. *