@autional/shared 0.1.0-rc.57 → 0.1.0-rc.58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autional/shared",
3
- "version": "0.1.0-rc.57",
3
+ "version": "0.1.0-rc.58",
4
4
  "description": "Autional shared layer — API client, auth, types, hooks, utils",
5
5
  "license": "AGPL-3.0",
6
6
  "main": "./src/index.ts",
@@ -33,6 +33,7 @@ export const ROLE_SECURITY_FULL = ['super_admin', 'security_admin'] as const;
33
33
  * Portal Guard 配置。
34
34
  * 如需新增 Guard,在此添加 role 集即可。
35
35
  */
36
+ /** @deprecated 与命名 Guard 同属死面(17 仓 0 消费);角色集请就近声明或走 RBAC。下个 minor 删除(ADR §9-3)。 */
36
37
  export const ADMIN_GUARDS = {
37
38
  admin: { roles: ROLE_SUPER_ADMIN },
38
39
  userMgmt: { roles: ROLE_USER_MGMT },
@@ -41,10 +42,12 @@ export const ADMIN_GUARDS = {
41
42
  securityAdmin: { roles: ROLE_SECURITY_FULL }, // admin-console 安全配置管理
42
43
  } as const;
43
44
 
45
+ /** @deprecated 与命名 Guard 同属死面(17 仓 0 消费);角色集请就近声明或走 RBAC。下个 minor 删除(ADR §9-3)。 */
44
46
  export const PLATFORM_GUARDS = {
45
47
  superAdmin: { roles: ROLE_SUPER_ADMIN },
46
48
  } as const;
47
49
 
50
+ /** @deprecated 与命名 Guard 同属死面(17 仓 0 消费);角色集请就近声明或走 RBAC。下个 minor 删除(ADR §9-3)。 */
48
51
  export const SECURITY_GUARDS = {
49
52
  security: { roles: ROLE_SECURITY_ENTRY }, // 基础准入(不含 admin)
50
53
  auditRead: { roles: ROLE_AUDIT_READ }, // 审计证据页(auditor 只读)
@@ -55,6 +58,7 @@ export const SECURITY_GUARDS = {
55
58
  // ============ Portal Config ============
56
59
 
57
60
  /** 所有 Portal 的认证配置映射 */
61
+ /** @deprecated 与命名 Guard 同属死面(17 仓 0 消费);角色集请就近声明或走 RBAC。下个 minor 删除(ADR §9-3)。 */
58
62
  export const PORTAL_AUTH_CONFIG = {
59
63
  'admin-console': { guards: ADMIN_GUARDS },
60
64
  'platform-console': { guards: PLATFORM_GUARDS },
@@ -12,6 +12,9 @@
12
12
  * <AdminGuard allowedRoles={['user_mgmt']}><UsersPage /></AdminGuard> // 覆盖默认角色
13
13
  *
14
14
  * @see portal-config.ts
15
+ *
16
+ * @deprecated 整个模块已无消费方(17 仓实测 0 处)。角色准入请直接用 `<RequireAuth allowedRoles>`。
17
+ * 下个 minor 删除;见 docs/CONSOLE-KIT-DECISION.md §9-3。
15
18
  */
16
19
 
17
20
  'use client';
@@ -32,6 +35,7 @@ interface PortalGuardProps {
32
35
  /**
33
36
  * Admin Console Guard — 默认 roles: ['super_admin', 'admin']
34
37
  */
38
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
35
39
  export function AdminGuard({ children, allowedRoles, ...rest }: PortalGuardProps) {
36
40
  return (
37
41
  <RequireAuth allowedRoles={allowedRoles || ADMIN_GUARDS.admin.roles} {...rest}>
@@ -43,6 +47,7 @@ export function AdminGuard({ children, allowedRoles, ...rest }: PortalGuardProps
43
47
  /**
44
48
  * 用户管理 Guard — roles: ['super_admin', 'admin', 'user_manager']
45
49
  */
50
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
46
51
  export function UserMgmtGuard({ children, allowedRoles, ...rest }: PortalGuardProps) {
47
52
  return (
48
53
  <RequireAuth allowedRoles={allowedRoles || ADMIN_GUARDS.userMgmt.roles} {...rest}>
@@ -54,6 +59,7 @@ export function UserMgmtGuard({ children, allowedRoles, ...rest }: PortalGuardPr
54
59
  /**
55
60
  * 安全只读 Guard — roles: ['super_admin', 'admin', 'security_admin']
56
61
  */
62
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
57
63
  export function SecurityGuard({ children, allowedRoles, ...rest }: PortalGuardProps) {
58
64
  return (
59
65
  <RequireAuth allowedRoles={allowedRoles || SECURITY_GUARDS.security.roles} {...rest}>
@@ -65,6 +71,7 @@ export function SecurityGuard({ children, allowedRoles, ...rest }: PortalGuardPr
65
71
  /**
66
72
  * Platform Console Guard — roles: ['super_admin', 'admin']
67
73
  */
74
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
68
75
  export function PlatformGuard({ children, allowedRoles, ...rest }: PortalGuardProps) {
69
76
  return (
70
77
  <RequireAuth allowedRoles={allowedRoles || PLATFORM_GUARDS.superAdmin.roles} {...rest}>
@@ -77,6 +84,7 @@ export function PlatformGuard({ children, allowedRoles, ...rest }: PortalGuardPr
77
84
  * Security Admin Guard — roles: ['super_admin', 'security_admin']
78
85
  * 用于 security-dashboard 中仅 security_admin 可访问的平台级页面
79
86
  */
87
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
80
88
  export function SecurityAdminGuard({ children, allowedRoles, ...rest }: PortalGuardProps) {
81
89
  return (
82
90
  <RequireAuth allowedRoles={allowedRoles || SECURITY_GUARDS.securityAdmin.roles} {...rest}>
@@ -89,6 +97,7 @@ export function SecurityAdminGuard({ children, allowedRoles, ...rest }: PortalGu
89
97
  * Auditor Guard — roles: ['auditor']
90
98
  * 仅 auditor 可访问的纯审计页面(只读)
91
99
  */
100
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
92
101
  export function AuditorGuard({ children, allowedRoles, ...rest }: PortalGuardProps) {
93
102
  return (
94
103
  <RequireAuth allowedRoles={allowedRoles || SECURITY_GUARDS.auditor.roles} {...rest}>
@@ -100,6 +109,7 @@ export function AuditorGuard({ children, allowedRoles, ...rest }: PortalGuardPro
100
109
  /**
101
110
  * Token-only Guard — 仅需登录,无角色限制
102
111
  */
112
+ /** @deprecated 全站 0 消费(17 仓实测);用 `<RequireAuth allowedRoles={...}>` 代替,下个 minor 删除(ADR §9-3)。 */
103
113
  export function AuthGuard({ children, ...rest }: PortalGuardProps) {
104
114
  return <RequireAuth {...rest}>{children}</RequireAuth>;
105
115
  }
@@ -275,6 +275,80 @@ describe('usePortalCatalog', () => {
275
275
  expect(result.current.portals[0]!.code).toBe('user');
276
276
  });
277
277
 
278
+ // 数据驱动可见性:服务端目录带 config.portal.audience 时**优先于**注册表
279
+ // (ADR §9-1:后端把受众写进目录后,加/调门户不再需要前端发版)。
280
+ it('目录自带 audience=admin → member 看不见(即使注册表里该 code 是 public)', async () => {
281
+ getMock.mockResolvedValue({
282
+ data: {
283
+ items: [
284
+ { code: 'user', name: '用户门户', order: 1, config: { portal: { audience: 'admin' } } },
285
+ ],
286
+ },
287
+ });
288
+ const { result } = renderHook(() => usePortalCatalog({ tenantId: 't1', role: 'member' }), {
289
+ wrapper: makeWrapper(),
290
+ });
291
+ await waitFor(() => expect(result.current.isLoading).toBe(false));
292
+ expect(result.current.portals).toEqual([]);
293
+ });
294
+
295
+ it('目录自带 audience=public → member 也看得见(覆盖注册表的 admin 判定)', async () => {
296
+ getMock.mockResolvedValue({
297
+ data: {
298
+ items: [
299
+ { code: 'config', name: '配置中心', order: 1, config: { portal: { audience: 'public' } } },
300
+ ],
301
+ },
302
+ });
303
+ const { result } = renderHook(() => usePortalCatalog({ tenantId: 't1', role: 'member' }), {
304
+ wrapper: makeWrapper(),
305
+ });
306
+ await waitFor(() => expect(result.current.portals).toHaveLength(1));
307
+ expect(result.current.portals[0]!.code).toBe('config');
308
+ });
309
+
310
+ it('目录自带 audience=platform → 非平台租户成员看不见', async () => {
311
+ getMock.mockResolvedValue({
312
+ data: {
313
+ items: [
314
+ { code: 'platform', name: '平台控制台', order: 1, config: { portal: { audience: 'platform' } } },
315
+ ],
316
+ },
317
+ });
318
+ const { result } = renderHook(() => usePortalCatalog({ tenantId: 't1', role: 'super_admin' }), {
319
+ wrapper: makeWrapper(),
320
+ });
321
+ await waitFor(() => expect(result.current.isLoading).toBe(false));
322
+ expect(result.current.portals).toEqual([]);
323
+ });
324
+
325
+ it('目录 audience 是脏值(未知字符串)→ 回落注册表判定', async () => {
326
+ getMock.mockResolvedValue({
327
+ data: {
328
+ items: [
329
+ { code: 'config', name: '配置中心', order: 1, config: { portal: { audience: 'root' } } },
330
+ ],
331
+ },
332
+ });
333
+ const { result } = renderHook(() => usePortalCatalog({ tenantId: 't1', role: 'member' }), {
334
+ wrapper: makeWrapper(),
335
+ });
336
+ await waitFor(() => expect(result.current.isLoading).toBe(false));
337
+ // 注册表里 config 属管理面 ⇒ member 不可见
338
+ expect(result.current.portals).toEqual([]);
339
+ });
340
+
341
+ it('目录无 config → 完全按注册表(向后兼容旧后端)', async () => {
342
+ getMock.mockResolvedValue({
343
+ data: { items: [{ code: 'config', name: '配置中心', order: 1 }] },
344
+ });
345
+ const { result } = renderHook(() => usePortalCatalog({ tenantId: 't1', role: 'member' }), {
346
+ wrapper: makeWrapper(),
347
+ });
348
+ await waitFor(() => expect(result.current.isLoading).toBe(false));
349
+ expect(result.current.portals).toEqual([]);
350
+ });
351
+
278
352
  it('role 缺省回退 store 当前角色(useCurrentRole),平台租户成员判定不依赖当前租户', async () => {
279
353
  useAuthStore.setState({
280
354
  tenants: [
@@ -8,6 +8,7 @@ import { getPortalUrl } from '../config';
8
8
  import {
9
9
  ADMIN_PLANE_ROLES as ADMIN_PLANE_ROLES_REGISTRY,
10
10
  getSwitcherExcludedCodes,
11
+ isPortalVisibleByAudience,
11
12
  isPortalVisibleForAudience,
12
13
  } from '../portal/registry';
13
14
  import { useCurrentRole } from './useCurrentRole';
@@ -89,12 +90,25 @@ interface RawPortalApplication {
89
90
  name: string;
90
91
  description?: string;
91
92
  order?: number;
92
- /** allowedRoles 为 apiClient camelCase 归一后的键(snake 形态兼容后端透出前的原始键) */
93
+ /**
94
+ * 服务端目录里的门户元数据(tenant-service `ApplicationResponse.config.portal`,2026-11 起透出)。
95
+ * `audience` 存在时**优先于**前端注册表;缺失回落注册表(新旧后端都能跑)。
96
+ * allowedRoles 为 apiClient camelCase 归一后的键(snake 形态兼容原始键)。
97
+ */
93
98
  config?: {
94
- portal?: { allowedRoles?: readonly string[]; allowed_roles?: readonly string[] };
99
+ portal?: {
100
+ audience?: string;
101
+ allowedRoles?: readonly string[];
102
+ allowed_roles?: readonly string[];
103
+ };
95
104
  } | null;
96
105
  }
97
106
 
107
+ /** 只认白名单内的受众值;脏值/缺失 ⇒ undefined(回落注册表)。 */
108
+ function toKnownAudience(raw: string | undefined | null): 'public' | 'admin' | 'platform' | undefined {
109
+ return raw === 'public' || raw === 'admin' || raw === 'platform' ? raw : undefined;
110
+ }
111
+
98
112
  function toEntry(app: RawPortalApplication, slug?: string): PortalCatalogEntry {
99
113
  return {
100
114
  code: app.code,
@@ -164,13 +178,18 @@ export function usePortalCatalog(options: UsePortalCatalogOptions = {}): UsePort
164
178
  return (query.data ?? []).filter((app) => !excluded.has(app.code));
165
179
  }, [query.data, exclude]);
166
180
 
167
- // 可见性(入口门禁镜像)+ allowed_roles 叠加
168
- // (allowed_roles 数据通路后端尚未透出——tenant-service 列表 DTO 无 config 字段;
169
- // 存在时叠加生效,见 usePortalCatalog 审计记录)
181
+ // 可见性 = 受众判定 + allowed_roles 叠加。
182
+ // 受众来源**优先数据**(服务端 `config.portal.audience`),缺失才回落注册表 ——
183
+ // 这样后端把受众写进目录后,加门户/调可见性不再需要前端发版(ADR §9-1)。
170
184
  const visiblePortals = useMemo(
171
185
  () =>
172
186
  excludedPortals.filter((app) => {
173
- if (!isPortalVisible(app.code, { role, isPlatformMember })) return false;
187
+ const ctx = { role, isPlatformMember };
188
+ const dataAudience = toKnownAudience(app.config?.portal?.audience);
189
+ const visible = dataAudience
190
+ ? isPortalVisibleByAudience(dataAudience, ctx)
191
+ : isPortalVisible(app.code, ctx);
192
+ if (!visible) return false;
174
193
  const allowedRoles =
175
194
  app.config?.portal?.allowedRoles ?? app.config?.portal?.allowed_roles;
176
195
  return !allowedRoles || allowedRoles.includes(role ?? '');
@@ -325,13 +325,25 @@ export function getLegacyEnvKey(code: string): string | undefined {
325
325
  return BY_CODE.get(code)?.legacyEnvKey;
326
326
  }
327
327
 
328
- /** 门户可见性(入口门禁镜像):未登记的门户默认放行(叠加原则,不误伤)。 */
329
- export function isPortalVisibleForAudience(
330
- code: string,
328
+ /**
329
+ * 按**受众**判定可见性(入口门禁镜像)。
330
+ *
331
+ * 同一函数服务两个数据源:注册表的 `audience` 与服务端目录里的 `config.portal.audience`。
332
+ * 未知/缺失受众一律按 `public`(叠加原则,不误伤新门户)。
333
+ */
334
+ export function isPortalVisibleByAudience(
335
+ audience: string | null | undefined,
331
336
  ctx: { role: string | null; isPlatformMember: boolean },
332
337
  ): boolean {
333
- const audience = BY_CODE.get(code)?.audience ?? 'public';
334
338
  if (audience === 'platform') return ctx.isPlatformMember;
335
339
  if (audience === 'admin') return ctx.role !== null && ADMIN_PLANE_ROLES.includes(ctx.role);
336
340
  return true;
337
341
  }
342
+
343
+ /** 未登记的门户默认放行(叠加原则,不误伤)。 */
344
+ export function isPortalVisibleForAudience(
345
+ code: string,
346
+ ctx: { role: string | null; isPlatformMember: boolean },
347
+ ): boolean {
348
+ return isPortalVisibleByAudience(BY_CODE.get(code)?.audience, ctx);
349
+ }