优化了一些问题

This commit is contained in:
2026-08-29 22:03:48 +08:00
parent 1d904e6532
commit 780e1eba21
46 changed files with 4074 additions and 3143 deletions
+66 -15
View File
@@ -11,6 +11,14 @@ defined('ABSPATH') || exit;
trait UserControllerAuthPhoneOperations {
private function delete_oauth_registration_user(int $user_id): void {
if ($user_id <= 0) {
return;
}
require_once ABSPATH . 'wp-admin/includes/user.php';
wp_delete_user($user_id);
}
private function auth_payload(\WP_REST_Request $request): array {
$params = $this->get_json_params($request);
return is_array($params) ? $params : [];
@@ -220,14 +228,23 @@ trait UserControllerAuthPhoneOperations {
if (!$user || !$this->auth_consume_code('login', $target, sanitize_text_field((string) $payload['code']), $channel)) {
return $this->error(__('账号或验证码错误', 'aksrt'), 401);
}
if ($oauth_data) {
$oauth_data = \AKSRT\Auth\Service::claim_oauth_bind_context($obk);
if (!$oauth_data) {
return $this->error(__('绑定会话已被使用,请重新授权', 'aksrt'), 410);
}
$binding = \AKSRT\Auth\Service::finalize_oauth_binding((int) $user->ID, $oauth_data, $obk);
if (is_wp_error($binding)) {
return $this->error($binding->get_error_message(), 409);
}
\AKSRT\Auth\OAuthAccountService::complete_login($user, !empty($payload['rememberme']));
$redirect = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : $redirect;
return $this->success(['message' => __('账号绑定成功', 'aksrt'), 'redirect' => $redirect]);
}
wp_set_current_user((int) $user->ID);
wp_set_auth_cookie((int) $user->ID, !empty($payload['rememberme']));
do_action('wp_login', $user->user_login, $user);
if ($oauth_data) {
\AKSRT\Auth\Service::finalize_oauth_binding((int) $user->ID, $oauth_data, $obk);
$redirect = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : $redirect;
}
return $this->success(['message' => $oauth_data ? __('账号绑定成功', 'aksrt') : __('登录成功', 'aksrt'), 'redirect' => $redirect]);
return $this->success(['message' => __('登录成功', 'aksrt'), 'redirect' => $redirect]);
}
if (!class_exists('\AKSRT\Auth\Service')) {
@@ -236,17 +253,27 @@ trait UserControllerAuthPhoneOperations {
$payload['log'] = $target;
$payload['pwd'] = (string) ($payload['password'] ?? $payload['pwd'] ?? '');
$payload['redirect_to'] = $redirect;
$result = \AKSRT\Auth\Service::login_user($payload, $oauth_data ? 'bind' : 'login');
if (empty($result['success'])) {
return $this->error(__('账号或密码错误', 'aksrt'), 401);
}
if ($oauth_data) {
$user_id = get_current_user_id();
if ($user_id <= 0) {
$resolved_login = \AKSRT\Auth\Service::resolve_email_to_username($target);
$user = wp_authenticate($resolved_login, (string) ($payload['password'] ?? $payload['pwd'] ?? ''));
if (is_wp_error($user) || !$user instanceof \WP_User) {
return $this->error(__('账号或密码错误', 'aksrt'), 401);
}
\AKSRT\Auth\Service::finalize_oauth_binding($user_id, $oauth_data, $obk);
$oauth_data = \AKSRT\Auth\Service::claim_oauth_bind_context($obk);
if (!$oauth_data) {
return $this->error(__('绑定会话已被使用,请重新授权', 'aksrt'), 410);
}
$binding = \AKSRT\Auth\Service::finalize_oauth_binding((int) $user->ID, $oauth_data, $obk);
if (is_wp_error($binding)) {
return $this->error($binding->get_error_message(), 409);
}
\AKSRT\Auth\OAuthAccountService::complete_login($user, !empty($payload['rememberme']));
$redirect = function_exists('aksrt_get_user_center_url') ? aksrt_get_user_center_url() : $redirect;
return $this->success(['message' => __('账号绑定成功', 'aksrt'), 'redirect' => $redirect]);
}
$result = \AKSRT\Auth\Service::login_user($payload, 'login');
if (empty($result['success'])) {
return $this->error(__('账号或密码错误', 'aksrt'), 401);
}
return $this->success(['message' => $oauth_data ? __('账号绑定成功', 'aksrt') : __('登录成功', 'aksrt'), 'redirect' => $redirect]);
}
@@ -263,7 +290,10 @@ trait UserControllerAuthPhoneOperations {
if ($obk !== '' && !$oauth_data) {
return $this->error(__('绑定会话已过期,请重新使用第三方登录', 'aksrt'), 410);
}
if (class_exists('\AKSRT\Settings\Settings') && (\AKSRT\Settings\Settings::close_sign() || (!$oauth_data && !\AKSRT\Settings\Settings::registration_enabled()))) {
if (class_exists('\AKSRT\Settings\Settings') && (
\AKSRT\Settings\Settings::close_sign()
|| ($oauth_data ? !\AKSRT\Settings\Settings::oauth_registration_enabled() : !\AKSRT\Settings\Settings::registration_enabled())
)) {
return $this->error(__('当前已关闭新用户注册', 'aksrt'), 403);
}
if ($this->auth_rate_limited('register')) {
@@ -296,26 +326,47 @@ trait UserControllerAuthPhoneOperations {
}
if ($channel === 'phone') {
$payload['phone'] = $target;
$payload['_defer_login'] = !empty($oauth_data);
$result = \AKSRT\Auth\Service::register_by_phone($payload);
if (empty($result['success'])) {
return $this->error($this->auth_registration_error((string) ($result['error'] ?? __('注册失败', 'aksrt'))));
}
$user_id = (int) ($result['user_id'] ?? 0);
if ($oauth_data && $user_id > 0) {
\AKSRT\Auth\Service::finalize_oauth_binding($user_id, $oauth_data, $obk);
$binding = \AKSRT\Auth\Service::finalize_oauth_binding($user_id, $oauth_data, $obk);
if (is_wp_error($binding)) {
$this->delete_oauth_registration_user($user_id);
return $this->error($binding->get_error_message(), 409);
}
$user = get_user_by('id', $user_id);
if ($user instanceof \WP_User) {
\AKSRT\Auth\OAuthAccountService::complete_login($user, true);
}
}
return $this->success(['message' => $oauth_data ? __('注册并绑定成功', 'aksrt') : __('注册成功', 'aksrt'), 'redirect' => $result['redirect'] ?? home_url('/'), 'notice' => (string) ($result['notice'] ?? '')]);
}
$payload['user_email'] = sanitize_email($target);
$payload['user_pass'] = (string) ($payload['password'] ?? '');
$payload['user_pass_confirm'] = (string) ($payload['password_confirm'] ?? $payload['password'] ?? '');
$payload['_defer_login'] = !empty($oauth_data);
$result = \AKSRT\Auth\Service::register_user($payload);
if (empty($result['success'])) {
return $this->error($this->auth_registration_error((string) ($result['error'] ?? __('注册失败', 'aksrt'))));
}
$user_id = (int) ($result['user_id'] ?? 0);
if ($oauth_data && $user_id > 0) {
\AKSRT\Auth\Service::finalize_oauth_binding($user_id, $oauth_data, $obk);
$binding = \AKSRT\Auth\Service::finalize_oauth_binding($user_id, $oauth_data, $obk);
if (is_wp_error($binding)) {
wp_delete_user($user_id);
return $this->error($binding->get_error_message(), 409);
}
$user = get_user_by('id', $user_id);
if ($user instanceof \WP_User) {
if ($channel === 'email' && !empty($payload['_auth_code_verified'])) {
\AKSRT\Auth\Service::mark_verified_email($user_id);
}
\AKSRT\Auth\OAuthAccountService::complete_login($user, true);
}
}
return $this->success([
'message' => $oauth_data ? __('注册并绑定成功', 'aksrt') : __('注册成功', 'aksrt'),