|
|
|
|
<?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' => 'false'];
|
|
|
|
|
$assert = static function ($condition, $message) {
|
|
|
|
|
if (!$condition) {
|
|
|
|
|
throw new RuntimeException($message);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
$capsule = new Capsule();
|
|
|
|
|
$capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default');
|
|
|
|
|
$capsule->setAsGlobal();
|
|
|
|
|
$capsule->bootEloquent();
|
|
|
|
|
$schema = $capsule->getConnection('default')->getSchemaBuilder();
|
|
|
|
|
$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('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_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('instance_id')->nullable();
|
|
|
|
|
$table->integer('snapshot_id')->nullable();
|
|
|
|
|
$table->text('payload_cipher')->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_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('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],
|
|
|
|
|
]);
|
|
|
|
|
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_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, 'payload_cipher' => 'owner-job'],
|
|
|
|
|
['id' => 205, 'user_id' => 2, 'instance_id' => 101,
|
|
|
|
|
'snapshot_id' => 104, 'payload_cipher' => 'delegated-admin-job'],
|
|
|
|
|
['id' => 206, 'user_id' => 2, 'instance_id' => null,
|
|
|
|
|
'snapshot_id' => null, 'payload_cipher' => 'unrelated-admin-job'],
|
|
|
|
|
['id' => 305, 'user_id' => 3, 'instance_id' => 301,
|
|
|
|
|
'snapshot_id' => 304, 'payload_cipher' => 'other-user-job'],
|
|
|
|
|
]);
|
|
|
|
|
foreach (['cloud_service_periods', 'cloud_renewals'] as $tableName) {
|
|
|
|
|
Capsule::table($tableName)->insert([
|
|
|
|
|
['user_id' => 1], ['user_id' => 3],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
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('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((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.');
|
|
|
|
|
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_invoices', 'cloud_invoice_payments'] as $tableName) {
|
|
|
|
|
$assert(Capsule::table($tableName)->pluck('user_id')->all() === [3],
|
|
|
|
|
'Cloud account history deletion was not isolated: ' . $tableName);
|
|
|
|
|
}
|
|
|
|
|
$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('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, 'Disabled optional services were accessed.');
|
|
|
|
|
|
|
|
|
|
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' => 'failed',
|
|
|
|
|
]);
|
|
|
|
|
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.');
|
|
|
|
|
|
|
|
|
|
echo 'Manual account deletion SQLite checks passed.' . PHP_EOL;
|