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

时间:2019-04-29 23:58:37

标签: 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文件夹的项目范围内扩展或实现此更改< / strong>始终保留此更改?

P.S。我已经尝试过这里提到的解决方案:Laravel 5.3 Password Broker Customization,但是它不起作用..目录树也不同,而且我不知道在哪里放置新的PasswordBroker.php文件。

谢谢!

3 个答案:

答案 0 :(得分:2)

这是您需要遵循的步骤。

创建一个新的自定义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 Manager:

<?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,
...
]

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

答案 1 :(得分:1)

最简单的解决方案是将自定义代码放在var employeesFilter = employees.Where(employee => employee.YearsOfService > 5); foreach (Employee employee in employeesFilter) { // ... } 中-这是引入app\Http\Controllers\Auth\ForgotPasswordController特性的控制器。

您的方法将覆盖该特征提供的一种,因此将代替该特征中的一种来调用它。您可以用代码覆盖整个SendsPasswordResetEmails方法,无论成功与否总是返回相同的响应。

sendResetLinkEmail

答案 2 :(得分:-1)

您可以在sendResetLinkFailedResponse类中覆盖ForgetPasswordController方法。

protected function sendResetLinkFailedResponse(Request $request, $response)
{
    return $this->sendResetLinkResponse($request, Password::RESET_LINK_SENT);
}

即使验证失败,我们也只会发送成功的响应。