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

标签 php laravel laravel-5 laravel-5.8

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

就我而言,出于安全考虑,我不想验证电子邮件是否已注册,我只想在后端进行检查并告诉用户“如果他提供了注册的电子邮件,他应该很快就会收到恢复电子邮件”。

我为实现这一目标所做的工作在 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文件。

提前致谢!

最佳答案

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

新建自定义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方法。让我们在同一个命名空间中创建一个自定义代理管理器:
<?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,
...
]

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

关于php - 如何在 Laravel 5.8 中扩展或制作自定义 PasswordBroker sendResetLink() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55912215/

相关文章:

php - 如何在 Laravel 中创建一条包罗万象的路线

php - Doctrine\DBAL\Driver\PDOException SQLSTATE[HY000] [2006] MySQL 服务器已消失

php - if(isset($var)) 和 if($var) 的区别

mysql - 如何更新多个相同的外键值,它是 laravel 中的文件类型

php - 用ajax请求渲染多个blade view sections

php - 当有数量和价格时,在 eloquent laravel 中使用 sum

php - Laravel action() 帮助程序忽略 APP_URL 环境变量

php - 在 php 中使用 header 重定向

php - 将数组值插入数据库 laravel

php - 在模型中实现关系