pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Laravel 5 實現多個Auth驅動

Laravel 5 Implement multiple Auth drivers(Laravel 5 實現多個Auth驅動)
本文介紹了Laravel 5 實現多個Auth驅動的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在構建一個至少具有兩級身份驗證的系統,并且兩者在數據庫中都有單獨的用戶模型和表.在 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);
    }
}

這篇關于Laravel 5 實現多個Auth驅動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Magento products by categories(按類別劃分的 Magento 產品)
Resource interpreted as image but transferred with MIME type text/html - Magento(資源被解釋為圖像但使用 MIME 類型 text/html 傳輸 - Magento)
Is there an event for customer account registration in Magento?(Magento 中是否有客戶帳戶注冊事件?)
Magento addFieldToFilter: Two fields, match as OR, not AND(Magento addFieldToFilter:兩個字段,匹配為 OR,而不是 AND)
quot;Error 404 Not Foundquot; in Magento Admin Login Page(“未找到錯誤 404在 Magento 管理員登錄頁面)
Get Order Increment Id in Magento(在 Magento 中獲取訂單增量 ID)
主站蜘蛛池模板: ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | SRRC认证_电磁兼容_EMC测试整改_FCC认证_SDOC认证-深圳市环测威检测技术有限公司 | 拉力测试机|材料拉伸试验机|电子拉力机价格|万能试验机厂家|苏州皖仪实验仪器有限公司 | 包塑丝_高铁绑丝_地暖绑丝_涂塑丝_塑料皮铁丝_河北创筹金属丝网制品有限公司 | 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | 济南品牌包装设计公司_济南VI标志设计公司_山东锐尚文化传播 | 软启动器-上海能曼电气有限公司 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 仿真植物|仿真树|仿真花|假树|植物墙 - 广州天昆仿真植物有限公司 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家 | 防火板_饰面耐火板价格、厂家_品牌认准格林雅 | 淄博不锈钢无缝管,淄博不锈钢管-鑫门物资有限公司 | 安徽合肥格力空调专卖店_格力中央空调_格力空调总经销公司代理-皖格制冷设备 | 粘度计维修,在线粘度计,二手博勒飞粘度计维修|收购-天津市祥睿科技有限公司 | 乐之康护 - 专业护工服务平台,提供医院陪护-居家照护-居家康复 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 不锈钢水箱生产厂家_消防水箱生产厂家-河南联固供水设备有限公司 | 北京易通慧公司从事北京网站优化,北京网络推广、网站建设一站式服务商-北京网站优化公司 | 便携式XPDM露点仪-在线式防爆露点仪-增强型烟气分析仪-约克仪器 冰雕-冰雪世界-大型冰雕展制作公司-赛北冰雕官网 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 北京普辉律师事务所官网_北京律师24小时免费咨询|法律咨询 | 医学动画公司-制作3d医学动画视频-医疗医学演示动画制作-医学三维动画制作公司 | TPM咨询,精益生产管理,5S,6S现场管理培训_华谋咨询公司 | 广域铭岛Geega(际嘉)工业互联网平台-以数字科技引领行业跃迁 | 水厂污泥地磅|污泥处理地磅厂家|地磅无人值守称重系统升级改造|地磅自动称重系统维修-河南成辉电子科技有限公司 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 东莞市海宝机械有限公司-不锈钢分选机-硅胶橡胶-生活垃圾-涡电流-静电-金属-矿石分选机 | 雨燕360体育免费直播_雨燕360免费NBA直播_NBA篮球高清直播无插件-雨燕360体育直播 | 电动车头盔厂家_赠品头盔_安全帽批发_山东摩托车头盔—临沂承福头盔 | 北京企业宣传片拍摄_公司宣传片制作-广告短视频制作_北京宣传片拍摄公司 | PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | 网站建设,北京网站建设,北京网站建设公司,网站系统开发,北京网站制作公司,响应式网站,做网站公司,海淀做网站,朝阳做网站,昌平做网站,建站公司 | 金联宇电缆总代理-金联宇集团-广东金联宇电缆实业有限公司 | 暴风影音| 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 成都热收缩包装机_袖口式膜包机_高速塑封机价格_全自动封切机器_大型套膜机厂家 | 复盛空压机配件-空气压缩机-复盛空压机(华北)总代理 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | Dataforth隔离信号调理模块-信号放大模块-加速度振动传感器-北京康泰电子有限公司 | 股指期货-期货开户-交易手续费佣金加1分-保证金低-期货公司排名靠前-万利信息开户 |