如何在Laravel 5.8中扩展或制作自定义PasswordBroker sendResetLink()方法?

问题描述 投票:0回答:2

目前,重置密码的逻辑是用户必须提供有效/注册的电子邮件才能接收密码恢复电子邮件。

在我的情况下,由于安全问题,我不想验证电子邮件是否已注册,我想在后端进行检查,并告诉用户“如果他已提供注册的电子邮件,他应该很快得到恢复电子邮件“。

我在vendor\laravel\framework\src\Illuminate\Auth\Passwords\PasswordBroker.php sendResetLink()方法中编写的实现此目的的方法是:

 /**
     * Send a password reset link to a user.
     *
     * @param  array  $credentials
     * @return string
     */
    public function sendResetLink(array $credentials)
    {
        // First we will check to see if we found a user at the given credentials and
        // if we did not we will redirect back to this current URI with a piece of
        // "flash" data in the session to indicate to the developers the errors.
        $user = $this->getUser($credentials);

        if (is_null($user)) {
            return static::INVALID_USER;
        }

        // Once we have the reset token, we are ready to send the message out to this
        // user with a link to reset their password. We will then redirect back to
        // the current URI having nothing set in the session to indicate errors.
        $user->sendPasswordResetNotification(
            $this->tokens->create($user)
        );

        return static::RESET_LINK_SENT;
    }

对此:

 /**
     * Send a password reset link to a user.
     *
     * @param  array  $credentials
     * @return string
     */
    public function sendResetLink(array $credentials)
    {
        // First we will check to see if we found a user at the given credentials and
        // if we did not we will redirect back to this current URI with a piece of
        // "flash" data in the session to indicate to the developers the errors.
        $user = $this->getUser($credentials);

//        if (is_null($user)) {
//            return static::INVALID_USER;
//        }

        // Once we have the reset token, we are ready to send the message out to this
        // user with a link to reset their password. We will then redirect back to
        // the current URI having nothing set in the session to indicate errors.
        if(!is_null($user)) {
            $user->sendPasswordResetNotification(
                $this->tokens->create($user)
            );
        }

        return static::RESET_LINK_SENT;
    }

这个硬编码选项不是最佳解决方案,因为它会在更新后消失。所以我想知道如何在App文件夹中的项目范围内扩展或实现此更改以始终保留此更改?

附:我已经尝试过这里提到的解决方案:Laravel 5.3 Password Broker Customization但它没有工作..目录树也不同,我无法理解在哪里放新的PasswordBroker.php文件。

提前致谢!

php laravel laravel-5 laravel-5.8
2个回答
1
投票

这里最简单的解决方案是将您的自定义代码放在app\Http\Controllers\Auth\ForgotPasswordController中 - 这是控制器拉入SendsPasswordResetEmails特性。

您的方法会覆盖该特征提供的方法,因此将调用该方法而不是特征中的方法。您可以使用代码覆盖整个sendResetLinkEmail方法,无论成功与否,始终都会返回相同的响应。

public function sendResetLinkEmail(Request $request)
{
    $this->validateEmail($request);

    // 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.
    $response = $this->broker()->sendResetLink(
        $request->only('email')
    );

    return back()->with('status', "If you've provided registered e-mail, you should get recovery e-mail shortly.");
}

3
投票

以下是您需要遵循的步骤。

创建一个新的自定义PasswordResetsServiceProvider。我有一个名为Extensions的文件夹(命名空间),我将放置此文件:

<?php

namespace App\Extensions\Passwords;

use Illuminate\Auth\Passwords\PasswordResetServiceProvider as BasePasswordResetServiceProvider;

class PasswordResetServiceProvider extends BasePasswordResetServiceProvider
{
    /**
     * Indicates if loading of the provider is deferred.
     *
     * @var bool
     */
    protected $defer = true;

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->registerPasswordBroker();
    }

    /**
     * Register the password broker instance.
     *
     * @return void
     */
    protected function registerPasswordBroker()
    {
        $this->app->singleton('auth.password', function ($app) {
            return new PasswordBrokerManager($app);
        });

        $this->app->bind('auth.password.broker', function ($app) {
            return $app->make('auth.password')->broker();
        });
    }
}

如您所见,此提供程序扩展了基本密码重置提供程序。唯一改变的是我们从PasswordBrokerManager方法返回自定义registerPasswordBroker。让我们在同一个命名空间中创建一个自定义Broker管理器:

<?php

namespace App\Extensions\Passwords;

use Illuminate\Auth\Passwords\PasswordBrokerManager as BasePasswordBrokerManager;

class PasswordBrokerManager extends BasePasswordBrokerManager
{
    /**
     * Resolve the given broker.
     *
     * @param  string  $name
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     *
     * @throws \InvalidArgumentException
     */
    protected function resolve($name)
    {
        $config = $this->getConfig($name);

        if (is_null($config)) {
            throw new InvalidArgumentException(
                "Password resetter [{$name}] is not defined."
            );
        }

        // The password broker uses a token repository to validate tokens and send user
        // password e-mails, as well as validating that password reset process as an
        // aggregate service of sorts providing a convenient interface for resets.
        return new PasswordBroker(
            $this->createTokenRepository($config),
            $this->app['auth']->createUserProvider($config['provider'] ?? null)
        );
    }
}

同样,这个PasswordBrokerManager从laravel扩展了基本管理器。这里唯一的区别是新的resolve方法,它从同一名称空间返回一个新的和自定义的PasswordBroker。所以最后一个文件我们将在同一名称空间中创建自定义PasswordBroker

<?php

namespace App\Extensions\Passwords;

use Illuminate\Auth\Passwords\PasswordBroker as BasePasswordBroker;

class PasswordBroker extends BasePasswordBroker
{
 /**
     * Send a password reset link to a user.
     *
     * @param  array  $credentials
     * @return string
     */
    public function sendResetLink(array $credentials)
    {
        // First we will check to see if we found a user at the given credentials and
        // if we did not we will redirect back to this current URI with a piece of
        // "flash" data in the session to indicate to the developers the errors.
        $user = $this->getUser($credentials);

//        if (is_null($user)) {
//            return static::INVALID_USER;
//        }

        // Once we have the reset token, we are ready to send the message out to this
        // user with a link to reset their password. We will then redirect back to
        // the current URI having nothing set in the session to indicate errors.
        if(!is_null($user)) {
            $user->sendPasswordResetNotification(
                $this->tokens->create($user)
            );
        }

        return static::RESET_LINK_SENT;
    }
}

正如您所看到的,我们从Laravel扩展了默认的PasswordBroker类,并且只覆盖了我们需要覆盖的方法。

最后一步是简单地用我们的Laravel Default PasswordReset代理替换它。在config/app.php文件中,更改注册提供程序的行:

'providers' => [
...
// Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
   App\Extensions\Passwords\PasswordResetServiceProvider::class,
...
]

这就是注册自定义密码代理所需的全部内容。希望有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.