feat: 搭建后台管理框架
This commit is contained in:
1 parent
6d28bab72b
commit
40546bfd01
238 files changed
+18160
-3837
No files matched your search
+27
-8
@@ -3,23 +3,42 @@ import { defaultRolePermissions } from '../permissions/permissions'
|
||||
import { useAuth } from './auth'
|
||||
import { customMenus } from '../data/customMenus'
|
||||
|
||||
const rolePermissions = reactive(Object.fromEntries(Object.entries(defaultRolePermissions).map(([role, list]) => [role, [...list]])))
|
||||
rolePermissions['超级管理员'].push(...customMenus.map(menu => menu.permission))
|
||||
const rolePermissions = reactive(
|
||||
Object.fromEntries(
|
||||
Object.entries(defaultRolePermissions).map(([role, list]) => [role, [...list]])
|
||||
)
|
||||
)
|
||||
rolePermissions['超级管理员'].push(...customMenus.map((menu) => menu.permission))
|
||||
|
||||
export function useRbac() {
|
||||
const { currentRole } = useAuth()
|
||||
const permissions = computed(() => rolePermissions[currentRole.value] || [])
|
||||
const can = permission => permissions.value.includes(permission)
|
||||
const can = (permission) => permissions.value.includes(permission)
|
||||
function updateRolePermission(role, permission, enabled) {
|
||||
const current = rolePermissions[role] || (rolePermissions[role] = [])
|
||||
rolePermissions[role] = enabled ? [...new Set([...current, permission])] : current.filter(item => item !== permission)
|
||||
rolePermissions[role] = enabled
|
||||
? [...new Set([...current, permission])]
|
||||
: current.filter((item) => item !== permission)
|
||||
}
|
||||
function createRole(name) {
|
||||
if (!rolePermissions[name]) rolePermissions[name] = []
|
||||
}
|
||||
function createRole(name) { if (!rolePermissions[name]) rolePermissions[name] = [] }
|
||||
function createPermission(permission) {
|
||||
rolePermissions['超级管理员'] = [...new Set([...(rolePermissions['超级管理员'] || []), permission])]
|
||||
rolePermissions['超级管理员'] = [
|
||||
...new Set([...(rolePermissions['超级管理员'] || []), permission])
|
||||
]
|
||||
}
|
||||
function removePermission(permission) {
|
||||
for (const role of Object.keys(rolePermissions)) rolePermissions[role] = rolePermissions[role].filter(item => item !== permission)
|
||||
for (const role of Object.keys(rolePermissions))
|
||||
rolePermissions[role] = rolePermissions[role].filter((item) => item !== permission)
|
||||
}
|
||||
return {
|
||||
permissions,
|
||||
can,
|
||||
rolePermissions,
|
||||
updateRolePermission,
|
||||
createRole,
|
||||
createPermission,
|
||||
removePermission
|
||||
}
|
||||
return { permissions, can, rolePermissions, updateRolePermission, createRole, createPermission, removePermission }
|
||||
}
|
||||
Reference in new issue
Block a user