Skip to content

Commit

Permalink
possibility to specify default user provider and passing user provide…
Browse files Browse the repository at this point in the history
…r in request guard callback
  • Loading branch information
Alexandr Evsigneev committed Apr 19, 2017
1 parent 34654e2 commit e27bd8a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
7 changes: 4 additions & 3 deletions 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 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 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 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 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 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

0 comments on commit e27bd8a

Please sign in to comment.