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



我想通过向DatabaseTokenRepository和PasswordBroker 添加方法来修改密码外观

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();
}
}
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表中的数据库记录。

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

有没有办法注册这些方法?还是宏?或者扩展DatabaseTokenRepositoryPasswordBroker的类,然后告诉Password Facade使用我的扩展类?

有什么建议吗?非常感谢。

感谢类似的问题,我解决了问题!

这是我的代码:

  1. 自定义方法getTokenRecord()供我使用Password::getTokenRecord($user)
namespace AppFacades;
use IlluminateAuthPasswordsPasswordBroker as Broker;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class PasswordBroker extends Broker
{
public function getTokenRecord(CanResetPasswordContract $user)
{
return $this->tokens->getRecord($user);
}
}
  1. getTokenRecord()方法调用的getRecord()
namespace AppRepositories;
use AppModelsMember;
use IlluminateAuthPasswordsDatabaseTokenRepository as DatabaseToken;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class DatabaseTokenRepository extends DatabaseToken
{
public function getRecord(CanResetPasswordContract $user)
{
return $this->getTable()->where(
'email',
$user->getEmailForPasswordReset()
)->first();
}
}
  1. 自定义PasswordBrokerManager以解析我的自定义PasswordBrokerDatabaseTokenRepository
<?php
namespace AppFacades;
use AppFacadesPasswordBroker;
use AppRepositoriesDatabaseTokenRepository;
use IlluminateAuthPasswordsPasswordBrokerManager as Manager;
use InvalidArgumentException;
class PasswordBrokerManager extends Manager
{
/**
* Resolve the given broker.
*
* @param  string  $name
* @return IlluminateContractsAuthPasswordBroker
*
* @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 IlluminateAuthPasswordsTokenRepositoryInterface
*/
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. 注册我的自定义密码外观
<?php
namespace AppProviders;
use AppFacadesPasswordBrokerManager;
use IlluminateSupportServiceProvider;
use IlluminateContractsSupportDeferrableProvider;
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注释掉原点PasswordResetServiceProvider::class,并添加我的自定义类
'providers' => [
// IlluminateAuthPasswordsPasswordResetServiceProvider::class,
AppProvidersPasswordResetServiceProvider::class,
]

最新更新