|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models {
|
|
|
|
|
class User
|
|
|
|
|
{
|
|
|
|
|
public $id;
|
|
|
|
|
public $email;
|
|
|
|
|
public $is_admin;
|
|
|
|
|
public $login_session_version;
|
|
|
|
|
public function __construct(array $row = [])
|
|
|
|
|
{
|
|
|
|
|
$this->id = $row['id'] ?? null;
|
|
|
|
|
$this->email = $row['email'] ?? null;
|
|
|
|
|
$this->is_admin = $row['is_admin'] ?? null;
|
|
|
|
|
$this->login_session_version = $row['login_session_version'] ?? 0;
|
|
|
|
|
}
|
|
|
|
|
public function getConnection() { return $GLOBALS['deleteTestConnection']; }
|
|
|
|
|
public static function whereIn($column, $values) { return (new self())->getConnection()->table('user')->whereIn($column, $values); }
|
|
|
|
|
}
|
|
|
|
|
class EmailLog
|
|
|
|
|
{
|
|
|
|
|
public function getConnection() { throw new \RuntimeException('Simulated unavailable log database'); }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace App\Services {
|
|
|
|
|
class Config
|
|
|
|
|
{
|
|
|
|
|
public static $logging = 'false';
|
|
|
|
|
public static function get($key, $default = null) { return $key === 'enable_log' ? self::$logging : $default; }
|
|
|
|
|
}
|
|
|
|
|
class LoginSessionRevocationService
|
|
|
|
|
{
|
|
|
|
|
public static $revoked = [];
|
|
|
|
|
public static function revokeUser(\App\Models\User $user)
|
|
|
|
|
{
|
|
|
|
|
self::$revoked[] = $user->id;
|
|
|
|
|
$connection = $user->getConnection();
|
|
|
|
|
$connection->table('user')->whereIn('id', [$user->id])->update(['login_session_version' => 1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
require dirname(__DIR__) . '/app/Services/UserDataDeletionService.php';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
class DeleteTestCollection implements \IteratorAggregate
|
|
|
|
|
{
|
|
|
|
|
private $rows;
|
|
|
|
|
public function __construct(array $rows) { $this->rows = $rows; }
|
|
|
|
|
public function isEmpty() { return $this->rows === []; }
|
|
|
|
|
public function all() { return $this->rows; }
|
|
|
|
|
public function getIterator(): \Traversable { return new \ArrayIterator($this->rows); }
|
|
|
|
|
}
|
|
|
|
|
class DeleteTestSchema
|
|
|
|
|
{
|
|
|
|
|
private $connection;
|
|
|
|
|
public function __construct($connection) { $this->connection = $connection; }
|
|
|
|
|
public function hasTable($table) { return array_key_exists($table, $this->connection->rows); }
|
|
|
|
|
public function hasColumn($table, $column) { return array_key_exists($column, $this->connection->rows[$table][0] ?? []); }
|
|
|
|
|
}
|
|
|
|
|
class DeleteTestConnection
|
|
|
|
|
{
|
|
|
|
|
public $rows;
|
|
|
|
|
public function __construct(array $rows) { $this->rows = $rows; }
|
|
|
|
|
public function getName() { return 'default'; }
|
|
|
|
|
public function getSchemaBuilder() { return new DeleteTestSchema($this); }
|
|
|
|
|
public function table($table) { return new DeleteTestQuery($this, $table); }
|
|
|
|
|
public function transaction($callback, $attempts = 1)
|
|
|
|
|
{
|
|
|
|
|
$before = $this->rows;
|
|
|
|
|
try { return $callback(); } catch (\Throwable $exception) { $this->rows = $before; throw $exception; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class DeleteTestQuery
|
|
|
|
|
{
|
|
|
|
|
private $connection;
|
|
|
|
|
private $table;
|
|
|
|
|
private $conditions = [];
|
|
|
|
|
public function __construct($connection, $table) { $this->connection = $connection; $this->table = $table; }
|
|
|
|
|
public function whereIn($column, $values) { $this->conditions[] = static fn ($row) => in_array($row[$column], $values, true); return $this; }
|
|
|
|
|
public function where($column, $value) { $this->conditions[] = static fn ($row) => $row[$column] === $value; return $this; }
|
|
|
|
|
public function lockForUpdate() { return $this; }
|
|
|
|
|
private function matches($row)
|
|
|
|
|
{
|
|
|
|
|
foreach ($this->conditions as $condition) { if (!$condition($row)) { return false; } }
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public function get()
|
|
|
|
|
{
|
|
|
|
|
$rows = [];
|
|
|
|
|
foreach ($this->connection->rows[$this->table] as $row) {
|
|
|
|
|
if ($this->matches($row)) { $rows[] = new \App\Models\User($row); }
|
|
|
|
|
}
|
|
|
|
|
return new DeleteTestCollection($rows);
|
|
|
|
|
}
|
|
|
|
|
public function pluck($column)
|
|
|
|
|
{
|
|
|
|
|
$values = [];
|
|
|
|
|
foreach ($this->connection->rows[$this->table] as $row) {
|
|
|
|
|
if ($this->matches($row)) { $values[] = $row[$column]; }
|
|
|
|
|
}
|
|
|
|
|
return new DeleteTestCollection($values);
|
|
|
|
|
}
|
|
|
|
|
public function update($values)
|
|
|
|
|
{
|
|
|
|
|
$count = 0;
|
|
|
|
|
foreach ($this->connection->rows[$this->table] as &$row) {
|
|
|
|
|
if ($this->matches($row)) { $row = array_replace($row, $values); $count++; }
|
|
|
|
|
}
|
|
|
|
|
return $count;
|
|
|
|
|
}
|
|
|
|
|
public function delete()
|
|
|
|
|
{
|
|
|
|
|
$old = $this->connection->rows[$this->table];
|
|
|
|
|
$this->connection->rows[$this->table] = array_values(array_filter($old, fn ($row) => !$this->matches($row)));
|
|
|
|
|
return count($old) - count($this->connection->rows[$this->table]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$assert = static function ($condition, $message) { if (!$condition) { throw new \RuntimeException($message); } };
|
|
|
|
|
$rows = [
|
|
|
|
|
'user' => [
|
|
|
|
|
['id' => 1, 'email' => 'one@example.com', 'is_admin' => 0, 'ref_by' => 0, 'login_session_version' => 0],
|
|
|
|
|
['id' => 2, 'email' => 'admin@example.com', 'is_admin' => 1, 'ref_by' => 0, 'login_session_version' => 0],
|
|
|
|
|
['id' => 3, 'email' => 'other@example.com', 'is_admin' => 0, 'ref_by' => 1, 'login_session_version' => 0],
|
|
|
|
|
],
|
|
|
|
|
'ticket' => [
|
|
|
|
|
['id' => 10, 'userid' => 1, 'rootid' => 0], ['id' => 11, 'userid' => 2, 'rootid' => 10],
|
|
|
|
|
['id' => 20, 'userid' => 3, 'rootid' => 0],
|
|
|
|
|
],
|
|
|
|
|
'configurations' => [['id' => 1, 'name' => 'email_blacklist', 'value' => "one@example.com\n@blocked.example"]],
|
|
|
|
|
];
|
|
|
|
|
$GLOBALS['deleteTestConnection'] = new DeleteTestConnection($rows);
|
|
|
|
|
$service = new \App\Services\UserDataDeletionService();
|
|
|
|
|
$invalid = $service->deleteUsers([0, -1, 'invalid', '1.5', [], null], false);
|
|
|
|
|
$assert($invalid['deleted'] === 0, 'Malformed IDs must not become a destructive target.');
|
|
|
|
|
$result = $service->deleteUsers([1, 1, '2', 999], false);
|
|
|
|
|
$assert($result['deleted_user_ids'] === [1] && $result['skipped_user_ids'] === [2, 999], 'Administrator or missing account was not protected.');
|
|
|
|
|
$remaining = $GLOBALS['deleteTestConnection']->rows;
|
|
|
|
|
$assert(array_column($remaining['user'], 'id') === [2, 3], 'Deletion affected unrelated accounts.');
|
|
|
|
|
$assert($remaining['user'][1]['ref_by'] === 0, 'Referral ownership was not detached from the deleted account.');
|
|
|
|
|
$assert(array_column($remaining['ticket'], 'id') === [20], 'Deleted account ticket thread cleanup failed.');
|
|
|
|
|
$assert($remaining['configurations'] === $rows['configurations'], 'Manual email blacklist was modified.');
|
|
|
|
|
$assert(\App\Services\LoginSessionRevocationService::$revoked === [1], 'The deleted account session was not revoked.');
|
|
|
|
|
|
|
|
|
|
\App\Services\Config::$logging = 'true';
|
|
|
|
|
$GLOBALS['deleteTestConnection'] = new DeleteTestConnection($rows);
|
|
|
|
|
try {
|
|
|
|
|
(new \App\Services\UserDataDeletionService())->deleteUsers([1], false);
|
|
|
|
|
throw new \RuntimeException('Log failure did not abort account deletion.');
|
|
|
|
|
} catch (\RuntimeException $exception) {
|
|
|
|
|
$assert($exception->getMessage() === 'Simulated unavailable log database', 'Unexpected deletion error.');
|
|
|
|
|
}
|
|
|
|
|
$assert($GLOBALS['deleteTestConnection']->rows === $rows, 'Default database did not roll back on log failure.');
|
|
|
|
|
echo 'Manual account deletion checks passed.' . PHP_EOL;
|
|
|
|
|
}
|