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.
884 lines
39 KiB
PHP
884 lines
39 KiB
PHP
<?php
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Models\User;
|
|
use App\Services\UserDataDeletionService;
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
$System_Config = ['enable_log' => 'true'];
|
|
if (!defined('BASE_PATH')) {
|
|
define('BASE_PATH', sys_get_temp_dir() . '/virt-cloud-account-delete-test-' . bin2hex(random_bytes(6)));
|
|
}
|
|
$attachmentDirectory = BASE_PATH . '/storage/ticket-attachments';
|
|
if (!mkdir($attachmentDirectory, 0700, true) && !is_dir($attachmentDirectory)) {
|
|
throw new RuntimeException('Could not create account deletion attachment fixture directory.');
|
|
}
|
|
$assert = static function ($condition, $message) {
|
|
if (!$condition) {
|
|
throw new RuntimeException($message);
|
|
}
|
|
};
|
|
$capsule = new Capsule();
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'log');
|
|
$capsule->setAsGlobal();
|
|
$capsule->bootEloquent();
|
|
$defaultConnection = $capsule->getConnection('default');
|
|
$defaultConnection->statement('PRAGMA foreign_keys = ON');
|
|
$schema = $defaultConnection->getSchemaBuilder();
|
|
$logConnection = $capsule->getConnection('log');
|
|
$logConnection->statement('PRAGMA foreign_keys = ON');
|
|
$logSchema = $logConnection->getSchemaBuilder();
|
|
$logSchema->create('site_visit_events', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
});
|
|
$logSchema->create('site_security_events', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
});
|
|
$logSchema->create('user_email_log', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->string('email');
|
|
});
|
|
$schema->create('user', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('email')->unique();
|
|
$table->boolean('is_admin')->default(false);
|
|
$table->integer('login_session_version')->default(0);
|
|
$table->integer('ref_by')->default(0);
|
|
});
|
|
$schema->create('ticket', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('userid');
|
|
$table->integer('rootid')->default(0);
|
|
});
|
|
$schema->create('ticket_attachments', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('ticket_id');
|
|
$table->integer('message_id');
|
|
$table->integer('user_id');
|
|
$table->string('storage_key');
|
|
});
|
|
$schema->create('ticket_telegram_deliveries', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('ticket_id');
|
|
$table->integer('bound_admin_id');
|
|
$table->string('state')->default('sent');
|
|
$table->integer('updated_at')->default(0);
|
|
});
|
|
$schema->create('yipay_orders', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('user_id');
|
|
$table->string('order_no')->unique();
|
|
});
|
|
foreach (['yipay_callback_events', 'yipay_refunds'] as $tableName) {
|
|
$schema->create($tableName, static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('order_no');
|
|
});
|
|
}
|
|
$schema->create('cloud_instances', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id'); $table->string('status');
|
|
});
|
|
$schema->create('cloud_network_findings',
|
|
static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('instance_id');
|
|
$table->foreign('instance_id')->references('id')
|
|
->on('cloud_instances')->cascadeOnDelete();
|
|
});
|
|
$schema->create('cloud_network_alert_events',
|
|
static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('finding_id');
|
|
$table->foreign('finding_id')->references('id')
|
|
->on('cloud_network_findings')->cascadeOnDelete();
|
|
});
|
|
$schema->create('cloud_network_alert_deliveries',
|
|
static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->integer('event_id')->unique();
|
|
$table->string('binding_generation')->nullable();
|
|
$table->integer('bound_admin_id')->nullable();
|
|
$table->string('chat_id')->nullable();
|
|
$table->integer('telegram_message_id')->nullable();
|
|
$table->string('state');
|
|
$table->string('error_code')->default('');
|
|
$table->string('skip_reason')->default('');
|
|
$table->integer('dispatch_started_at')->nullable();
|
|
$table->integer('terminal_at')->nullable();
|
|
$table->integer('destination_redacted_at')->nullable();
|
|
$table->integer('updated_at')->default(0);
|
|
$table->foreign('event_id')->references('id')
|
|
->on('cloud_network_alert_events')->cascadeOnDelete();
|
|
});
|
|
$schema->create('cloud_orders', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id'); $table->string('status');
|
|
});
|
|
$schema->create('cloud_jobs', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
$table->integer('order_id')->nullable();
|
|
$table->integer('instance_id')->nullable();
|
|
$table->integer('snapshot_id')->nullable();
|
|
$table->string('kind')->nullable();
|
|
$table->string('execution_mode')->default('agent');
|
|
$table->string('phase')->default('terminal');
|
|
$table->string('outcome')->nullable()->default('succeeded');
|
|
$table->string('status')->default('succeeded');
|
|
$table->boolean('effect_may_have_applied')->default(false);
|
|
$table->boolean('safe_to_resubmit')->default(false);
|
|
$table->string('effect_resolution')->default('not_applicable');
|
|
$table->boolean('finalization_incident')->default(0);
|
|
$table->string('finalization_error_code')->nullable();
|
|
$table->text('sealed_payload')->nullable();
|
|
});
|
|
$schema->create('cloud_instance_snapshots', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
$table->integer('instance_id');
|
|
});
|
|
$schema->create('cloud_instance_firewalls', static function (Blueprint $table) {
|
|
$table->integer('instance_id')->primary(); $table->integer('user_id');
|
|
});
|
|
$schema->create('cloud_instance_firewall_versions', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('instance_id');
|
|
});
|
|
$schema->create('cloud_instance_firewall_rules', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('version_id');
|
|
});
|
|
foreach (['cloud_service_periods', 'cloud_renewals'] as $tableName) {
|
|
$schema->create($tableName, static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
});
|
|
}
|
|
$schema->create('cloud_backup_period_entitlements', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
});
|
|
foreach (['cloud_instance_network_restrictions',
|
|
'cloud_traffic_usage_settlement_buckets',
|
|
'cloud_traffic_settlement_receipts',
|
|
'cloud_traffic_instance_cursors', 'cloud_traffic_ledger_events',
|
|
'cloud_traffic_entitlements', 'cloud_traffic_pools'] as $tableName) {
|
|
$schema->create($tableName, static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
});
|
|
}
|
|
$schema->create('cloud_traffic_scan_failures', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->string('scan_kind');
|
|
$table->integer('subject_id');
|
|
});
|
|
$schema->create('cloud_invoices', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id');
|
|
});
|
|
$schema->create('cloud_invoice_payments', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('user_id'); $table->string('status');
|
|
});
|
|
$schema->create('cloud_mail_outbox', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('invoice_id');
|
|
});
|
|
$schema->create('cloud_service_compensations', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('requested_by_user_id');
|
|
$table->string('state'); $table->string('scope_type')->nullable();
|
|
$table->integer('scope_id')->nullable();
|
|
$table->text('scope_snapshot_json')->nullable();
|
|
});
|
|
$schema->create('cloud_service_compensation_items', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('batch_id');
|
|
$table->integer('user_id'); $table->string('state');
|
|
});
|
|
$schema->create('cloud_service_compensation_recipients', static function (Blueprint $table) {
|
|
$table->increments('id'); $table->integer('batch_id');
|
|
$table->integer('user_id'); $table->string('status');
|
|
});
|
|
$schema->create('email_verify', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('email');
|
|
});
|
|
$schema->create('configurations', static function (Blueprint $table) {
|
|
$table->increments('id');
|
|
$table->string('name')->unique();
|
|
$table->text('value');
|
|
});
|
|
Capsule::table('user')->insert([
|
|
['id' => 1, 'email' => 'deleted@example.com', 'is_admin' => 0, 'ref_by' => 0],
|
|
['id' => 2, 'email' => 'admin@example.com', 'is_admin' => 1, 'ref_by' => 0],
|
|
['id' => 3, 'email' => 'other@example.com', 'is_admin' => 0, 'ref_by' => 1],
|
|
]);
|
|
Capsule::table('ticket')->insert([
|
|
['id' => 10, 'userid' => 1, 'rootid' => 0],
|
|
['id' => 11, 'userid' => 2, 'rootid' => 10],
|
|
['id' => 20, 'userid' => 3, 'rootid' => 0],
|
|
]);
|
|
$attachmentFiles = [
|
|
'deleted-user.png' => 'deleted user attachment',
|
|
'deleted-admin-reply.png' => 'administrator reply attachment',
|
|
'retained-user.png' => 'retained user attachment',
|
|
];
|
|
foreach ($attachmentFiles as $storageKey => $contents) {
|
|
if (file_put_contents($attachmentDirectory . '/' . $storageKey, $contents) === false) {
|
|
throw new RuntimeException('Could not create ticket attachment fixture.');
|
|
}
|
|
}
|
|
Capsule::table('ticket_attachments')->insert([
|
|
['id' => 1, 'ticket_id' => 10, 'message_id' => 10, 'user_id' => 1,
|
|
'storage_key' => 'deleted-user.png'],
|
|
['id' => 2, 'ticket_id' => 10, 'message_id' => 11, 'user_id' => 2,
|
|
'storage_key' => 'deleted-admin-reply.png'],
|
|
['id' => 3, 'ticket_id' => 20, 'message_id' => 20, 'user_id' => 3,
|
|
'storage_key' => 'retained-user.png'],
|
|
]);
|
|
Capsule::table('ticket_telegram_deliveries')->insert([
|
|
['id' => 1, 'ticket_id' => 10, 'bound_admin_id' => 2],
|
|
['id' => 2, 'ticket_id' => 20, 'bound_admin_id' => 2],
|
|
]);
|
|
Capsule::table('yipay_orders')->insert([
|
|
['user_id' => 1, 'order_no' => 'deleted-order'],
|
|
['user_id' => 3, 'order_no' => 'retained-order'],
|
|
]);
|
|
foreach (['yipay_callback_events', 'yipay_refunds'] as $tableName) {
|
|
Capsule::table($tableName)->insert([
|
|
['order_no' => 'deleted-order'], ['order_no' => 'retained-order'],
|
|
]);
|
|
}
|
|
Capsule::table('cloud_instances')->insert([
|
|
['id' => 101, 'user_id' => 1, 'status' => 'deleted'],
|
|
['id' => 301, 'user_id' => 3, 'status' => 'active'],
|
|
]);
|
|
Capsule::table('cloud_network_findings')->insert([
|
|
['id' => 1, 'instance_id' => 101],
|
|
['id' => 2, 'instance_id' => 301],
|
|
]);
|
|
Capsule::table('cloud_network_alert_events')->insert([
|
|
['id' => 1, 'finding_id' => 1],
|
|
['id' => 2, 'finding_id' => 2],
|
|
]);
|
|
Capsule::table('cloud_network_alert_deliveries')->insert([
|
|
[
|
|
'id' => 1, 'event_id' => 1,
|
|
'binding_generation' => str_repeat('a', 32),
|
|
'bound_admin_id' => 1, 'chat_id' => '111111',
|
|
'telegram_message_id' => 101, 'state' => 'sent',
|
|
'terminal_at' => time() - 10, 'updated_at' => time() - 10,
|
|
],
|
|
[
|
|
'id' => 2, 'event_id' => 2,
|
|
'binding_generation' => str_repeat('b', 32),
|
|
'bound_admin_id' => 1, 'chat_id' => '111111',
|
|
'telegram_message_id' => 102, 'state' => 'sent',
|
|
'terminal_at' => time() - 10, 'updated_at' => time() - 10,
|
|
],
|
|
]);
|
|
Capsule::table('cloud_orders')->insert([
|
|
['id' => 102, 'user_id' => 1, 'status' => 'completed'],
|
|
['id' => 302, 'user_id' => 3, 'status' => 'completed'],
|
|
]);
|
|
Capsule::table('cloud_instance_snapshots')->insert([
|
|
['id' => 104, 'user_id' => 1, 'instance_id' => 101],
|
|
['id' => 304, 'user_id' => 3, 'instance_id' => 301],
|
|
]);
|
|
Capsule::table('cloud_instance_firewalls')->insert([
|
|
['instance_id' => 101, 'user_id' => 1],
|
|
['instance_id' => 301, 'user_id' => 3],
|
|
]);
|
|
Capsule::table('cloud_instance_firewall_versions')->insert([
|
|
['id' => 106, 'instance_id' => 101],
|
|
['id' => 306, 'instance_id' => 301],
|
|
]);
|
|
Capsule::table('cloud_instance_firewall_rules')->insert([
|
|
['id' => 107, 'version_id' => 106],
|
|
['id' => 307, 'version_id' => 306],
|
|
]);
|
|
Capsule::table('cloud_jobs')->insert([
|
|
['id' => 105, 'user_id' => 1, 'instance_id' => 101,
|
|
'snapshot_id' => 104, 'sealed_payload' => 'owner-job'],
|
|
['id' => 205, 'user_id' => 2, 'instance_id' => 101,
|
|
'snapshot_id' => 104, 'sealed_payload' => 'delegated-admin-job'],
|
|
['id' => 206, 'user_id' => 2, 'instance_id' => null,
|
|
'snapshot_id' => null, 'sealed_payload' => 'unrelated-admin-job'],
|
|
['id' => 305, 'user_id' => 3, 'instance_id' => 301,
|
|
'snapshot_id' => 304, 'sealed_payload' => 'other-user-job'],
|
|
]);
|
|
foreach (['cloud_service_periods', 'cloud_renewals'] as $tableName) {
|
|
Capsule::table($tableName)->insert([
|
|
['user_id' => 1], ['user_id' => 3],
|
|
]);
|
|
}
|
|
Capsule::table('cloud_backup_period_entitlements')->insert([
|
|
['user_id' => 1], ['user_id' => 3],
|
|
]);
|
|
foreach (['cloud_instance_network_restrictions',
|
|
'cloud_traffic_usage_settlement_buckets',
|
|
'cloud_traffic_settlement_receipts',
|
|
'cloud_traffic_instance_cursors', 'cloud_traffic_ledger_events',
|
|
'cloud_traffic_entitlements', 'cloud_traffic_pools'] as $tableName) {
|
|
Capsule::table($tableName)->insert([
|
|
['user_id' => 1], ['user_id' => 3],
|
|
]);
|
|
}
|
|
Capsule::table('cloud_traffic_scan_failures')->insert([
|
|
['scan_kind' => 'instance', 'subject_id' => 101],
|
|
['scan_kind' => 'instance', 'subject_id' => 301],
|
|
]);
|
|
Capsule::table('cloud_invoices')->insert([
|
|
['id' => 103, 'user_id' => 1], ['id' => 303, 'user_id' => 3],
|
|
]);
|
|
Capsule::table('cloud_invoice_payments')->insert([
|
|
['user_id' => 1, 'status' => 'paid_invoice'],
|
|
['user_id' => 3, 'status' => 'paid_invoice'],
|
|
]);
|
|
Capsule::table('cloud_mail_outbox')->insert([
|
|
['invoice_id' => 103], ['invoice_id' => 303],
|
|
]);
|
|
Capsule::table('cloud_service_compensations')->insert([
|
|
'id' => 1, 'requested_by_user_id' => 2, 'state' => 'completed',
|
|
'scope_type' => 'instance', 'scope_id' => 501,
|
|
'scope_snapshot_json' => json_encode([
|
|
'type' => 'instance', 'id' => 501, 'label' => 'tenant-secret-vm',
|
|
'instance_id' => 501, 'user_id' => 1, 'host_id' => 5,
|
|
], JSON_THROW_ON_ERROR),
|
|
]);
|
|
Capsule::table('cloud_service_compensation_items')->insert([
|
|
['id' => 1, 'batch_id' => 1, 'user_id' => 1, 'state' => 'applied'],
|
|
['id' => 2, 'batch_id' => 1, 'user_id' => 3, 'state' => 'applied'],
|
|
]);
|
|
Capsule::table('cloud_service_compensation_recipients')->insert([
|
|
['id' => 1, 'batch_id' => 1, 'user_id' => 1, 'status' => 'sent'],
|
|
['id' => 2, 'batch_id' => 1, 'user_id' => 3, 'status' => 'sent'],
|
|
]);
|
|
Capsule::table('email_verify')->insert([
|
|
['email' => 'deleted@example.com'], ['email' => 'other@example.com'],
|
|
]);
|
|
Capsule::table('configurations')->insert([
|
|
'name' => 'email_blacklist', 'value' => "deleted@example.com\n@blocked.example",
|
|
]);
|
|
$result = (new UserDataDeletionService())->deleteUsers([1, '2', 999], false);
|
|
$assert($result['deleted_user_ids'] === [1], 'Expected only the ordinary selected account to be deleted.');
|
|
$assert($result['skipped_user_ids'] === [2, 999], 'Administrator or missing account was not excluded.');
|
|
$assert(User::find(1) === null && User::find(2) !== null && User::find(3) !== null, 'User deletion was not isolated.');
|
|
$assert(Capsule::table('cloud_instances')->where('id', 301)->exists()
|
|
&& Capsule::table('cloud_network_findings')->pluck('id')->all() === [2]
|
|
&& Capsule::table('cloud_network_alert_events')->pluck('id')->all() === [2],
|
|
'Instance-centric abuse data did not follow tenant instance deletion.');
|
|
$retainedAbuseDelivery = Capsule::table('cloud_network_alert_deliveries')
|
|
->where('event_id', 2)->first();
|
|
$assert(Capsule::table('cloud_network_alert_deliveries')->count() === 1
|
|
&& $retainedAbuseDelivery !== null
|
|
&& $retainedAbuseDelivery->state === 'sent'
|
|
&& $retainedAbuseDelivery->bound_admin_id === null
|
|
&& $retainedAbuseDelivery->chat_id === null
|
|
&& $retainedAbuseDelivery->telegram_message_id === null
|
|
&& $retainedAbuseDelivery->destination_redacted_at !== null,
|
|
'Account deletion removed an event disposition or retained its former administrator destination.');
|
|
$assert((int) User::find(3)->ref_by === 0, 'Deleted referrer was not cleared.');
|
|
$assert(Capsule::table('ticket')->pluck('id')->all() === [20], 'Ticket replies from the deleted thread were retained.');
|
|
$assert(Capsule::table('ticket_attachments')->pluck('id')->all() === [3],
|
|
'Ticket attachment deletion was not isolated.');
|
|
$assert(Capsule::table('ticket_telegram_deliveries')->pluck('id')->all() === [2],
|
|
'Telegram ticket delivery mapping deletion was not isolated.');
|
|
$assert(!is_file($attachmentDirectory . '/deleted-user.png')
|
|
&& !is_file($attachmentDirectory . '/deleted-admin-reply.png')
|
|
&& is_file($attachmentDirectory . '/retained-user.png'),
|
|
'Ticket attachment files were not cleaned after a committed account deletion.');
|
|
foreach (['yipay_orders', 'yipay_callback_events', 'yipay_refunds'] as $tableName) {
|
|
$assert(Capsule::table($tableName)->pluck('order_no')->all() === ['retained-order'], 'Payment relation deletion was not isolated.');
|
|
}
|
|
foreach (['cloud_instances', 'cloud_orders', 'cloud_instance_snapshots',
|
|
'cloud_instance_firewalls', 'cloud_service_periods', 'cloud_renewals',
|
|
'cloud_backup_period_entitlements', 'cloud_invoices',
|
|
'cloud_invoice_payments', 'cloud_instance_network_restrictions',
|
|
'cloud_traffic_usage_settlement_buckets',
|
|
'cloud_traffic_settlement_receipts',
|
|
'cloud_traffic_instance_cursors', 'cloud_traffic_ledger_events',
|
|
'cloud_traffic_entitlements', 'cloud_traffic_pools'] as $tableName) {
|
|
$assert(Capsule::table($tableName)->pluck('user_id')->all() === [3],
|
|
'Cloud account history deletion was not isolated: ' . $tableName);
|
|
}
|
|
$assert(Capsule::table('cloud_traffic_scan_failures')
|
|
->pluck('subject_id')->all() === [301],
|
|
'Cloud traffic scan quarantine deletion was not isolated.');
|
|
$assert(Capsule::table('cloud_instance_firewall_versions')->pluck('id')->all()
|
|
=== [306]
|
|
&& Capsule::table('cloud_instance_firewall_rules')->pluck('id')->all()
|
|
=== [307],
|
|
'Tenant deletion orphaned or over-deleted firewall policy history.');
|
|
$assert(Capsule::table('cloud_jobs')->orderBy('id')->pluck('id')->all()
|
|
=== [206, 305],
|
|
'Tenant deletion retained delegated jobs or removed unrelated administrator jobs.');
|
|
$assert(Capsule::table('cloud_mail_outbox')->pluck('invoice_id')->all() === [303],
|
|
'Cloud invoice mail history deletion was not isolated.');
|
|
$assert(Capsule::table('cloud_service_compensations')->pluck('id')->all() === [1],
|
|
'Account deletion removed the immutable compensation batch audit.');
|
|
$retainedCompensationScope = json_decode((string) Capsule::table(
|
|
'cloud_service_compensations'
|
|
)->where('id', 1)->value('scope_snapshot_json'), true, 16,
|
|
JSON_THROW_ON_ERROR);
|
|
$assert($retainedCompensationScope === [
|
|
'type' => 'instance', 'id' => 501, 'label' => '单实例 #501',
|
|
'instance_id' => 501,
|
|
], 'Account deletion retained tenant PII in compensation batch audit.');
|
|
$assert(Capsule::table('cloud_service_compensation_items')->pluck('user_id')->all()
|
|
=== [3]
|
|
&& Capsule::table('cloud_service_compensation_recipients')
|
|
->pluck('user_id')->all() === [3],
|
|
'Compensation tenant details were not deleted in isolation.');
|
|
$assert(Capsule::table('email_verify')->pluck('email')->all() === ['other@example.com'], 'Email verification cleanup was not isolated.');
|
|
$assert(Capsule::table('configurations')->where('name', 'email_blacklist')->value('value') === "deleted@example.com\n@blocked.example", 'Manual blacklist was changed.');
|
|
$assert($result['cache_error'] === false
|
|
&& $result['log_rows_deleted'] === 0,
|
|
'Log account history was not deleted in isolation.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 4, 'email' => 'payment-pending@example.com', 'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_invoice_payments')->insert([
|
|
'user_id' => 4, 'status' => 'creation_unknown',
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([4], false);
|
|
throw new RuntimeException('Account with an unresolved payment was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '支付安全栅栏'),
|
|
'Unresolved-payment deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(4) !== null, 'Unresolved-payment deletion was not rolled back.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 5, 'email' => 'provision-invoice@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_orders')->insert([
|
|
'id' => 502, 'user_id' => 5, 'status' => 'awaiting_payment',
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([5], false);
|
|
throw new RuntimeException('Account with an unpaid provisioning invoice was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '开通中的订单'),
|
|
'Unpaid-provisioning deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(5) !== null
|
|
&& Capsule::table('cloud_orders')->where('id', 502)->exists(),
|
|
'Unpaid-provisioning deletion was not rolled back.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 6, 'email' => 'provision-fenced@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_orders')->insert([
|
|
'id' => 602, 'user_id' => 6, 'status' => 'failed',
|
|
]);
|
|
Capsule::table('cloud_jobs')->insert([
|
|
'id' => 605, 'user_id' => 6, 'order_id' => 602,
|
|
'instance_id' => null, 'snapshot_id' => null,
|
|
'kind' => 'create', 'execution_mode' => 'agent',
|
|
'phase' => 'terminal', 'outcome' => 'failed', 'status' => 'failed',
|
|
'effect_may_have_applied' => true, 'safe_to_resubmit' => false,
|
|
'effect_resolution' => 'unknown',
|
|
'sealed_payload' => 'unknown-effect-create-audit',
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([6], false);
|
|
throw new RuntimeException(
|
|
'Account with an unknown-effect terminal task was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '资源安全栅栏'),
|
|
'Unknown-effect terminal task deletion returned the wrong error.');
|
|
}
|
|
$fencedJob = Capsule::table('cloud_jobs')->where('id', 605)->first();
|
|
$assert(User::find(6) !== null
|
|
&& Capsule::table('cloud_orders')->where('id', 602)
|
|
->where('status', 'failed')->exists()
|
|
&& $fencedJob !== null
|
|
&& $fencedJob->order_id === 602
|
|
&& $fencedJob->instance_id === null
|
|
&& $fencedJob->kind === 'create'
|
|
&& $fencedJob->phase === 'terminal'
|
|
&& $fencedJob->outcome === 'failed'
|
|
&& $fencedJob->status === 'failed'
|
|
&& (int) $fencedJob->effect_may_have_applied === 1
|
|
&& (int) $fencedJob->safe_to_resubmit === 0
|
|
&& $fencedJob->effect_resolution === 'unknown'
|
|
&& $fencedJob->sealed_payload === 'unknown-effect-create-audit',
|
|
'Unknown-effect terminal provisioning fence lost its order or job audit.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 8, 'email' => 'compensation-applying@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_service_compensations')->insert([
|
|
'id' => 2, 'requested_by_user_id' => 2, 'state' => 'applying',
|
|
]);
|
|
Capsule::table('cloud_service_compensation_items')->insert([
|
|
'id' => 8, 'batch_id' => 2, 'user_id' => 8, 'state' => 'applying',
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([8], false);
|
|
throw new RuntimeException('Account with an applying compensation was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '进行中的实例补偿'),
|
|
'Applying-compensation deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(8) !== null
|
|
&& Capsule::table('cloud_service_compensation_items')->where('id', 8)->exists()
|
|
&& Capsule::table('cloud_service_compensations')->where('id', 2)->exists(),
|
|
'Applying-compensation deletion fence did not preserve its audit rows.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 9, 'email' => 'compensation-sending@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_service_compensations')->insert([
|
|
'id' => 3, 'requested_by_user_id' => 2, 'state' => 'completed',
|
|
]);
|
|
Capsule::table('cloud_service_compensation_recipients')->insert([
|
|
'id' => 9, 'batch_id' => 3, 'user_id' => 9, 'status' => 'sending',
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([9], false);
|
|
throw new RuntimeException('Account with a sending compensation notice was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '投递中的补偿通知'),
|
|
'Sending-compensation deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(9) !== null
|
|
&& Capsule::table('cloud_service_compensation_recipients')
|
|
->where('id', 9)->exists()
|
|
&& Capsule::table('cloud_service_compensations')->where('id', 3)->exists(),
|
|
'Sending-compensation deletion fence did not preserve its audit rows.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 10, 'email' => 'compensation-pending@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_service_compensations')->insert([
|
|
'id' => 4, 'requested_by_user_id' => 2, 'state' => 'pending',
|
|
]);
|
|
Capsule::table('cloud_service_compensation_items')->insert([
|
|
'id' => 10, 'batch_id' => 4, 'user_id' => 10, 'state' => 'pending',
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([10], false);
|
|
throw new RuntimeException('Account in a pending compensation batch was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '进行中的实例补偿'),
|
|
'Pending-compensation deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(10) !== null
|
|
&& Capsule::table('cloud_service_compensation_items')
|
|
->where('id', 10)->exists(),
|
|
'Pending-compensation deletion fence removed its item.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 11, 'email' => 'compensation-mail-pending@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_service_compensations')->insert([
|
|
'id' => 5, 'requested_by_user_id' => 2, 'state' => 'completed',
|
|
]);
|
|
Capsule::table('cloud_service_compensation_recipients')->insert([
|
|
'id' => 11, 'batch_id' => 5, 'user_id' => 11, 'status' => 'pending',
|
|
]);
|
|
$pendingMailDeletion = (new UserDataDeletionService())
|
|
->deleteUsers([11], false);
|
|
$assert($pendingMailDeletion['deleted_user_ids'] === [11]
|
|
&& User::find(11) === null
|
|
&& !Capsule::table('cloud_service_compensation_recipients')
|
|
->where('id', 11)->exists()
|
|
&& Capsule::table('cloud_service_compensations')->where('id', 5)->exists(),
|
|
'Locked pending compensation mail was not safely removed with the account.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 12, 'email' => 'former-alert-admin@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_network_findings')->insert([
|
|
'id' => 12, 'instance_id' => 301,
|
|
]);
|
|
Capsule::table('cloud_network_alert_events')->insert([
|
|
'id' => 12, 'finding_id' => 12,
|
|
]);
|
|
Capsule::table('cloud_network_alert_deliveries')->insert([
|
|
'id' => 12, 'event_id' => 12,
|
|
'binding_generation' => str_repeat('c', 32),
|
|
'bound_admin_id' => 12, 'chat_id' => '121212',
|
|
'telegram_message_id' => null, 'state' => 'dispatching',
|
|
'dispatch_started_at' => time(), 'terminal_at' => null,
|
|
'updated_at' => time(),
|
|
]);
|
|
$logConnection->table('site_visit_events')->insert([
|
|
'id' => 12, 'user_id' => 12,
|
|
]);
|
|
Capsule::table('configurations')->insert([
|
|
'id' => 12,
|
|
'name' => \App\Services\TicketTelegramConfig::BINDING,
|
|
'value' => json_encode([
|
|
'bound_admin_id' => 12,
|
|
'chat_id' => '121212',
|
|
'binding_generation' => str_repeat('c', 32),
|
|
], JSON_THROW_ON_ERROR),
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([12], false);
|
|
throw new RuntimeException('Dispatching abuse alert recipient was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '投递中的网络滥用告警'),
|
|
'Dispatching abuse alert deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(12) !== null
|
|
&& Capsule::table('cloud_network_alert_deliveries')
|
|
->where('id', 12)->value('state') === 'dispatching',
|
|
'Dispatching abuse alert deletion fence did not roll back.');
|
|
$assert($logConnection->table('site_visit_events')
|
|
->where('user_id', 12)->exists()
|
|
&& Capsule::table('configurations')->where('id', 12)->exists(),
|
|
'Dispatching abuse guard ran after irreversible log or binding cleanup.');
|
|
Capsule::table('cloud_network_alert_deliveries')->where('id', 12)->update([
|
|
'state' => 'unknown', 'terminal_at' => time(),
|
|
]);
|
|
$formerAdminDeletion = (new UserDataDeletionService())
|
|
->deleteUsers([12], false);
|
|
$formerAdminDelivery = Capsule::table('cloud_network_alert_deliveries')
|
|
->where('id', 12)->first();
|
|
$assert($formerAdminDeletion['deleted_user_ids'] === [12]
|
|
&& $formerAdminDelivery?->state === 'unknown'
|
|
&& $formerAdminDelivery->bound_admin_id === null
|
|
&& $formerAdminDelivery->chat_id === null
|
|
&& $formerAdminDelivery->destination_redacted_at !== null,
|
|
'Former alert administrator was not anonymized without losing event disposition.');
|
|
$assert(!Capsule::table('configurations')->where('id', 12)->exists(),
|
|
'Deleted former administrator retained an active Telegram binding.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 13, 'email' => 'pending-binding-admin@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('configurations')->insert([
|
|
'id' => 13,
|
|
'name' => \App\Services\TicketTelegramConfig::BINDING,
|
|
'value' => json_encode([
|
|
'bound_admin_id' => 2,
|
|
'chat_id' => '222222',
|
|
'binding_generation' => str_repeat('d', 32),
|
|
'pending_admin_id' => 13,
|
|
'pending_hash' => str_repeat('e', 64),
|
|
'pending_expires_at' => time() + 300,
|
|
], JSON_THROW_ON_ERROR),
|
|
]);
|
|
(new UserDataDeletionService())->deleteUsers([13], false);
|
|
$retainedBinding = json_decode((string) Capsule::table('configurations')
|
|
->where('id', 13)->value('value'), true);
|
|
$assert(($retainedBinding['bound_admin_id'] ?? 0) === 2
|
|
&& !array_key_exists('pending_admin_id', $retainedBinding)
|
|
&& !array_key_exists('pending_hash', $retainedBinding)
|
|
&& !array_key_exists('pending_expires_at', $retainedBinding),
|
|
'Deleted pending administrator was left consumable after user-id reuse.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 14, 'email' => 'ticket-dispatch-admin@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('ticket_telegram_deliveries')->insert([
|
|
'id' => 14, 'ticket_id' => 0, 'bound_admin_id' => 14,
|
|
'state' => 'dispatching', 'updated_at' => time(),
|
|
]);
|
|
$logConnection->table('site_visit_events')->insert([
|
|
'id' => 14, 'user_id' => 14,
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([14], false);
|
|
throw new RuntimeException('Dispatching ticket recipient was deleted.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '投递中的工单通知'),
|
|
'Dispatching ticket deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(14) !== null
|
|
&& $logConnection->table('site_visit_events')
|
|
->where('user_id', 14)->exists(),
|
|
'Ticket dispatch guard ran after irreversible log deletion.');
|
|
Capsule::table('ticket_telegram_deliveries')->where('id', 14)->update([
|
|
'state' => 'failed',
|
|
]);
|
|
(new UserDataDeletionService())->deleteUsers([14], false);
|
|
$assert(User::find(14) === null
|
|
&& !$logConnection->table('site_visit_events')
|
|
->where('user_id', 14)->exists(),
|
|
'Known terminal ticket delivery prevented safe account deletion.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 16, 'email' => 'stale-ticket-admin@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('ticket_telegram_deliveries')->insert([
|
|
'id' => 16, 'ticket_id' => 0, 'bound_admin_id' => 16,
|
|
'state' => 'dispatching', 'updated_at' => time() - 40,
|
|
]);
|
|
(new UserDataDeletionService())->deleteUsers([16], false);
|
|
$assert(User::find(16) === null
|
|
&& !Capsule::table('ticket_telegram_deliveries')->where('id', 16)->exists(),
|
|
'Stale ticket dispatch permanently blocked account deletion.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 18, 'email' => 'ticket-owner-dispatch@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('ticket')->insert([
|
|
'id' => 180, 'userid' => 18, 'rootid' => 0,
|
|
]);
|
|
Capsule::table('ticket_telegram_deliveries')->insert([
|
|
'id' => 18, 'ticket_id' => 180, 'bound_admin_id' => 2,
|
|
'state' => 'dispatching', 'updated_at' => time(),
|
|
]);
|
|
$logConnection->table('site_visit_events')->insert([
|
|
'id' => 18, 'user_id' => 18,
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([18], false);
|
|
throw new RuntimeException('Ticket owner was deleted during dispatch.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '投递中的工单通知'),
|
|
'Owner-ticket dispatch deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(18) !== null
|
|
&& Capsule::table('ticket_telegram_deliveries')->where('id', 18)->exists()
|
|
&& $logConnection->table('site_visit_events')
|
|
->where('user_id', 18)->exists(),
|
|
'Owner-ticket dispatch fence allowed partial deletion.');
|
|
Capsule::table('ticket_telegram_deliveries')->where('id', 18)->update([
|
|
'state' => 'unknown',
|
|
]);
|
|
(new UserDataDeletionService())->deleteUsers([18], false);
|
|
$assert(User::find(18) === null
|
|
&& !Capsule::table('ticket_telegram_deliveries')->where('id', 18)->exists(),
|
|
'Terminal owner-ticket delivery prevented safe account deletion.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 15, 'email' => 'alert-owner@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => 315, 'user_id' => 15, 'status' => 'deleted',
|
|
]);
|
|
Capsule::table('cloud_network_findings')->insert([
|
|
'id' => 15, 'instance_id' => 315,
|
|
]);
|
|
Capsule::table('cloud_network_alert_events')->insert([
|
|
'id' => 15, 'finding_id' => 15,
|
|
]);
|
|
Capsule::table('cloud_network_alert_deliveries')->insert([
|
|
'id' => 15, 'event_id' => 15,
|
|
'binding_generation' => str_repeat('f', 32),
|
|
'bound_admin_id' => 2, 'chat_id' => '222222',
|
|
'telegram_message_id' => null, 'state' => 'dispatching',
|
|
'dispatch_started_at' => time(), 'terminal_at' => null,
|
|
'updated_at' => time(),
|
|
]);
|
|
$logConnection->table('site_visit_events')->insert([
|
|
'id' => 15, 'user_id' => 15,
|
|
]);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([15], false);
|
|
throw new RuntimeException('Owner event was cascaded during dispatch.');
|
|
} catch (RuntimeException $error) {
|
|
$assert(str_contains($error->getMessage(), '投递中的网络滥用告警'),
|
|
'Owner-event dispatch deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(15) !== null
|
|
&& Capsule::table('cloud_network_alert_events')->where('id', 15)->exists()
|
|
&& $logConnection->table('site_visit_events')
|
|
->where('user_id', 15)->exists(),
|
|
'Owner-event dispatch fence allowed partial account deletion.');
|
|
Capsule::table('cloud_network_alert_deliveries')->where('id', 15)->update([
|
|
'state' => 'unknown', 'terminal_at' => time(),
|
|
]);
|
|
(new UserDataDeletionService())->deleteUsers([15], false);
|
|
$assert(User::find(15) === null
|
|
&& !Capsule::table('cloud_network_alert_events')->where('id', 15)->exists()
|
|
&& !Capsule::table('cloud_network_alert_deliveries')->where('id', 15)->exists(),
|
|
'Terminal owner event did not cascade with tenant deletion.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 17, 'email' => 'stale-alert-owner@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('cloud_instances')->insert([
|
|
'id' => 317, 'user_id' => 17, 'status' => 'deleted',
|
|
]);
|
|
Capsule::table('cloud_network_findings')->insert([
|
|
'id' => 17, 'instance_id' => 317,
|
|
]);
|
|
Capsule::table('cloud_network_alert_events')->insert([
|
|
'id' => 17, 'finding_id' => 17,
|
|
]);
|
|
Capsule::table('cloud_network_alert_deliveries')->insert([
|
|
'id' => 17, 'event_id' => 17,
|
|
'binding_generation' => str_repeat('a', 32),
|
|
'bound_admin_id' => 2, 'chat_id' => '222222',
|
|
'telegram_message_id' => null, 'state' => 'dispatching',
|
|
'dispatch_started_at' => time() - 40, 'terminal_at' => null,
|
|
'updated_at' => time() - 40,
|
|
]);
|
|
(new UserDataDeletionService())->deleteUsers([17], false);
|
|
$assert(User::find(17) === null
|
|
&& !Capsule::table('cloud_network_alert_events')->where('id', 17)->exists()
|
|
&& !Capsule::table('cloud_network_alert_deliveries')->where('id', 17)->exists(),
|
|
'Stale abuse dispatch permanently blocked owner deletion.');
|
|
|
|
Capsule::table('user')->insert([
|
|
'id' => 7, 'email' => 'rollback-attachment@example.com',
|
|
'is_admin' => 0, 'ref_by' => 0,
|
|
]);
|
|
Capsule::table('ticket')->insert([
|
|
'id' => 70, 'userid' => 7, 'rootid' => 0,
|
|
]);
|
|
Capsule::table('ticket_attachments')->insert([
|
|
'id' => 7, 'ticket_id' => 70, 'message_id' => 70, 'user_id' => 7,
|
|
'storage_key' => 'rollback-user.png',
|
|
]);
|
|
Capsule::table('ticket_telegram_deliveries')->insert([
|
|
'id' => 7, 'ticket_id' => 70, 'bound_admin_id' => 2,
|
|
]);
|
|
$rollbackAttachment = $attachmentDirectory . '/rollback-user.png';
|
|
if (file_put_contents($rollbackAttachment, 'rollback attachment') === false) {
|
|
throw new RuntimeException('Could not create rollback attachment fixture.');
|
|
}
|
|
Capsule::statement(
|
|
"CREATE TRIGGER prevent_user_7_delete BEFORE DELETE ON user "
|
|
. "WHEN OLD.id = 7 BEGIN SELECT RAISE(ABORT, 'forced user deletion failure'); END"
|
|
);
|
|
try {
|
|
(new UserDataDeletionService())->deleteUsers([7], false);
|
|
throw new RuntimeException('Forced account deletion failure did not abort.');
|
|
} catch (Throwable $error) {
|
|
$assert(str_contains($error->getMessage(), 'forced user deletion failure'),
|
|
'Forced account deletion returned the wrong error.');
|
|
}
|
|
$assert(User::find(7) !== null
|
|
&& Capsule::table('ticket')->where('id', 70)->exists()
|
|
&& Capsule::table('ticket_attachments')->where('id', 7)->exists()
|
|
&& Capsule::table('ticket_telegram_deliveries')->where('id', 7)->exists(),
|
|
'Database rows did not roll back after the forced account deletion failure.');
|
|
$assert(is_file($rollbackAttachment),
|
|
'Ticket attachment file was deleted before the account transaction committed.');
|
|
|
|
$deletionSource = file_get_contents(dirname(__DIR__)
|
|
. '/app/Services/UserDataDeletionService.php');
|
|
$instanceLockAt = is_string($deletionSource)
|
|
? strpos($deletionSource, '$ownedInstanceIds = $connection->table')
|
|
: false;
|
|
$eventLockAt = is_string($deletionSource)
|
|
? strpos($deletionSource, '$lockedEventIds = $connection->table')
|
|
: false;
|
|
$assert($instanceLockAt !== false && $eventLockAt !== false
|
|
&& $instanceLockAt < $eventLockAt
|
|
&& str_contains($deletionSource,
|
|
"->whereIn('user_id', \$userIds)->orderBy('id')->lockForUpdate()"),
|
|
'Account deletion no longer locks owned instances before abuse events.');
|
|
|
|
Capsule::statement('DROP TRIGGER prevent_user_7_delete');
|
|
@unlink($rollbackAttachment);
|
|
@unlink($attachmentDirectory . '/retained-user.png');
|
|
@rmdir($attachmentDirectory);
|
|
@rmdir(BASE_PATH . '/storage');
|
|
@rmdir(BASE_PATH);
|
|
|
|
echo 'Manual account deletion SQLite checks passed.' . PHP_EOL;
|