permission.ts
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
export const LOGIN_PATH = '/login'
export const FORBIDDEN_PATH = '/forbidden'
export const APP_PAGE_PATHS = [
'/',
'/m/workbench',
'/m/production/sn-query',
'/work-orders',
'/sn-management',
'/operations',
'/inbox',
'/customers',
'/base-info/device-types',
'/settings',
'/settings/members',
'/settings/notifications',
'/settings/security'
] as const
export const BUTTON_PERMISSION_KEYS = [
'work-orders.create',
'work-orders.edit',
'work-orders.dispatch',
'work-orders.pause',
'work-orders.resume',
'work-orders.close',
'sn.import',
'sn.freeze',
'sn.unfreeze',
'sn.scrap',
'operations.skip',
'members.invite',
'members.edit',
'members.role.update',
'members.remove',
'customers.create',
'customers.delete',
'device-types.create',
'device-types.update',
'device-types.delete'
] as const
export type AppPagePath = (typeof APP_PAGE_PATHS)[number]
export type ButtonPermissionKey = (typeof BUTTON_PERMISSION_KEYS)[number]
export type RolePermissionMatrix = Record<string, Partial<Record<AppPagePath, ButtonPermissionKey[]>>>
const APP_PAGE_PATH_SET = new Set<string>(APP_PAGE_PATHS)
const PUBLIC_PATH_SET = new Set<string>([LOGIN_PATH, FORBIDDEN_PATH])
const MANAGER_WORK_ORDER_BUTTONS: ButtonPermissionKey[] = [
'work-orders.create',
'work-orders.edit',
'work-orders.dispatch',
'work-orders.pause',
'work-orders.resume',
'work-orders.close'
]
const MANAGER_SN_BUTTONS: ButtonPermissionKey[] = [
'sn.import',
'sn.freeze',
'sn.unfreeze',
'sn.scrap'
]
const MANAGER_OPERATION_BUTTONS: ButtonPermissionKey[] = [
'operations.skip'
]
const ADMIN_PAGE_PERMISSIONS = APP_PAGE_PATHS.reduce<Partial<Record<AppPagePath, ButtonPermissionKey[]>>>((permissions, pagePath) => {
permissions[pagePath] = [...BUTTON_PERMISSION_KEYS]
return permissions
}, {})
const ROLE_PERMISSION_MATRIX: RolePermissionMatrix = {
// admin:支持所有页面和所有按钮
admin: ADMIN_PAGE_PERMISSIONS,
// 管理员:仅支持工单管理、SN 管理、工序管理页面及对应按钮
qa_manager: {
'/work-orders': MANAGER_WORK_ORDER_BUTTONS,
'/sn-management': MANAGER_SN_BUTTONS,
'/operations': MANAGER_OPERATION_BUTTONS
},
// 操作员:只能访问移动端页面
operator: {
'/m/workbench': [],
'/m/production/sn-query': []
}
}
function normalizePath(path: string) {
const [pathname] = path.split('?')
const purePath = pathname ? pathname.split('#')[0] : '/'
const trimmed = (purePath ?? '/').trim() || '/'
if (trimmed.length > 1 && trimmed.endsWith('/')) {
return trimmed.slice(0, -1)
}
return trimmed
}
export function normalizePagePath(path: string) {
return normalizePath(path)
}
export function toPagePath(path: string): AppPagePath | null {
const normalized = normalizePath(path)
if (!APP_PAGE_PATH_SET.has(normalized)) {
return null
}
return normalized as AppPagePath
}
function getRolePagePermissions(role: string) {
return ROLE_PERMISSION_MATRIX[role] ?? null
}
export function getAccessiblePagesForRoles(roles: string[]): Set<AppPagePath> {
const pages = new Set<AppPagePath>()
for (const role of roles) {
const rolePermissions = getRolePagePermissions(role)
if (!rolePermissions) {
continue
}
for (const rawPagePath of Object.keys(rolePermissions)) {
const pagePath = toPagePath(rawPagePath)
if (pagePath) {
pages.add(pagePath)
}
}
}
return pages
}
export function getAllowedButtonsForPage(roles: string[], pagePath: AppPagePath): Set<ButtonPermissionKey> {
const buttons = new Set<ButtonPermissionKey>()
for (const role of roles) {
const rolePermissions = getRolePagePermissions(role)
if (!rolePermissions || !rolePermissions[pagePath]) {
continue
}
for (const button of rolePermissions[pagePath] ?? []) {
buttons.add(button)
}
}
return buttons
}
export function isPublicPath(path: string) {
return PUBLIC_PATH_SET.has(normalizePath(path))
}
export function isProtectedPage(path: string) {
return toPagePath(path) !== null
}
export function canAccessPageByRoles(roles: string[], pagePath: string) {
const pageKey = toPagePath(pagePath)
if (!pageKey) {
return true
}
const accessiblePages = getAccessiblePagesForRoles(roles)
return accessiblePages.has(pageKey)
}
export function canUseButtonByRoles(roles: string[], pagePath: string, buttonKey: ButtonPermissionKey) {
const pageKey = toPagePath(pagePath)
if (!pageKey) {
return false
}
const accessiblePages = getAccessiblePagesForRoles(roles)
if (!accessiblePages.has(pageKey)) {
return false
}
const allowedButtons = getAllowedButtonsForPage(roles, pageKey)
return allowedButtons.has(buttonKey)
}