首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修改密码外观?(PasswordBroker和DatabaseTokenRepository)

如何修改密码外观?(PasswordBroker和DatabaseTokenRepository)
EN

Stack Overflow用户
提问于 2022-07-22 01:56:45
回答 1查看 99关注 0票数 0

我想通过向密码立面PasswordBroker中添加方法来修改PasswordBroker

代码语言:javascript
复制
class DatabaseTokenRepository implements TokenRepositoryInterface
{
    /*I want to add the method, reference to the exists() method*/
    public function getRecord(CanResetPasswordContract $user)
    {
        return (array) $this->getTable()->where(
            'email', $user->getEmailForPasswordReset()
        )->first();
    }
}
代码语言:javascript
复制
class PasswordBroker implements PasswordBrokerContract
{
    /*I want to add the method*/
    public function getTokenRecord(CanResetPasswordContract $user)
    {
        return $this->tokens->getRecord($user);
    }
}

因此,我可以使用Password::getTokenRecord($user)获取password_resets表中的数据库记录。

我相信使用Password外观,代码更一致,而不是使用DB:table('password_resets')->where('email', $user->email)->first()

有办法注册这些方法吗?还是宏?或者扩展DatabaseTokenRepositoryPasswordBroker的类,然后告诉密码外观使用我的扩展类?

有什么建议吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-26 07:53:06

感谢相似问题,我解决了这个问题!

这是我的密码:

  1. 自定义方法getTokenRecord()让我使用Password::getTokenRecord($user)
代码语言:javascript
复制
namespace App\Facades;

use Illuminate\Auth\Passwords\PasswordBroker as Broker;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class PasswordBroker extends Broker
{
    public function getTokenRecord(CanResetPasswordContract $user)
    {
        return $this->tokens->getRecord($user);
    }
}
  1. getRecord()方法调用的getTokenRecord()
代码语言:javascript
复制
namespace App\Repositories;

use App\Models\Member;
use Illuminate\Auth\Passwords\DatabaseTokenRepository as DatabaseToken;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class DatabaseTokenRepository extends DatabaseToken
{
    public function getRecord(CanResetPasswordContract $user)
    {
        return $this->getTable()->where(
            'email',
            $user->getEmailForPasswordReset()
        )->first();
    }
}
  1. 解析我的自定义PasswordBrokerDatabaseTokenRepository的自定义PasswordBroker
代码语言:javascript
复制
<?php

namespace App\Facades;

use App\Facades\PasswordBroker;
use App\Repositories\DatabaseTokenRepository;
use Illuminate\Auth\Passwords\PasswordBrokerManager as Manager;
use InvalidArgumentException;

class PasswordBrokerManager extends Manager
{
    /**
     * 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)
        );
    }

    /**
     * Create a token repository instance based on the given configuration.
     *
     * @param  array  $config
     * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
     */
    protected function createTokenRepository(array $config)
    {
        $key = $this->app['config']['app.key'];

        if (str_starts_with($key, 'base64:')) {
            $key = base64_decode(substr($key, 7));
        }

        $connection = $config['connection'] ?? null;

        return new DatabaseTokenRepository(
            $this->app['db']->connection($connection),
            $this->app['hash'],
            $config['table'],
            $key,
            $config['expire'],
            $config['throttle'] ?? 0
        );
    }
}
  1. 注册我的自定义密码外观
代码语言:javascript
复制
<?php

namespace App\Providers;

use App\Facades\PasswordBrokerManager;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Support\DeferrableProvider;

class PasswordResetServiceProvider extends ServiceProvider implements DeferrableProvider
{

    public function register()
    {
        $this->registerPasswordBrokerManager();
    }

    protected function registerPasswordBrokerManager()
    {
        $this->app->singleton('auth.password', function ($app) {
            return new PasswordBrokerManager($app);
        });
    }

    public function provides()
    {
        return ['auth.password'];
    }
}
  1. 注释掉来自config/app.php的源config/app.php,并添加我的自定义类
代码语言:javascript
复制
'providers' => [
    // Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
    App\Providers\PasswordResetServiceProvider::class,
]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73074307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档