From 8b070cf956f6968d0916e68e1569636939a9fe53 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Wed, 29 Jul 2026 16:38:57 +0800 Subject: [PATCH] Add user management and password reset support and bump version to 0.3.7 --- package.json | 2 +- src/cli.ts | 35 ++++++++++++++++++ src/domains/seal/api.ts | 65 +++++++++++++++++++++++++++++++++- src/domains/seal/tools.test.ts | 12 +++++++ src/domains/seal/tools.ts | 41 +++++++++++++++++++++ src/domains/seal/types.ts | 26 ++++++++++++++ 6 files changed, 179 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 49d24b1..9a30c9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "seal-home", - "version": "0.3.6", + "version": "0.3.7", "type": "module", "module": "src/server.ts", "bin": { diff --git a/src/cli.ts b/src/cli.ts index 713e763..6189476 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -160,6 +160,37 @@ async function main() { return; } + if (area === "users" && action === "create") { + const name = stringOption(args.options.name) ?? maybeName; + const phone = stringOption(args.options.phone); + if (!name || !phone) throw new Error("Usage: seal-home users create --name --phone [--dept ]"); + const deptId = stringOption(args.options.dept) ?? stringOption(args.options.departmentId); + const departmentIds = deptId ? [deptId] : undefined; + await runTool("seal_user_create", client, corp, { name, phone, departmentIds }, outputOptions(args.options)); + return; + } + + if (area === "users" && action === "reset-password") { + const userId = maybeName ?? stringOption(args.options.userId); + if (!userId) throw new Error("Usage: seal-home users reset-password "); + await runTool("seal_user_reset_password", client, corp, { userId }, outputOptions(args.options)); + return; + } + + if (area === "users" && action === "get") { + const userId = maybeName ?? stringOption(args.options.userId); + if (!userId) throw new Error("Usage: seal-home users get "); + await runTool("seal_user_get", client, corp, { userId }, outputOptions(args.options)); + return; + } + + if (area === "users" && action === "delete") { + const userId = maybeName ?? stringOption(args.options.userId); + if (!userId) throw new Error("Usage: seal-home users delete "); + await runTool("seal_user_delete", client, corp, { userId }, outputOptions(args.options)); + return; + } + if (area === "context") { await runTool("seal_approval_context_get", client, corp, { documentLimit: numberOption(args.options.documentLimit), @@ -1259,6 +1290,10 @@ Usage: seal-home delegation list [--corp ] seal-home delegation create [--note ] [--corp ] seal-home delegation revoke [--corp ] + seal-home users create --name --phone [--dept ] [--corp ] + seal-home users reset-password [--corp ] + seal-home users get [--corp ] + seal-home users delete [--corp ] seal-home source config [--corp ] seal-home tool [--corp ] [--json '{"key":"value"}'] seal-home rules count [--corp ] diff --git a/src/domains/seal/api.ts b/src/domains/seal/api.ts index f973183..c129e96 100644 --- a/src/domains/seal/api.ts +++ b/src/domains/seal/api.ts @@ -24,7 +24,13 @@ import { SealDelegationItem, SealDelegationItemSchema, SealDelegationCreateResult, - SealDelegationCreateResultSchema + SealDelegationCreateResultSchema, + SealUserCreateResult, + SealUserCreateResultSchema, + SealUserResetPasswordResult, + SealUserResetPasswordResultSchema, + SealUserDetail, + SealUserDetailSchema } from "./types.js"; async function unwrap(response: Response, schema: z.ZodType): Promise { @@ -333,3 +339,60 @@ export async function revokeDelegation(client: KyInstance, tokenId: string): Pro return json.data?.success ?? response.ok; } +export async function createLocalUser( + client: KyInstance, + params: { name: string; phone: string; permissions?: string[]; departmentIds?: string[] } +): Promise { + let departmentIds = params.departmentIds && params.departmentIds.length > 0 ? params.departmentIds : undefined; + if (!departmentIds) { + const rootDepts = (await client.get("api/v1/contacts/departments/root/children").json()) as { + data?: { items?: Array<{ _id: string }> }; + }; + const firstId = rootDepts.data?.items?.[0]?._id; + if (firstId) { + departmentIds = [firstId]; + } + } + return unwrap( + await client.post("api/v1/contacts/local/users", { + json: { + name: params.name, + phone: params.phone, + permissions: params.permissions ?? [], + departmentIds: departmentIds ?? [] + } + }), + SealUserCreateResultSchema + ); +} + +export async function resetUserPassword( + client: KyInstance, + userId: string +): Promise { + return unwrap( + await client.post(`api/v1/contacts/local/users/${userId}/reset-password`, { json: {} }), + SealUserResetPasswordResultSchema + ); +} + +export async function getUserDetail( + client: KyInstance, + userId: string +): Promise { + return unwrap( + await client.get(`api/v1/contacts/users/${userId}`), + SealUserDetailSchema + ); +} + +export async function deleteLocalUser( + client: KyInstance, + userId: string +): Promise { + const response = await client.delete(`api/v1/contacts/local/users/${userId}`); + const json = (await response.json()) as { data?: { success?: boolean } }; + return json.data?.success ?? response.ok; +} + + diff --git a/src/domains/seal/tools.test.ts b/src/domains/seal/tools.test.ts index 5e5eebb..67e4d56 100644 --- a/src/domains/seal/tools.test.ts +++ b/src/domains/seal/tools.test.ts @@ -66,6 +66,18 @@ describe("sealTools", () => { expect(revokeTool).toBeDefined(); }); + test("includes user management tools", () => { + const createTool = sealTools.find((item) => item.name === "seal_user_create"); + const resetTool = sealTools.find((item) => item.name === "seal_user_reset_password"); + const getTool = sealTools.find((item) => item.name === "seal_user_get"); + const deleteTool = sealTools.find((item) => item.name === "seal_user_delete"); + + expect(createTool).toBeDefined(); + expect(resetTool).toBeDefined(); + expect(getTool).toBeDefined(); + expect(deleteTool).toBeDefined(); + }); + test("includes the approval search tool", () => { const tool = sealTools.find((item) => item.name === "seal_approval_search"); expect(tool).toBeDefined(); diff --git a/src/domains/seal/tools.ts b/src/domains/seal/tools.ts index a99a8fc..44277d1 100644 --- a/src/domains/seal/tools.ts +++ b/src/domains/seal/tools.ts @@ -54,6 +54,47 @@ export const sealTools = [ handler: async (client: KyInstance, params: { tokenId: string }) => api.revokeDelegation(client, params.tokenId) }, + { + name: "seal_user_create", + description: "创建本地 Seal 用户,生成并返回初始随机密码", + parameters: z.object({ + name: z.string().describe("用户姓名"), + phone: z.string().describe("手机号码 (例如 13300001111 或 +8613300001111)"), + permissions: z.array(z.string()).optional().describe("权限列表,默认为 []"), + departmentIds: z.array(z.string()).optional().describe("关联部门 ID 数组") + }), + handler: async ( + client: KyInstance, + params: { name: string; phone: string; permissions?: string[]; departmentIds?: string[] } + ) => api.createLocalUser(client, params) + }, + { + name: "seal_user_reset_password", + description: "重置指定 Seal 用户的默认/随机密码,并返回生成的新密码", + parameters: z.object({ + userId: z.string().describe("用户 ID (_id)") + }), + handler: async (client: KyInstance, params: { userId: string }) => + api.resetUserPassword(client, params.userId) + }, + { + name: "seal_user_get", + description: "获取指定 Seal 用户的详细信息", + parameters: z.object({ + userId: z.string().describe("用户 ID (_id)") + }), + handler: async (client: KyInstance, params: { userId: string }) => + api.getUserDetail(client, params.userId) + }, + { + name: "seal_user_delete", + description: "删除指定 Seal 本地用户", + parameters: z.object({ + userId: z.string().describe("用户 ID (_id)") + }), + handler: async (client: KyInstance, params: { userId: string }) => + api.deleteLocalUser(client, params.userId) + }, { name: "seal_approval_rules_list", description: "列出 Seal 当前草稿审批规则;支持 countOnly、fields、limit 以避免返回规则正文", diff --git a/src/domains/seal/types.ts b/src/domains/seal/types.ts index 3d2d257..d7eeb32 100644 --- a/src/domains/seal/types.ts +++ b/src/domains/seal/types.ts @@ -234,6 +234,28 @@ export const SealDelegationCreateResultSchema = z.object({ link: z.string() }).passthrough(); +export const SealUserCreateResultSchema = z.object({ + userId: z.string(), + password: z.string() +}).passthrough(); + +export const SealUserResetPasswordResultSchema = z.object({ + password: z.string() +}).passthrough(); + +export const SealUserDetailSchema = z.object({ + _id: z.string(), + tenantId: z.string().optional(), + name: z.string(), + mobile: z.string().optional(), + email: z.string().optional(), + departmentIds: z.array(z.string()).default([]), + status: z.string().optional(), + permissions: z.array(z.string()).default([]), + createdAt: z.string().optional(), + updatedAt: z.string().optional() +}).passthrough(); + export type MeData = z.infer; export type SealSessionData = z.infer; export type ApprovalRule = z.infer; @@ -248,4 +270,8 @@ export type ApprovalRun = z.infer; export type ApprovalRunListData = z.infer; export type SealDelegationItem = z.infer; export type SealDelegationCreateResult = z.infer; +export type SealUserCreateResult = z.infer; +export type SealUserResetPasswordResult = z.infer; +export type SealUserDetail = z.infer; +