Laravel 5 实现多个Auth驱动

2021-12-18 00:00:00 authentication reflection php laravel-5

我正在构建一个至少具有两级身份验证的系统,并且两者在数据库中都有单独的用户模型和表.在 google 上快速搜索,目前唯一的解决方案是使用 MultiAuth 包,该包在 Auth 上硬塞多个驱动程序.

I am building a system with at least two levels of Authentication and both have separate User models and tables in the database. A quick search on google and the only solution thus far is with a MultiAuth package that shoehorns multiple drivers on Auth.

我正在尝试删除相当简单的 Auth.但我希望 CustomerAuthAdminAuth 根据 config/customerauth.phpconfigadminauth.php<使用单独的配置文件/代码>

I am attempting to remove Auth which is fairly straight-forward. But I would like CustomerAuth and AdminAuth using a separate config file as per config/customerauth.php and configadminauth.php

推荐答案

解决方案

我假设您有一个可以处理的包.在此示例中,我的供应商命名空间将简单地为:Example - 可以按照说明找到所有代码片段.

Solution

I'm assuming you have a package available to work on. My vendor namespace in this example will simply be: Example - all code snippets can be found following the instructions.

我将 config/auth.php 复制到 config/customerauth.php 并相应地修改了设置.

I copied config/auth.php to config/customerauth.php and amended the settings accordingly.

我编辑了 config/app.php 并将 IlluminateAuthAuthServiceProvider 替换为 ExampleAuthCustomerAuthServiceProvider.

I edited the config/app.php and replaced the IlluminateAuthAuthServiceProvider with ExampleAuthCustomerAuthServiceProvider.

我编辑了 config/app.php 并将 Auth 别名替换为:

I edited the config/app.php and replaced the Auth alias with:

'CustomerAuth' => 'ExampleSupportFacadesCustomerAuth',

然后我在包中实现了代码,例如 vendor/example/src/.我从 ServiceProvider 开始:Example/Auth/CustomerAuthServiceProvider.php

I then implemented the code within the package for example vendor/example/src/. I started with the ServiceProvider: Example/Auth/CustomerAuthServiceProvider.php

<?php namespace ExampleAuth;

use IlluminateAuthAuthServiceProvider;
use ExampleAuthCustomerAuthManager;
use ExampleAuthSiteGuard;

class CustomerAuthServiceProvider extends AuthServiceProvider
{
    public function register()
    {
        $this->app->alias('customerauth',        'ExampleAuthCustomerAuthManager');
        $this->app->alias('customerauth.driver', 'ExampleAuthSiteGuard');
        $this->app->alias('customerauth.driver', 'ExampleContractsAuthSiteGuard');

        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('IlluminateContractsAuthAuthenticatable', 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 ExampleAuth;

use IlluminateAuthAuthManager;
use IlluminateAuthEloquentUserProvider;
use ExampleAuthSiteGuard 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 驱动程序应该是不同的):

I then implemented Example/Auth/SiteGuard.php (note the methods implemented have an additional site_ defined, this should be different for other Auth drivers):

<?php namespace ExampleAuth;

use IlluminateAuthGuard;

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 IlluminateContractsAuthGuard;

interface SiteGuard extends Guard {}

最后我实现了 Facade;Example/Support/Facades/Auth/CustomerAuth.php

Finally I implemented the Facade; Example/Support/Facades/Auth/CustomerAuth.php

<?php namespace ExampleSupportFacades;

class CustomerAuth extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'customerauth';
    }
}

<小时>

快速更新,当尝试使用带有 phpunit 你可能会得到以下错误:


A quick update, when trying to use these custom auth drivers with phpunit you may get the following error:

Driver [CustomerAuth] not supported.

您还需要实现这一点,最简单的解决方案是覆盖 be 方法并创建一个与其类似的 trait:

You also need to implement this, the easiest solution is override the be method and also creating a trait similar to it:

<?php namespace ExampleVendorTesting;

use IlluminateContractsAuthAuthenticatable as UserContract;

trait ApplicationTrait
{
    public function be(UserContract $user, $driver = null)
    {
        $this->app['customerauth']->driver($driver)->setUser($user);
    }
}

相关文章