|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Services\AccountEmailChangeService;
|
|
|
|
|
use App\Services\AccountSecurityService;
|
|
|
|
|
use App\Services\AdminCsrf;
|
|
|
|
|
use App\Services\Auth;
|
|
|
|
|
use App\Services\Payment\RechargeMethodsService;
|
|
|
|
|
use App\Services\UserFeatureRestrictionService;
|
|
|
|
|
use App\Services\UserProfileService;
|
|
|
|
|
use App\Utils\Hash;
|
|
|
|
|
|
|
|
|
|
/** Account drawer APIs which do not need to render a page. */
|
|
|
|
|
class AccountSecurityController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
public function updateProfile($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$csrf = $this->verifyCsrf($request, $response);
|
|
|
|
|
if ($csrf !== null) {
|
|
|
|
|
return $csrf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$input = [];
|
|
|
|
|
foreach (UserProfileService::FIELDS as $field) {
|
|
|
|
|
$value = $request->getParam($field, '');
|
|
|
|
|
$input[$field] = is_string($value) ? $value : '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$profile = UserProfileService::update(Auth::getUser(), $input);
|
|
|
|
|
} catch (\InvalidArgumentException $exception) {
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => $exception->getMessage(),
|
|
|
|
|
], 422);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Account profile update failed: ' . get_class($exception));
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => '账户信息保存失败,请稍后重试',
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'profile' => $profile,
|
|
|
|
|
'msg' => '账户信息已保存',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function emailVerifySend($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$csrf = $this->verifyCsrf($request, $response);
|
|
|
|
|
if ($csrf !== null) {
|
|
|
|
|
return $csrf;
|
|
|
|
|
}
|
|
|
|
|
$user = Auth::getUser();
|
|
|
|
|
$result = AccountEmailChangeService::sendNewEmailCode(
|
|
|
|
|
$user,
|
|
|
|
|
$request->getParam('email', ''),
|
|
|
|
|
$request->getServerParam('REMOTE_ADDR', '')
|
|
|
|
|
);
|
|
|
|
|
$status = $result['ok'] ? 200 : ($result['retry_after'] > 0 ? 429 : 422);
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => $result['ok'] ? 1 : 0,
|
|
|
|
|
'msg' => $result['msg'],
|
|
|
|
|
'retry_after' => $result['retry_after'],
|
|
|
|
|
], $status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updateEmail($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$csrf = $this->verifyCsrf($request, $response);
|
|
|
|
|
if ($csrf !== null) {
|
|
|
|
|
return $csrf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$result = AccountEmailChangeService::change(
|
|
|
|
|
Auth::getUser(),
|
|
|
|
|
$request->getParam('email', ''),
|
|
|
|
|
$request->getParam('emailcode', '')
|
|
|
|
|
);
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Account email update failed: ' . get_class($exception));
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => '邮箱修改失败,请稍后重试',
|
|
|
|
|
], 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$result['ok']) {
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => $result['msg'],
|
|
|
|
|
], $result['status']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
Auth::logout();
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
// The database-backed login version was already revoked. Cookie
|
|
|
|
|
// cleanup is best effort so a driver outage cannot undo success.
|
|
|
|
|
error_log('Account email logout cleanup failed: ' . get_class($exception));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'email' => $result['email'],
|
|
|
|
|
'msg' => $result['msg'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function passwordVerifySend($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$csrf = $this->verifyCsrf($request, $response);
|
|
|
|
|
if ($csrf !== null) {
|
|
|
|
|
return $csrf;
|
|
|
|
|
}
|
|
|
|
|
$user = Auth::getUser();
|
|
|
|
|
$result = AccountSecurityService::sendPasswordEmailCode(
|
|
|
|
|
$user,
|
|
|
|
|
$request->getServerParam('REMOTE_ADDR', '')
|
|
|
|
|
);
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => $result['ok'] ? 1 : 0,
|
|
|
|
|
'msg' => $result['msg'],
|
|
|
|
|
'retry_after' => $result['retry_after'],
|
|
|
|
|
], $result['ok'] ? 200 : 429);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function updatePassword($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$csrf = $this->verifyCsrf($request, $response);
|
|
|
|
|
if ($csrf !== null) {
|
|
|
|
|
return $csrf;
|
|
|
|
|
}
|
|
|
|
|
$user = Auth::getUser();
|
|
|
|
|
$old = $request->getParam('oldpwd', '');
|
|
|
|
|
$password = $request->getParam('pwd', '');
|
|
|
|
|
$repeat = $request->getParam('repwd', '');
|
|
|
|
|
if (!is_string($old) || !is_string($password) || !is_string($repeat)) {
|
|
|
|
|
return $this->json($response, ['ret' => 0, 'msg' => '密码格式无效'], 422);
|
|
|
|
|
}
|
|
|
|
|
if (!Hash::checkPassword($user->pass, $old)) {
|
|
|
|
|
return $this->json($response, ['ret' => 0, 'msg' => '旧密码错误'], 422);
|
|
|
|
|
}
|
|
|
|
|
$error = Hash::newPasswordError($password);
|
|
|
|
|
if ($error !== null || $password !== $repeat) {
|
|
|
|
|
return $this->json($response, ['ret' => 0, 'msg' => $error ?? '两次密码不一致'], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$factor = AccountSecurityService::consumePasswordFactor(
|
|
|
|
|
$user,
|
|
|
|
|
$request->getParam('emailcode', ''),
|
|
|
|
|
$request->getParam('gacode', ''),
|
|
|
|
|
$request->getServerParam('REMOTE_ADDR', '')
|
|
|
|
|
);
|
|
|
|
|
if ($factor === null) {
|
|
|
|
|
$hasTotp = AccountSecurityService::hasTotp($user);
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'need_security_verify' => 1,
|
|
|
|
|
'need_email_verify' => 1,
|
|
|
|
|
'need_ga_verify' => $hasTotp ? 1 : 0,
|
|
|
|
|
'msg' => $hasTotp ? '请使用邮箱验证码或谷歌验证器完成安全验证' : '请先获取邮箱验证码完成安全验证',
|
|
|
|
|
], 422);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$user->updatePassword($password);
|
|
|
|
|
Auth::logout();
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
error_log('Account password update failed: ' . get_class($exception));
|
|
|
|
|
return $this->json($response, ['ret' => 0, 'msg' => '密码修改失败,请稍后重试'], 500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'method' => $factor,
|
|
|
|
|
'msg' => '密码修改成功,请重新登录',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function rechargeOptions($request, $response, $args)
|
|
|
|
|
{
|
|
|
|
|
$user = Auth::getUser();
|
|
|
|
|
if (UserFeatureRestrictionService::isRechargeRestricted($user)) {
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => UserFeatureRestrictionService::RECHARGE_MESSAGE,
|
|
|
|
|
'methods' => [],
|
|
|
|
|
], 503);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 1,
|
|
|
|
|
'methods' => RechargeMethodsService::forUser($user),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function json($response, array $data, $status = 200)
|
|
|
|
|
{
|
|
|
|
|
$response = $response->withStatus((int) $status)
|
|
|
|
|
->withHeader('Content-Type', 'application/json; charset=utf-8')
|
|
|
|
|
->withHeader('Cache-Control', 'no-store, private');
|
|
|
|
|
$response->getBody()->write(json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function verifyCsrf($request, $response)
|
|
|
|
|
{
|
|
|
|
|
if (AdminCsrf::verify($request)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->json($response, [
|
|
|
|
|
'ret' => 0,
|
|
|
|
|
'msg' => '请求已过期,请刷新页面后重试',
|
|
|
|
|
], 403);
|
|
|
|
|
}
|
|
|
|
|
}
|