diff --git a/src/commands/user/auth-groups.ts b/src/commands/user/auth-groups.ts index a95f371..2e4b089 100644 --- a/src/commands/user/auth-groups.ts +++ b/src/commands/user/auth-groups.ts @@ -1,16 +1,89 @@ /** - * commands/user/auth-groups.ts — 占位, Task 16 实现 + * commands/user/auth-groups.ts — admin 查用户权限分组 + * + * API: GET /api/permission/Users/getAllPermission?userId={uid} + * + * 返回: 权限分组树 (角色/岗位/组织), 每项带 hasChildren 标识 + * web 端触发: Authorize.vue init() → getAuthGroup({userId}), 用于左侧树展示 + * + * 下一步: 选 objectId 配合 auth-list 查具体权限 */ import { Command } from 'commander' -import { log } from '../../core/log.ts' +import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' +import { log, printJson } from '../../core/log.ts' + +interface AuthGroupItem { + id?: string + type?: string + fullName?: string + hasChildren?: boolean + pcCurStand?: boolean + appCurStand?: boolean + children?: AuthGroupItem[] +} + +interface AuthGroupsOpts { + userId: string + json?: boolean +} + +function flattenTree(items: AuthGroupItem[], depth = 0): Array { + const out: Array = [] + for (const it of items) { + out.push({ ...it, _depth: depth }) + if (it.children?.length) { + out.push(...flattenTree(it.children, depth + 1)) + } + } + return out +} export function buildUserAuthGroupsCommand(): Command { - const cmd = new Command('auth-groups').description('TODO: implement in Task 16') - cmd.action(async () => { - log.warn('TODO: implement in Task 16') - process.exit(1) + const cmd = new Command('auth-groups') + .description('查用户权限分组 (admin 操作, 返回角色/岗位/组织树)') + .requiredOption('--userId ', '用户 id') + .option('--json', '原始 JSON 输出', false) + .action(async (opts: AuthGroupsOpts) => { + const c = await JnpfClient.fromToken() + const r = await c.get('/permission/Users/getAllPermission', { + userId: opts.userId, + }) + if (r.code !== 200) { + log.error(`user auth-groups 失败 (code=${r.code}): ${r.msg}`) + process.exit(1) + } + const tree = r.data || [] + if (opts.json) { + printJson(tree) + return + } + const flat = flattenTree(tree) + log.info(`用户 id=${opts.userId} 共 ${flat.length} 个权限分组节点`) + console.log('OBJECT_ID TYPE FULLNAME HAS_CHILD') + console.log('-'.repeat(95)) + for (const it of flat) { + const id = String(it.id || '').padEnd(33) + const type = String(it.type || '').padEnd(12) + const indent = ' '.repeat(it._depth) + const name = (indent + (it.fullName || '')).padEnd(31).slice(0, 31) + const child = it.hasChildren ? '✓' : '' + console.log(`${id}${type}${name}${child}`) + } + }) + installAiHelpHook(cmd, { + purpose: '查用户权限分组 (admin 操作, 返回角色/岗位/组织树)', + required: { '--userId': '用户 id' }, + optional: { '--json': '原始 JSON 输出 (含 children 嵌套)' }, + example: 'dacli user auth-groups --userId=1234567890', + source: { + web: 'web/apps/jnpf-web-apps-main/src/views/_core/profile/components/Authorize.vue', + api: 'GET /api/permission/Users/getAllPermission', + }, + notes: [ + '**objectId + objectType**: 从输出选一个 objectId, 配合 auth-list 查具体权限', + '**objectType 候选**: role / position / organize (与后端 type 一致)', + ], }) - installAiHelpHook(cmd, { purpose: 'TODO' }) return cmd } \ No newline at end of file