@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 +1 -1
- package/src/permissions.svelte.ts +25 -17
package/package.json
CHANGED
|
@@ -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
|
|
126
|
-
*
|
|
127
|
-
*
|
|
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 (
|
|
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
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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;
|