Laravel 5实施多个验证司机 [英] Laravel 5 Implement multiple Auth drivers

查看:130
本文介绍了Laravel 5实施多个验证司机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个系统,验证至少有两个层次,都有在数据库中的用户分开模式和表。谷歌和唯一的解决方案快速搜索迄今已与上鞋拔验证

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.

我试图删除验证这是相当直接的。不过,我想用一个单独的配置文件按配置/ customerauth喜欢 CustomerAuth AdminAuth 。 PHP 配置\\ adminauth.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 config\adminauth.php

推荐答案

我假设您有可工作的软件包。在这个例子中我的供应商的名称空间将仅仅是:示例 - 所有code片段可以按照指示找到

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.

我复制配置/ auth.php 配置/ customerauth.php 及相应修改设置。

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

我编辑了配置/ app.php 并替换照亮\\验证\\ AuthServiceProvider 实例\\验证\\ CustomerAuthServiceProvider

我编辑了配置/ app.php ,取而代之的验证别名:

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

'CustomerAuth' => 'Example\Support\Facades\CustomerAuth',

我则实现套餐内code例如供应商/例如/ src目录/ 。我开始用的ServiceProvider:例/认证/ 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 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();
            });
        });
    }
}

然后我执行:例/认证/ 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;
    }
}

然后我实施例/认证/ SiteGuard.php (注意实施,有额外的site_定义的方法,这应该是其他车手验证不同):

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 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));
    }
}

然后我实施例/合同/认证/ SiteGuard.php

use Illuminate\Contracts\Auth\Guard;

interface SiteGuard extends Guard {}

最后我实现了门面; 例/支持/幕墙/认证/ CustomerAuth.php

<?php namespace Example\Support\Facades;

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.

您还需要实现这一点,最简单的解决办法是重写方法,并同时创建一个特性相似它:

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

<?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);
    }
}

这篇关于Laravel 5实施多个验证司机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆