feat: 新增租户 active 状态与统一禁用闸门
This commit is contained in:
1 parent
634f0e2b78
commit
0ff3d9a313
21 files changed
+855
-14
No files matched your search
@@ -93,6 +93,9 @@ let lastCorpLoadIssues: CorpConfigLoadIssue[] = [];
|
||||
// Files already warned about this process: loadCorpConfigs runs several times
|
||||
// per invocation (selection, dispatch, tool handlers), so warn each file once.
|
||||
const warnedCorpFiles = new Set<string>();
|
||||
// A malformed `active` value is normalized to disabled instead of dropping the
|
||||
// whole file. Track those separately so the warning names the right remedy.
|
||||
const warnedActiveCorpFiles = new Set<string>();
|
||||
|
||||
/**
|
||||
* Issues from the most recent loadCorpConfigs() call. A malformed file must not
|
||||
@@ -112,6 +115,7 @@ function loadCorpConfigEntries(): Array<{ config: CorpConfig; path: string }> {
|
||||
const dirs = resolveEnterprisesDirs();
|
||||
const byId = new Map<string, { config: CorpConfig; path: string }>();
|
||||
const issues: CorpConfigLoadIssue[] = [];
|
||||
const activeWarnings: Array<{ dir: string; file: string; corpId: string }> = [];
|
||||
|
||||
for (const dir of dirs) {
|
||||
for (const file of readdirSync(dir)
|
||||
@@ -119,13 +123,26 @@ function loadCorpConfigEntries(): Array<{ config: CorpConfig; path: string }> {
|
||||
.filter((item) => !item.startsWith("example."))) {
|
||||
const fullPath = join(dir, file);
|
||||
let corp: CorpConfig;
|
||||
let activeMalformed = false;
|
||||
try {
|
||||
const content = readFileSync(fullPath, "utf-8");
|
||||
corp = CorpConfigSchema.parse(JSON.parse(content));
|
||||
const raw = JSON.parse(content) as Record<string, unknown>;
|
||||
const parsed = CorpConfigSchema.parse(raw);
|
||||
// `CorpConfigSchema.active` is fail-closed: a non-boolean value parses
|
||||
// to `false` instead of rejecting the file. Detect it here so the user
|
||||
// learns why the tenant became unusable and how to fix it.
|
||||
activeMalformed = "active" in raw && typeof raw.active !== "boolean";
|
||||
// Materialize the tenant availability default so every config read from
|
||||
// disk carries an explicit boolean. Legacy files without `active` stay
|
||||
// enabled; `false` (explicit or normalized) gates command execution.
|
||||
corp = { ...parsed, active: parsed.active ?? true };
|
||||
} catch (error) {
|
||||
issues.push({ dir, file, reason: error instanceof Error ? error.message : String(error) });
|
||||
continue;
|
||||
}
|
||||
if (activeMalformed) {
|
||||
activeWarnings.push({ dir, file, corpId: corp.id });
|
||||
}
|
||||
byId.set(corp.id, { config: corp, path: fullPath });
|
||||
}
|
||||
}
|
||||
@@ -144,6 +161,19 @@ function loadCorpConfigEntries(): Array<{ config: CorpConfig; path: string }> {
|
||||
console.error(`👉 Next action: seal-use doctor (检查配置文件格式与权限)`);
|
||||
}
|
||||
|
||||
const freshActive = activeWarnings.filter((item) => {
|
||||
const key = `${item.dir}/${item.file}`;
|
||||
if (warnedActiveCorpFiles.has(key)) return false;
|
||||
warnedActiveCorpFiles.add(key);
|
||||
return true;
|
||||
});
|
||||
for (const item of freshActive) {
|
||||
console.error(
|
||||
`⚠️ 企业配置 ${item.dir}/${item.file} 的 active 字段值非法(只能是 true/false),企业 ${item.corpId} 已按禁用处理;仍可检索,但指向它的命令会被阻断。`
|
||||
);
|
||||
console.error(`👉 Next action: seal-use corps enable ${item.corpId} --confirm (或把配置修正为 "active": true)`);
|
||||
}
|
||||
|
||||
return Array.from(byId.values());
|
||||
}
|
||||
|
||||
@@ -268,6 +298,9 @@ export function updateCorpConfig(corpId: string, patch: Partial<CorpConfig>): Co
|
||||
}
|
||||
}
|
||||
const validated = CorpConfigSchema.parse(merged);
|
||||
writeSecureFile(filePath, `${JSON.stringify(validated, null, 2)}\n`);
|
||||
return validated;
|
||||
// Keep `active` explicit on disk after any config update (including ones that
|
||||
// predate the field), matching the materialized value from loadCorpConfigs().
|
||||
const normalized: CorpConfig = { ...validated, active: validated.active ?? true };
|
||||
writeSecureFile(filePath, `${JSON.stringify(normalized, null, 2)}\n`);
|
||||
return normalized;
|
||||
}
|
||||
Reference in new issue
Block a user