fix(profile): align account dialogs and contracts

This commit is contained in:
2026-10-06 01:09:04 +08:00
parent f646fa8573
commit 248912f610
17 changed files with 1339 additions and 832 deletions
@@ -5,10 +5,13 @@ declare(strict_types=1);
namespace {
define('ABSPATH', __DIR__ . '/');
define('MINUTE_IN_SECONDS', 60);
define('HOUR_IN_SECONDS', 3600);
$GLOBALS['aksrt_profile_meta'] = [];
$GLOBALS['aksrt_profile_users'] = [];
$GLOBALS['aksrt_nonce_valid'] = true;
$GLOBALS['aksrt_can_edit_user'] = true;
$GLOBALS['aksrt_current_user_id'] = 7;
$GLOBALS['aksrt_transients'] = [];
$GLOBALS['aksrt_expected_sms_code'] = '246810';
@@ -34,7 +37,7 @@ namespace {
function is_admin(): bool { return true; }
function current_user_can(string $capability, ...$args): bool { return $GLOBALS['aksrt_can_edit_user']; }
function is_user_logged_in(): bool { return true; }
function get_current_user_id(): int { return 7; }
function get_current_user_id(): int { return (int) ($GLOBALS['aksrt_current_user_id'] ?? 7); }
function get_user_meta(int $user_id, string $key, bool $single = false) {
$value = $GLOBALS['aksrt_profile_meta'][$user_id][$key] ?? '';
return $single ? $value : ($value === '' ? [] : [$value]);
@@ -48,7 +51,7 @@ namespace {
foreach ($GLOBALS['aksrt_profile_meta'] as $user_id => $meta) {
if (in_array((int) $user_id, array_map('intval', $args['exclude'] ?? []), true)) continue;
if (($meta[$args['meta_key'] ?? ''] ?? null) !== ($args['meta_value'] ?? null)) continue;
$results[] = ($args['fields'] ?? '') === 'ids' ? (int) $user_id : new WP_User((int) $user_id);
$results[] = in_array(($args['fields'] ?? ''), ['ids', 'ID'], true) ? (int) $user_id : new WP_User((int) $user_id);
if (count($results) >= (int) ($args['number'] ?? 1)) break;
}
return $results;
@@ -99,6 +102,12 @@ namespace AKSRT\Sms {
}
}
namespace AKSRT\Settings {
final class Settings {
public static function get_all(): array { return ['bind_phone' => true]; }
}
}
namespace AKSRT\Auth {
require dirname(__DIR__, 2) . '/src/Auth/ServicePart1b.php';
final class PhoneHarnessService { use ServicePart1b; }
@@ -150,10 +159,25 @@ namespace {
check(empty($same_phone['success']), 'binding accepted the current phone as a change');
$phone_to_bind = '13800000010';
$otp_key = 'aksrt_auth_code_' . md5('bind_phone|' . strtolower(trim($phone_to_bind)));
$otp_key = 'aksrt_auth_code_' . md5('account_bind_new_phone_7|' . strtolower(trim($phone_to_bind)));
$operations = new \AKSRT\Admin\PhoneOperationsHarness();
$GLOBALS['aksrt_transients'][$otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$old_phone = '13800000007';
$old_otp_key = 'aksrt_auth_code_' . md5('account_verify_old_phone_7|' . strtolower(trim($old_phone)));
$GLOBALS['aksrt_transients'][$old_otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$GLOBALS['aksrt_expected_sms_code'] = '246810';
$wrong_old_code = $operations->user_verify_phone(new WP_REST_Request(['phone' => $old_phone, 'code' => '000000']));
check(empty($wrong_old_code->data['success']), 'REST phone replacement accepted an incorrect old-number code');
check(($GLOBALS['aksrt_transients'][$old_otp_key]['attempts'] ?? 0) === 1, 'incorrect old-number code attempt was not recorded');
$GLOBALS['aksrt_transients'][$old_otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() - 1];
$expired_old_code = $operations->user_verify_phone(new WP_REST_Request(['phone' => $old_phone, 'code' => '246810']));
check(empty($expired_old_code->data['success']), 'REST phone replacement accepted an expired old-number code');
check(!isset($GLOBALS['aksrt_transients'][$old_otp_key]), 'expired old-number code was not discarded');
$GLOBALS['aksrt_transients'][$old_otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$verified_old_phone = $operations->user_verify_phone(new WP_REST_Request(['phone' => $old_phone, 'code' => '246810']));
check(!empty($verified_old_phone->data['success']), 'REST phone replacement rejected the correct old-number code');
check(!isset($GLOBALS['aksrt_transients'][$old_otp_key]), 'valid old-number code was not consumed');
check(($GLOBALS['aksrt_transients']['aksrt_account_phone_old_7']['phone'] ?? '') === $old_phone, 'old-number proof was not issued for the current account');
$GLOBALS['aksrt_transients'][$otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$wrong_code = $operations->user_bind_phone(new WP_REST_Request(['phone' => $phone_to_bind, 'code' => '000000']));
check(empty($wrong_code->data['success']), 'REST phone binding accepted an incorrect SMS code');
check(($GLOBALS['aksrt_transients'][$otp_key]['attempts'] ?? 0) === 1, 'incorrect SMS code attempt was not recorded');
@@ -161,11 +185,29 @@ namespace {
$expired_code = $operations->user_bind_phone(new WP_REST_Request(['phone' => $phone_to_bind, 'code' => '246810']));
check(empty($expired_code->data['success']), 'REST phone binding accepted an expired SMS code');
check(!isset($GLOBALS['aksrt_transients'][$otp_key]), 'expired SMS code was not discarded');
$duplicate_phone = '13800000008';
$duplicate_otp_key = 'aksrt_auth_code_' . md5('account_bind_new_phone_7|' . strtolower(trim($duplicate_phone)));
$GLOBALS['aksrt_transients'][$duplicate_otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$duplicate_code = $operations->user_bind_phone(new WP_REST_Request(['phone' => $duplicate_phone, 'code' => '246810']));
check(empty($duplicate_code->data['success']), 'REST phone binding accepted a number owned by another user');
check(isset($GLOBALS['aksrt_transients'][$duplicate_otp_key]), 'duplicate-number validation consumed the valid SMS code');
$GLOBALS['aksrt_transients'][$otp_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$valid_code = $operations->user_bind_phone(new WP_REST_Request(['phone' => $phone_to_bind, 'code' => '246810']));
check(!empty($valid_code->data['success']), 'REST phone binding rejected the correct SMS code');
check(get_user_meta(7, 'phone_number', true) === $phone_to_bind, 'REST phone binding did not store the canonical phone meta');
check(!isset($GLOBALS['aksrt_transients'][$otp_key]), 'valid SMS code was not consumed');
check(!isset($GLOBALS['aksrt_transients']['aksrt_account_phone_old_7']), 'successful phone change did not consume the old-number proof');
$initial_bind_user_id = 9;
$initial_bind_phone = '13800000011';
$initial_bind_code_key = 'aksrt_auth_code_' . md5('account_bind_new_phone_9|' . strtolower(trim($initial_bind_phone)));
$GLOBALS['aksrt_current_user_id'] = $initial_bind_user_id;
$GLOBALS['aksrt_transients'][$initial_bind_code_key] = ['hash' => 'fixture-hash', 'attempts' => 0, 'expires' => time() + 300];
$initial_bind = $operations->user_bind_phone(new WP_REST_Request(['phone' => $initial_bind_phone, 'code' => '246810']));
check(!empty($initial_bind->data['success']), 'first-time phone binding incorrectly required an old-number proof');
check(get_user_meta($initial_bind_user_id, 'phone_number', true) === $initial_bind_phone, 'first-time phone binding did not store the canonical phone meta');
check(!isset($GLOBALS['aksrt_transients'][$initial_bind_code_key]), 'successful first-time phone binding did not consume its SMS code');
$GLOBALS['aksrt_current_user_id'] = 7;
$_POST = [
'aksrt_profile_admin_nonce' => 'ok',