- 实测澄清协助平台时长限制: 服务端 POST /api/assistPlatform/create 硬性单次上限为 24 小时 (1 天),超过直接报错 400,支持 --days/--hours 并在超限时清晰提示 - 协助直通会话生成及落盘时实时穿透解析组织架构: 提取人员姓名、工号、手机、所属部门、所配角色清单及核心系统权限 - 建立高管身份与特权角色风控评级体系 (analyzeStaffRoleRisk): 自动识别总监、主管理员、财务负责人、总经理等高危身份并输出安全风控警示框 - 将完整人员身份画像与风控元数据注入持久化企业配置 (enterprises/<corpId>.json) - 在 SKILL.md 追加 Rule 13 及更新 assist-platform.md 说明文档
191 lines
7.2 KiB
TypeScript
191 lines
7.2 KiB
TypeScript
import {
|
|
generateAssistSession,
|
|
importAssistSessionToLocal,
|
|
} from "../../../domains/assist-platform/services.js";
|
|
import type { AssistIdentityProfile } from "../../../domains/assist-platform/types.js";
|
|
import { isDryRun, mutationPreview, requireMutationConfirmation } from "../../mutation.js";
|
|
import { booleanOption, stringOption } from "../../parser.js";
|
|
import { printJson, printKeyValue } from "../../printer.js";
|
|
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
|
import { resolveAssistClient } from "./common.js";
|
|
|
|
function formatIdentityLines(ident?: AssistIdentityProfile): {
|
|
profilePairs: Record<string, string>;
|
|
warningText: string;
|
|
} {
|
|
const profilePairs: Record<string, string> = {};
|
|
let warningText = "";
|
|
|
|
if (!ident) {
|
|
return { profilePairs, warningText };
|
|
}
|
|
|
|
profilePairs["获取员工身份"] = `${ident.staffName} (${ident.staffId || "-"})`;
|
|
if (ident.departmentName) profilePairs["所属部门"] = ident.departmentName;
|
|
if (ident.cellphone) profilePairs["绑定手机"] = ident.cellphone;
|
|
if (ident.roleNames.length > 0) profilePairs["所配角色清单"] = ident.roleNames.join("、");
|
|
if (ident.readablePermissions.length > 0) {
|
|
const permSummary = ident.readablePermissions.slice(0, 5).join("、");
|
|
profilePairs["核心系统权限"] = `${permSummary}${ident.readablePermissions.length > 5 ? ` 等共 ${ident.readablePermissions.length} 项` : ""}`;
|
|
}
|
|
|
|
if (ident.isSeniorExecutive && ident.riskWarning) {
|
|
const titles = ident.seniorTitles.length > 0 ? ident.seniorTitles.join(" / ") : "高级管理人员";
|
|
warningText =
|
|
`\n⚠️ 【高阶管理人员权限风控警示 (Executive Role Risk Warning)】\n` +
|
|
`当前协助会话获取的是企业【${titles}】身份(${ident.staffName})。\n` +
|
|
`防范越权与误用守则:\n` +
|
|
` 1. 该身份在企业内具备高级审批、财务支配与全系统管理权限;\n` +
|
|
` 2. 经理与一线实施人员严禁代用高管身份进行业务审批通过或非授权表单提交;\n` +
|
|
` 3. 请严格将此会话权限限定在必要的系统排障、配置诊断与接口联调中!\n`;
|
|
}
|
|
|
|
return { profilePairs, warningText };
|
|
}
|
|
|
|
export async function handleEnter(
|
|
parsed: CliParsedArgs,
|
|
runtime: CliRuntime
|
|
): Promise<CliResult> {
|
|
const client = resolveAssistClient(parsed, runtime);
|
|
const corpId =
|
|
parsed.args[0] ||
|
|
stringOption(parsed.options, ["corp", "corpid", "id"]) ||
|
|
runtime.selectedCorp?.id;
|
|
|
|
if (!corpId) {
|
|
return {
|
|
stdout: "",
|
|
stderr: "用法: hose-use assist-platform enter <企业ID> [--admin] [--staff <姓名>] [--save] [--switch]\n示例: hose-use assist-platform enter Cxa3oOtQi2eS$g --staff 李志财 --save --yes\n",
|
|
exitCode: 1,
|
|
};
|
|
}
|
|
|
|
const ticketId = stringOption(parsed.options, ["ticket", "ticket-id", "auth-code"]);
|
|
const staffName = stringOption(parsed.options, ["staff", "user", "name"]);
|
|
const forceAdmin = booleanOption(parsed.options, ["admin"]);
|
|
// 如果显式指定了 staffName 且未显式指定 --admin,则为用户模式;否则默认为管理员模式
|
|
const isAdmin = forceAdmin || !staffName;
|
|
|
|
const shouldSave = booleanOption(parsed.options, ["save", "import", "persist"]);
|
|
const shouldSwitch = booleanOption(parsed.options, ["switch", "current"]);
|
|
const overwrite = booleanOption(parsed.options, ["overwrite"]);
|
|
const saveName = stringOption(parsed.options, ["alias", "corp-name"]);
|
|
|
|
// 1. 若不要求保存落盘,仅生成直通免密链接输出
|
|
if (!shouldSave && !shouldSwitch) {
|
|
const sessionRes = await generateAssistSession(client, {
|
|
corpId,
|
|
ticketId,
|
|
isAdmin,
|
|
staffName,
|
|
});
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson(sessionRes, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const pairs: Record<string, string> = {
|
|
企业ID: sessionRes.corporationId,
|
|
授权模式: sessionRes.isAdmin ? "👑 企业管理员 (Admin)" : `👤 普通员工 (${sessionRes.staffName || "-"})`,
|
|
授权工单: sessionRes.ticketId,
|
|
会话Token: sessionRes.token,
|
|
免密直通链接: sessionRes.loginUrl,
|
|
};
|
|
|
|
const { profilePairs, warningText } = formatIdentityLines(sessionRes.identity);
|
|
Object.assign(pairs, profilePairs);
|
|
|
|
return {
|
|
stdout:
|
|
`====== 协助平台免密会话生成成功 ======\n${printKeyValue(pairs)}\n` +
|
|
`${warningText}\n操作提示:\n` +
|
|
` - 在浏览器中打开以上链接可直接进入协助调试工作台\n` +
|
|
` - 若要一键落盘到本地企业配置: hose-use assist-platform enter ${corpId} ${sessionRes.isAdmin ? "--admin" : `--staff "${sessionRes.staffName}"`} --save --yes\n`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
// 2. 要求持久化落盘 / 切换为工作企业
|
|
if (isDryRun(parsed, runtime)) {
|
|
return mutationPreview(
|
|
parsed,
|
|
runtime,
|
|
"从协助平台换取免密凭证并落盘为本地企业配置",
|
|
saveName || `企业 [${corpId}]`,
|
|
{
|
|
corpId,
|
|
isAdmin,
|
|
staffName: staffName || null,
|
|
ticketId: ticketId || "自动选择最新有效工单",
|
|
switchCurrent: shouldSwitch,
|
|
overwrite,
|
|
}
|
|
);
|
|
}
|
|
|
|
const nextConfirmCmd = `hose-use assist-platform enter ${corpId} ${isAdmin ? "--admin" : `--staff "${staffName}"`} --save ${shouldSwitch ? "--switch " : ""}--yes`;
|
|
requireMutationConfirmation(
|
|
parsed,
|
|
runtime,
|
|
`从协助平台换取凭证并导入企业 [${corpId}]`,
|
|
nextConfirmCmd
|
|
);
|
|
|
|
const { sessionResult, corpConfig } = await importAssistSessionToLocal(client, {
|
|
corpId,
|
|
ticketId,
|
|
isAdmin,
|
|
staffName,
|
|
saveName,
|
|
overwrite: overwrite ?? true,
|
|
});
|
|
|
|
// 若指定了 --switch,切换当前激活企业
|
|
if (shouldSwitch) {
|
|
runtime.corpContext.switchCorp(corpConfig.id, true);
|
|
}
|
|
|
|
if (runtime.jsonMode) {
|
|
return {
|
|
stdout: printJson({
|
|
success: true,
|
|
sessionResult,
|
|
enterprise: {
|
|
id: corpConfig.id,
|
|
name: corpConfig.name,
|
|
staffId: corpConfig.credentials.staffId,
|
|
hasOpenApi: Boolean(corpConfig.credentials.appKey),
|
|
switched: shouldSwitch,
|
|
},
|
|
}, runtime.outputOptions),
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|
|
|
|
const successPairs: Record<string, string> = {
|
|
企业标识: corpConfig.id,
|
|
企业名称: corpConfig.name,
|
|
执行身份: sessionResult.isAdmin ? "👑 企业管理员" : `👤 ${sessionResult.staffName || "员工"} (${corpConfig.credentials.staffId})`,
|
|
授权工单: sessionResult.ticketId,
|
|
凭证状态: corpConfig.credentials.appKey ? `✅ 已提取 OpenAPI 持久化密钥 (${corpConfig.credentials.appKey})` : "⚪ 临时协助会话",
|
|
配置文件: `enterprises/${corpConfig.id}.json`,
|
|
激活状态: shouldSwitch ? "🎯 已设为当前激活工作企业" : "可随时运行 hose-use corps switch 进行切换",
|
|
};
|
|
|
|
const { profilePairs, warningText } = formatIdentityLines(sessionResult.identity);
|
|
Object.assign(successPairs, profilePairs);
|
|
|
|
return {
|
|
stdout: `[SUCCESS] 协助企业凭证获取并落盘成功!\n${printKeyValue(successPairs)}\n${warningText}`,
|
|
stderr: "",
|
|
exitCode: 0,
|
|
};
|
|
}
|