You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Controllers/PasswordController.php

126 lines
5.0 KiB
PHTML

<?php
namespace App\Controllers;
use App\Models\User;
use App\Services\CaptchaConfig;
use App\Models\PasswordReset;
use App\Services\Password;
use App\Utils\Hash;
use App\Utils\Cookie;
/***
* Class Password
* @package App\Controllers
* 密码重置
*/
class PasswordController extends BaseController
{
public function __construct()
{
$this->applyDomainTheme($this->getDomainConfigurations());
$this->initializePublicLocale();
}
public function reset()
{
return $this->publicView('auth.password')
->assign('captch_respass', CaptchaConfig::get('captch_respass'))
->display('password/reset.tpl');
}
public function handleReset($request, $response, $args)
{
Cookie::startSession();
$captch = $request->getParam('captch');
$email = $request->getParam('email');
// check limit
if (CaptchaConfig::get('captch_respass') == 'true') {
$expectedCaptcha = isset($_SESSION['captch']) && is_scalar($_SESSION['captch'])
? strtolower(trim((string) $_SESSION['captch'])) : '';
$providedCaptcha = is_scalar($captch) ? strtolower(trim((string) $captch)) : '';
if ($expectedCaptcha === '' || $providedCaptcha === '' || !hash_equals($expectedCaptcha, $providedCaptcha)) {
$res['ret'] = 0;
$res['msg'] = $this->publicCopy('auth.password', 'auth.login.error.captcha');
return $response->getBody()->write(json_encode($res));
}
unset($_SESSION['captch']);
}
// send email
$user = User::where('email', $email)->first();
if ($user == null) {
$rs['ret'] = 0;
$rs['msg'] = $this->publicCopy('auth.password', 'auth.password.error.email_missing');
return $response->getBody()->write(json_encode($rs));
}
if (!Password::sendResetEmail($email, $this->language === 'true')) {
$rs['ret'] = 0;
$rs['msg'] = $this->publicCopy('auth.password', 'auth.password.error.send_failed');
return $response->getBody()->write(json_encode($rs));
}
$rs['ret'] = 1;
$rs['msg'] = $this->publicCopy('auth.password', 'auth.password.request.sent');
return $response->getBody()->write(json_encode($rs));
}
public function token($request, $response, $args)
{
$token = $args['token'];
return $this->publicView('auth.password')
->assign('token', $token)
->display('password/token.tpl');
}
public function handleToken($request, $response, $args)
{
$password = $request->getParam('password');
$error = Hash::newPasswordError($password);
if ($error !== null || $password !== $request->getParam('repasswd')) {
$message = $error !== null
? $this->publicCopy('auth.password', 'auth.register.password_invalid')
: $this->publicCopy('auth.password', 'auth.register.password_mismatch');
return $this->echoJson($response, ['ret' => 0, 'msg' => $message]);
}
$tokenStr = isset($args['token']) && is_string($args['token']) ? $args['token'] : '';
if (preg_match('/^[a-f0-9]{64}$/', $tokenStr) !== 1) {
return $this->echoJson($response, [
'ret' => 0,
'msg' => $this->publicCopy('auth.password', 'auth.password.error.link_invalid'),
]);
}
$connection = (new User())->getConnection();
try {
$connection->transaction(function () use ($tokenStr, $password) {
$token = PasswordReset::where('token', $tokenStr)->lockForUpdate()->first();
if ($token === null || (int) $token->expire_time < time()) {
throw new \InvalidArgumentException($this->publicCopy(
'auth.password', 'auth.password.error.link_invalid'
));
}
$user = User::where('email', $token->email)->lockForUpdate()->first();
if ($user === null) {
throw new \InvalidArgumentException($this->publicCopy(
'auth.password', 'auth.password.error.link_invalid'
));
}
$user->updatePassword($password);
PasswordReset::where('email', $token->email)->delete();
});
} catch (\InvalidArgumentException $exception) {
return $this->echoJson($response, ['ret' => 0, 'msg' => $exception->getMessage()]);
} catch (\Throwable $exception) {
error_log('Password recovery failed: ' . get_class($exception));
return $this->echoJson($response, [
'ret' => 0,
'msg' => $this->publicCopy('auth.password', 'auth.password.error.reset_failed'),
]);
}
return $this->echoJson($response, [
'ret' => 1,
'msg' => $this->publicCopy('auth.password', 'auth.password.reset_success'),
]);
}
}