【发布时间】:2020-01-31 23:40:54
【问题描述】:
我正在使用 Laravel 的“重置密码”。
我们的架构中有一些特殊之处:多个帐户可以拥有相同的电子邮件地址,登录名是唯一的密钥。 我想更改密码重置控制器,以便在密码重置视图中: - 如果用户输入其电子邮件,则为该电子邮件的所有帐户设置密码(我应该在中间件中进行吗?现在只设置了一个随机帐户,我猜是第一个) - 如果用户输入了登录名,我们只更改其登录名的密码
你认为这可能吗? (对于新帐户,不可能使用现有电子邮件创建新帐户,但现在我们有大约 8000 名用户拥有双重电子邮件帐户,因此很遗憾无法更改)。 非常感谢您的建议!
这是我的代码,我不知道从哪里开始
[编辑]
这是我根据 Mostakim Billah 的建议编写的代码: 我重写了现有的 resetPassword et reset 函数(让它们保持原样)并添加了 //HERE 部分
public function reset(Request $request)
{
$request->validate($this->rules(), $this->validationErrorMessages());
$response = $this->broker()->reset(
$this->credentials($request), function ($user, $password) {
$this->resetPassword($user, $password);
}
);
return $response == Password::PASSWORD_RESET
? $this->sendResetResponse($request, $response)
: $this->sendResetFailedResponse($request, $response);
}
protected function resetPassword($user, $password)
{
$user->password = Hash::make($password);
$user->setRememberToken(Str::random(60));
$user->save();
// HERE: set passwords for other users with the same email
**User::where('email', $user->email)
->where('login', '!=', $user->login)
->where('password', null)
->update(['password' => Hash::make($password)]);**
event(new PasswordReset($user));
$this->guard()->login($user);
}
【问题讨论】: