Laravel 5 实施多个 Auth 驱动程序
Laravel 5 Implement multiple Auth drivers
剧情简介
我正在构建一个至少具有两级身份验证的系统,并且两者在数据库中都有单独的用户模型和表。在 google 上快速搜索,到目前为止唯一的解决方案是使用 MultiAuth 包,该包在 Auth
.
上插入多个驱动程序
我的目标
我正在尝试删除 Auth
,这非常简单。但我想 CustomerAuth
和 AdminAuth
根据 config/customerauth.php
和 config\adminauth.php
使用单独的配置文件
解决方案
我假设您有一个可用的包。在此示例中,我的供应商命名空间将只是:Example
- 所有代码片段都可以按照说明找到。
我将config/auth.php
复制到config/customerauth.php
并相应地修改了设置。
我编辑了 config/app.php
并将 Illuminate\Auth\AuthServiceProvider
替换为 Example\Auth\CustomerAuthServiceProvider
。
我编辑了 config/app.php
并将 Auth
别名替换为:
'CustomerAuth' => 'Example\Support\Facades\CustomerAuth',
然后我在包中实现了代码,例如 vendor/example/src/
。我从 ServiceProvider 开始:Example/Auth/CustomerAuthServiceProvider.php
<?php namespace Example\Auth;
use Illuminate\Auth\AuthServiceProvider;
use Example\Auth\CustomerAuthManager;
use Example\Auth\SiteGuard;
class CustomerAuthServiceProvider extends AuthServiceProvider
{
public function register()
{
$this->app->alias('customerauth', 'Example\Auth\CustomerAuthManager');
$this->app->alias('customerauth.driver', 'Example\Auth\SiteGuard');
$this->app->alias('customerauth.driver', 'Example\Contracts\Auth\SiteGuard');
parent::register();
}
protected function registerAuthenticator()
{
$this->app->singleton('customerauth', function ($app) {
$app['customerauth.loaded'] = true;
return new CustomerAuthManager($app);
});
$this->app->singleton('customerauth.driver', function ($app) {
return $app['customerauth']->driver();
});
}
protected function registerUserResolver()
{
$this->app->bind('Illuminate\Contracts\Auth\Authenticatable', function ($app) {
return $app['customerauth']->user();
});
}
protected function registerRequestRebindHandler()
{
$this->app->rebinding('request', function ($app, $request) {
$request->setUserResolver(function() use ($app) {
return $app['customerauth']->user();
});
});
}
}
然后我实现了:Example/Auth/CustomerAuthManager.php
<?php namespace Example\Auth;
use Illuminate\Auth\AuthManager;
use Illuminate\Auth\EloquentUserProvider;
use Example\Auth\SiteGuard as Guard;
class CustomerAuthManager extends AuthManager
{
protected function callCustomCreator($driver)
{
$custom = parent::callCustomCreator($driver);
if ($custom instanceof Guard) return $custom;
return new Guard($custom, $this->app['session.store']);
}
public function createDatabaseDriver()
{
$provider = $this->createDatabaseProvider();
return new Guard($provider, $this->app['session.store']);
}
protected function createDatabaseProvider()
{
$connection = $this->app['db']->connection();
$table = $this->app['config']['customerauth.table'];
return new DatabaseUserProvider($connection, $this->app['hash'], $table);
}
public function createEloquentDriver()
{
$provider = $this->createEloquentProvider();
return new Guard($provider, $this->app['session.store']);
}
protected function createEloquentProvider()
{
$model = $this->app['config']['customerauth.model'];
return new EloquentUserProvider($this->app['hash'], $model);
}
public function getDefaultDriver()
{
return $this->app['config']['customerauth.driver'];
}
public function setDefaultDriver($name)
{
$this->app['config']['customerauth.driver'] = $name;
}
}
然后我实现了 Example/Auth/SiteGuard.php
(注意实现的方法有一个额外的 site_ 定义,这对于其他 Auth 驱动程序应该是不同的):
<?php namespace Example\Auth;
use Illuminate\Auth\Guard;
class SiteGuard extends Guard
{
public function getName()
{
return 'login_site_'.md5(get_class($this));
}
public function getRecallerName()
{
return 'remember_site_'.md5(get_class($this));
}
}
然后我实施了Example/Contracts/Auth/SiteGuard.php
use Illuminate\Contracts\Auth\Guard;
interface SiteGuard extends Guard {}
我终于实现了门面; Example/Support/Facades/Auth/CustomerAuth.php
<?php namespace Example\Support\Facades;
class CustomerAuth extends Facade
{
protected static function getFacadeAccessor()
{
return 'customerauth';
}
}
快速更新,当尝试将这些自定义身份验证驱动程序与 phpunit 一起使用时,您可能会遇到以下错误:
Driver [CustomerAuth] not supported.
您还需要实现它,最简单的解决方案是重写 be
方法并创建一个与之类似的 trait
:
<?php namespace Example\Vendor\Testing;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
trait ApplicationTrait
{
public function be(UserContract $user, $driver = null)
{
$this->app['customerauth']->driver($driver)->setUser($user);
}
}
剧情简介
我正在构建一个至少具有两级身份验证的系统,并且两者在数据库中都有单独的用户模型和表。在 google 上快速搜索,到目前为止唯一的解决方案是使用 MultiAuth 包,该包在 Auth
.
我的目标
我正在尝试删除 Auth
,这非常简单。但我想 CustomerAuth
和 AdminAuth
根据 config/customerauth.php
和 config\adminauth.php
解决方案
我假设您有一个可用的包。在此示例中,我的供应商命名空间将只是:Example
- 所有代码片段都可以按照说明找到。
我将config/auth.php
复制到config/customerauth.php
并相应地修改了设置。
我编辑了 config/app.php
并将 Illuminate\Auth\AuthServiceProvider
替换为 Example\Auth\CustomerAuthServiceProvider
。
我编辑了 config/app.php
并将 Auth
别名替换为:
'CustomerAuth' => 'Example\Support\Facades\CustomerAuth',
然后我在包中实现了代码,例如 vendor/example/src/
。我从 ServiceProvider 开始:Example/Auth/CustomerAuthServiceProvider.php
<?php namespace Example\Auth;
use Illuminate\Auth\AuthServiceProvider;
use Example\Auth\CustomerAuthManager;
use Example\Auth\SiteGuard;
class CustomerAuthServiceProvider extends AuthServiceProvider
{
public function register()
{
$this->app->alias('customerauth', 'Example\Auth\CustomerAuthManager');
$this->app->alias('customerauth.driver', 'Example\Auth\SiteGuard');
$this->app->alias('customerauth.driver', 'Example\Contracts\Auth\SiteGuard');
parent::register();
}
protected function registerAuthenticator()
{
$this->app->singleton('customerauth', function ($app) {
$app['customerauth.loaded'] = true;
return new CustomerAuthManager($app);
});
$this->app->singleton('customerauth.driver', function ($app) {
return $app['customerauth']->driver();
});
}
protected function registerUserResolver()
{
$this->app->bind('Illuminate\Contracts\Auth\Authenticatable', function ($app) {
return $app['customerauth']->user();
});
}
protected function registerRequestRebindHandler()
{
$this->app->rebinding('request', function ($app, $request) {
$request->setUserResolver(function() use ($app) {
return $app['customerauth']->user();
});
});
}
}
然后我实现了:Example/Auth/CustomerAuthManager.php
<?php namespace Example\Auth;
use Illuminate\Auth\AuthManager;
use Illuminate\Auth\EloquentUserProvider;
use Example\Auth\SiteGuard as Guard;
class CustomerAuthManager extends AuthManager
{
protected function callCustomCreator($driver)
{
$custom = parent::callCustomCreator($driver);
if ($custom instanceof Guard) return $custom;
return new Guard($custom, $this->app['session.store']);
}
public function createDatabaseDriver()
{
$provider = $this->createDatabaseProvider();
return new Guard($provider, $this->app['session.store']);
}
protected function createDatabaseProvider()
{
$connection = $this->app['db']->connection();
$table = $this->app['config']['customerauth.table'];
return new DatabaseUserProvider($connection, $this->app['hash'], $table);
}
public function createEloquentDriver()
{
$provider = $this->createEloquentProvider();
return new Guard($provider, $this->app['session.store']);
}
protected function createEloquentProvider()
{
$model = $this->app['config']['customerauth.model'];
return new EloquentUserProvider($this->app['hash'], $model);
}
public function getDefaultDriver()
{
return $this->app['config']['customerauth.driver'];
}
public function setDefaultDriver($name)
{
$this->app['config']['customerauth.driver'] = $name;
}
}
然后我实现了 Example/Auth/SiteGuard.php
(注意实现的方法有一个额外的 site_ 定义,这对于其他 Auth 驱动程序应该是不同的):
<?php namespace Example\Auth;
use Illuminate\Auth\Guard;
class SiteGuard extends Guard
{
public function getName()
{
return 'login_site_'.md5(get_class($this));
}
public function getRecallerName()
{
return 'remember_site_'.md5(get_class($this));
}
}
然后我实施了Example/Contracts/Auth/SiteGuard.php
use Illuminate\Contracts\Auth\Guard;
interface SiteGuard extends Guard {}
我终于实现了门面; Example/Support/Facades/Auth/CustomerAuth.php
<?php namespace Example\Support\Facades;
class CustomerAuth extends Facade
{
protected static function getFacadeAccessor()
{
return 'customerauth';
}
}
快速更新,当尝试将这些自定义身份验证驱动程序与 phpunit 一起使用时,您可能会遇到以下错误:
Driver [CustomerAuth] not supported.
您还需要实现它,最简单的解决方案是重写 be
方法并创建一个与之类似的 trait
:
<?php namespace Example\Vendor\Testing;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
trait ApplicationTrait
{
public function be(UserContract $user, $driver = null)
{
$this->app['customerauth']->driver($driver)->setUser($user);
}
}