@svadmin/core 0.26.0 → 0.27.0

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": "@svadmin/core",
3
- "version": "0.26.0",
3
+ "version": "0.27.0",
4
4
  "author": "zuohuadong",
5
5
  "repository": {
6
6
  "type": "git",
@@ -98,16 +98,19 @@ export async function canAccessAsync(resourceOrBatch: string | CanParams[], acti
98
98
 
99
99
  // ─── Feature Gate ─────────────────────────────────────────────
100
100
 
101
- /**
102
- * 角色层级定义 (高权 → 低权)
103
- */
104
- const ROLE_HIERARCHY = ["superadmin", "admin", "manager", "user", "viewer", "guest"];
105
-
106
101
  export interface FeatureGateConfig {
107
102
  /** 允许访问的角色列表 */
108
103
  roles?: string[];
109
- /** 需要的最低角色 (含以上所有角色) */
104
+ /**
105
+ * 需要的最低角色 (含以上所有角色)。
106
+ * 使用 minRole 时必须同时提供 roleHierarchy。
107
+ */
110
108
  minRole?: string;
109
+ /**
110
+ * 角色层级定义 (高权 → 低权),用于 minRole 比较。
111
+ * 由调用方按自身业务定义,框架不预设任何角色。
112
+ */
113
+ roleHierarchy?: string[];
111
114
  /** 需要的权限列表 (全部匹配) */
112
115
  permissions?: string[];
113
116
  }
@@ -119,34 +122,39 @@ export interface FeatureGateUser {
119
122
 
120
123
  /**
121
124
  * 创建功能门控函数 — 基于角色和权限判断用户是否可访问某功能。
125
+ * 不预设任何角色层级,由调用方完全定义。
122
126
  *
123
127
  * @example
124
128
  * ```ts
125
- * const canUseCode = createFeatureGate({
126
- * minRole: 'manager',
127
- * permissions: ['code:execute'],
129
+ * const HIERARCHY = ['admin', 'editor', 'viewer'];
130
+ * const canEdit = createFeatureGate({
131
+ * minRole: 'editor',
132
+ * roleHierarchy: HIERARCHY,
133
+ * permissions: ['content:write'],
128
134
  * });
129
135
  *
130
- * if (canUseCode(user)) { ... }
136
+ * if (canEdit(user)) { ... }
131
137
  * ```
132
138
  */
133
139
  export function createFeatureGate(config: FeatureGateConfig): (user: FeatureGateUser) => boolean {
134
140
  return (user: FeatureGateUser): boolean => {
135
- // 角色白名单
136
141
  if (config.roles && !config.roles.includes(user.role)) {
137
142
  return false;
138
143
  }
139
144
 
140
- // 最低角色层级检查
141
145
  if (config.minRole) {
142
- const userIndex = ROLE_HIERARCHY.indexOf(user.role);
143
- const minIndex = ROLE_HIERARCHY.indexOf(config.minRole);
144
- if (userIndex === -1 || minIndex === -1 || userIndex > minIndex) {
145
- return false;
146
+ const hierarchy = config.roleHierarchy;
147
+ if (!hierarchy || hierarchy.length === 0) {
148
+ if (user.role !== config.minRole) return false;
149
+ } else {
150
+ const userIndex = hierarchy.indexOf(user.role);
151
+ const minIndex = hierarchy.indexOf(config.minRole);
152
+ if (userIndex === -1 || minIndex === -1 || userIndex > minIndex) {
153
+ return false;
154
+ }
146
155
  }
147
156
  }
148
157
 
149
- // 权限检查 (全部匹配)
150
158
  if (config.permissions && config.permissions.length > 0) {
151
159
  const hasAll = config.permissions.every((permission) => {
152
160
  if (user.permissions.includes("*")) return true;