lty/qy-lty-admin/lib/permissions.ts
pmc bd95ba470c feat: update admin panel, API modules, and add migrations
- Update food, outfits, props, home-decor pages and components
- Add permissions page and sidebar updates
- Update API client and all API modules (auth, food, dances, etc.)
- Add card model migrations for optional fields
- Update Django views, serializers, and authentication
- Add affinity level migrations and user app updates
- Add project documentation

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-20 13:06:50 +08:00

123 lines
3.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 权限矩阵 - 定义各角色对各模块的访问权限
*
* 权限矩阵对照表:
* | 模块 | 超级管理员 | 内容管理员 | AI模型管理员 | 卡牌管理员 | 查看者 |
* |-------------|-----------|-----------|------------|-----------|-------|
* | 仪表盘查看 | ✓ | ✓ | ✓ | ✓ | ✓ |
* | 用户管理 | ✓ | | | | |
* | 角色权限管理 | ✓ | | | | |
* | AI模型管理 | ✓ | | ✓ | | |
* | 服装管理 | ✓ | ✓ | | ✓ | |
* | 道具管理 | ✓ | ✓ | | ✓ | |
* | 歌曲管理 | ✓ | ✓ | | | |
* | 系统设置 | ✓ | | | | |
*/
// 所有可识别的角色名称
export type RoleName = "超级管理员" | "内容管理员" | "AI模型管理员" | "卡牌管理员" | "查看者" | "管理员";
// 模块权限 key
export type PermissionModule =
| "dashboard"
| "users"
| "permissions"
| "ai-model"
| "outfits"
| "props"
| "home-decor"
| "food"
| "songs"
| "dances"
| "achievements"
| "affinity"
| "settings";
// 权限矩阵定义
const PERMISSION_MATRIX: Record<RoleName, PermissionModule[]> = {
: [
"dashboard", "users", "permissions", "ai-model",
"outfits", "props", "home-decor", "food",
"songs", "dances", "achievements", "affinity", "settings",
],
: [
"dashboard", "outfits", "props", "home-decor", "food",
"songs", "dances", "achievements", "affinity",
],
AI模型管理员: [
"dashboard", "ai-model",
],
: [
"dashboard", "outfits", "props", "home-decor", "food",
],
: [
"dashboard",
],
// 后备角色:普通管理员等同于查看者
: [
"dashboard",
],
};
/**
* 获取当前用户角色
*/
export function getUserRole(): RoleName {
if (typeof window === "undefined") return "查看者";
const role = localStorage.getItem("user_role");
if (role && role in PERMISSION_MATRIX) {
return role as RoleName;
}
return "查看者";
}
/**
* 获取当前用户有权限的模块列表
*/
export function getAllowedModules(): PermissionModule[] {
const role = getUserRole();
return PERMISSION_MATRIX[role] || PERMISSION_MATRIX["查看者"];
}
/**
* 检查当前用户是否有某个模块的权限
*/
export function hasPermission(module: PermissionModule): boolean {
return getAllowedModules().includes(module);
}
/**
* 根据路径判断所需的模块权限
*/
export function getModuleFromPath(pathname: string): PermissionModule | null {
// 去掉开头的斜杠,取第一段路径
const segment = pathname.replace(/^\//, "").split("/")[0];
const pathMap: Record<string, PermissionModule> = {
"": "dashboard",
"ai-model": "ai-model",
"outfits": "outfits",
"props": "props",
"home-decor": "home-decor",
"food": "food",
"songs": "songs",
"dances": "dances",
"achievements": "achievements",
"affinity": "affinity",
"users": "users",
"permissions": "permissions",
"settings": "settings",
};
return pathMap[segment] ?? null;
}
/**
* 检查当前用户是否有访问某个路径的权限
*/
export function hasPathPermission(pathname: string): boolean {
const module = getModuleFromPath(pathname);
if (module === null) return true; // 未知路径默认允许(如 login、register
return hasPermission(module);
}