75 lines
6.1 KiB
PHP
75 lines
6.1 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
// Narrow, read-only preflight. Configuration remains in process memory.
|
||
date_default_timezone_set('Asia/Singapore');
|
||
$root = dirname(__DIR__, 2);
|
||
$readEnv = static function (string $file): array {
|
||
$out = []; $section = '';
|
||
foreach (file($file, FILE_IGNORE_NEW_LINES) ?: [] as $line) {
|
||
$line = trim($line);
|
||
if ($line === '' || $line[0] === '#' || $line[0] === ';') continue;
|
||
if (preg_match('/^\[([^\]]+)\]$/', $line, $match)) { $section = strtolower($match[1]); continue; }
|
||
if (!str_contains($line, '=')) continue;
|
||
[$key, $value] = explode('=', $line, 2);
|
||
$key = strtolower(trim($key)); $value = trim($value);
|
||
if (strlen($value) >= 2 && (($value[0] === '"' && str_ends_with($value, '"')) || ($value[0] === "'" && str_ends_with($value, "'")))) $value = substr($value, 1, -1);
|
||
$out[$key] = $value;
|
||
if ($section !== '') $out[$section . '.' . $key] = $value;
|
||
}
|
||
return $out;
|
||
};
|
||
$all = static function (PDO $db, string $sql, array $args = []): array {
|
||
$q = $db->prepare($sql); $q->execute($args); return $q->fetchAll(PDO::FETCH_ASSOC);
|
||
};
|
||
try {
|
||
$stage = 'original database connection';
|
||
$env = $readEnv($root . '/server/.env');
|
||
$get = static function (string $key, string $default = '') use ($env): string {
|
||
return $env['database.' . $key] ?? $env['database_' . $key] ?? $default;
|
||
};
|
||
$prefix = $get('prefix', 'zyt_');
|
||
if (!preg_match('/^[A-Za-z0-9_]+$/', $prefix)) throw new RuntimeException('Invalid prefix');
|
||
$dsn = 'mysql:host=' . $get('hostname') . ';port=' . $get('hostport', '3306') . ';dbname=' . $get('database') . ';charset=' . $get('charset', 'utf8mb4');
|
||
$db = new PDO($dsn, $get('username'), $get('password'), [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false]);
|
||
unset($env, $get, $dsn);
|
||
$db->exec('SET TRANSACTION READ ONLY');
|
||
$db->beginTransaction();
|
||
$stage = 'target original records';
|
||
$observed = date(DATE_ATOM);
|
||
$accounts = $all($db, "SELECT id,root,disable,is_paw FROM {$prefix}admin WHERE account=? ORDER BY id", ['group_hosp_test_1030']);
|
||
$activeAccounts = $all($db, "SELECT id FROM {$prefix}admin WHERE account=? AND (delete_time IS NULL OR delete_time=0) ORDER BY id", ['group_hosp_test_1030']);
|
||
foreach ($accounts as &$account) {
|
||
$account['role_ids'] = array_map('intval', array_column($all($db, "SELECT role_id FROM {$prefix}admin_role WHERE admin_id=? ORDER BY role_id", [$account['id']]), 'role_id'));
|
||
$account['dept_ids'] = array_map('intval', array_column($all($db, "SELECT dept_id FROM {$prefix}admin_dept WHERE admin_id=? ORDER BY dept_id", [$account['id']]), 'dept_id'));
|
||
} unset($account);
|
||
$roles = $all($db, "SELECT id,data_scope FROM {$prefix}system_role WHERE name=? ORDER BY id", ['集团登录只读验收']);
|
||
$activeRoles = $all($db, "SELECT id FROM {$prefix}system_role WHERE name=? AND (delete_time IS NULL OR delete_time=0) ORDER BY id", ['集团登录只读验收']);
|
||
foreach ($roles as &$role) $role['menu_ids'] = array_map('intval', array_column($all($db, "SELECT menu_id FROM {$prefix}system_role_menu WHERE role_id=? ORDER BY menu_id", [$role['id']]), 'menu_id')); unset($role);
|
||
$dept = $all($db, "SELECT id,pid,status FROM {$prefix}dept WHERE id=52 AND (delete_time IS NULL OR delete_time=0)");
|
||
$db->rollBack();
|
||
$groupEnvFile = $root . '/identity-admin/server/.env';
|
||
$groupEnv = is_file($groupEnvFile) ? $readEnv($groupEnvFile) : [];
|
||
$groupPath = getenv('IAM_GROUP_DB_PATH') ?: ($groupEnv['iam_group_db_path'] ?? 'runtime/group.sqlite');
|
||
if (!preg_match('~^(?:[A-Za-z]:[\\\\/]|/)~', $groupPath)) $groupPath = $root . '/identity-admin/server/' . $groupPath;
|
||
if (!is_file($groupPath)) throw new RuntimeException('Group file missing');
|
||
$stage = 'group database connection';
|
||
$group = new PDO('sqlite:' . $groupPath, null, null, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
|
||
$group->exec('PRAGMA query_only=ON');
|
||
$group->beginTransaction();
|
||
$stage = 'target group records';
|
||
$groupObserved = date(DATE_ATOM);
|
||
$company = $all($group, 'SELECT id,code,name,parent_id,disable FROM company WHERE id=1');
|
||
$members = [];
|
||
foreach ($accounts as $account) $members[] = ['source_id' => (int) $account['id'], 'exists' => count($all($group, 'SELECT 1 FROM membership WHERE company_id=1 AND source_id=?', [$account['id']])) > 0];
|
||
$group->rollBack();
|
||
$result = ['original_db_observed_at' => $observed, 'group_db_observed_at' => $groupObserved, 'account' => $accounts, 'active_account_ids' => array_map('intval', array_column($activeAccounts, 'id')), 'role' => $roles, 'active_role_ids' => array_map('intval', array_column($activeRoles, 'id')), 'dept_52' => $dept, 'company_1' => $company, 'target_membership_exists' => $members];
|
||
$md = "# 测试账号创建前只读核对\n\n原库采集时刻:{$observed}(Asia/Singapore)。集团元数据采集时刻:{$groupObserved}。\n\n只按目标账号 `group_hosp_test_1030`、目标角色“集团登录只读验收”、部门 52 和公司 1 查询;原库使用 READ ONLY 事务并回滚,集团 SQLite 使用 query_only 事务并回滚。未写数据库,未操作浏览器,未读取认证敏感列。查询可能与浏览器保存操作并行,结果仅代表上述采集时刻。\n\n```json\n" . json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n```\n\n`account`/`role` 包括精确名称命中的历史行,`active_*_ids` 仅表示未软删记录。成员存在性仅检查目标账号对应 source_id;账号不存在时成员结果为空数组。该报告不自动授权任何动作。\n";
|
||
file_put_contents(__DIR__ . '/test-account-preflight.md', $md);
|
||
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), PHP_EOL;
|
||
} catch (Throwable $error) {
|
||
if (isset($db) && $db->inTransaction()) $db->rollBack();
|
||
if (isset($group) && $group->inTransaction()) $group->rollBack();
|
||
fwrite(STDERR, 'Preflight failed at ' . ($stage ?? 'initialization') . '; diagnostic details suppressed.' . PHP_EOL);
|
||
exit(1);
|
||
}
|