0

我使用自定义视图实现了 Fortify Multi Auth Guard(管理员和用户)。我有管理员和用户的单独登录视图。我还为用户实施了忘记密码,并且运行良好。我只是无法为管理员实施忘记密码。此外,我还想为管理员和用户实施电子邮件验证。我找到了一半的解决方案请允许我解释一下:-

供应商/laravel/framework/src/illuminate/Auth/Passwords/PasswordBrokerManager.php

public function broker($name = null)
{
    $name = $name ?: $this->getDefaultDriver();
    return $this->brokers[$name] ?? ($this->brokers[$name] = $this->resolve($name));
}

如果我将上述方法修改如下,它仅适用于用户

public function broker($name = null)
{
    $name = $name ?: $this->getDefaultDriver();
    return $this->brokers[$name[0]] ?? ($this->brokers[$name[0]] = $this->resolve($name[0]));
}

如果我如下修改方法,它仅适用于管理员

public function broker($name = null)
{
    $name = $name ?: $this->getDefaultDriver();
    return $this->brokers[$name[1]] ?? ($this->brokers[$name[1]] = $this->resolve($name[1]));
}

根据要求,它将如何为两者工作?覆盖提到的 broker() 方法的最佳方法是什么?

配置/auth.php

<?php

return [


    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],


    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],


    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],


    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
        
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],


    'password_timeout' => 10800,

];

配置/fortify.php

<?php

use App\Providers\RouteServiceProvider;
use Laravel\Fortify\Features;

return [

    'guard' => 'web',

    'passwords' => ['users', 'admins'],

    'username' => 'email',

    'email' => 'email',

    'home' => RouteServiceProvider::HOME,

    'prefix' => '',

    'domain' => null,

    'middleware' => ['web'],

    'limiters' => [
        'login' => 'login',
        'two-factor' => 'two-factor',
    ],

    'views' => true,

    'features' => [
        Features::registration(),
        Features::resetPasswords(),
        // Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        Features::twoFactorAuthentication([
            'confirmPassword' => true,
        ]),
    ],

];
4

1 回答 1

0

我实现了这个,但对于 Tenancy,但是一旦我处理了这 2 个差异保护,这个想法是一样的。在 auth.php 中类似但添加到 'defaults'

'defaults' => [
        'guard' => 'web',
        'passwords' => ['users','tenant'],  // in your case must be ['users','admins']
    ],

其余的对于 auth 和 fortify 文件都是相同的配置。在我的情况下,我可以在处理租户或主应用程序用户时通过 Tenancy 包识别,然后我做

public function broker($name = null)
{
   $name = $name ?: $this->getDefaultDriver();
   if (Tenancy::getTenant()) {
      return $this->brokers[$name[1]] ?? ($this->brokers[$name[1]] = $this->resolve($name[1]));
   }
   else
      return $this->brokers[$name[0]] ?? ($this->brokers[$name[0]] = $this->resolve($name[0]));
}

在您的情况下,您必须找到将一些标志传递给代理的方法,以便您管理它,或者如果可以在 PasswordResetLinkController 的代理方法中管理它,请进行更多研究。在这里,您有 store 方法,您可以检查用户的请求是否为管理员,并为每种情况提供所需的代理

public function store(Request $request): Responsable
{
   $request->validate([Fortify::email() => 'required|email']);
       //see if here you can get the user through email credentials

   // We will send the password reset link to this user. Once we have attempted
   // to send the link, we will examine the response then see the message we
   // need to show to the user. Finally, we'll send out a proper response.
   $status = $this->broker()->sendResetLink($request->only(Fortify::email()));
  //......

于 2021-05-25T22:12:45.527 回答