first commit
This commit is contained in:
commit
db15eb3ee0
42 files changed
+4620
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { reactive, computed } from 'vue'
|
||||
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))
|
||||
|
||||
export function useRbac() {
|
||||
const { currentRole } = useAuth()
|
||||
const permissions = computed(() => rolePermissions[currentRole.value] || [])
|
||||
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)
|
||||
}
|
||||
function createRole(name) { if (!rolePermissions[name]) rolePermissions[name] = [] }
|
||||
function createPermission(permission) {
|
||||
rolePermissions['超级管理员'] = [...new Set([...(rolePermissions['超级管理员'] || []), permission])]
|
||||
}
|
||||
function removePermission(permission) {
|
||||
for (const role of Object.keys(rolePermissions)) rolePermissions[role] = rolePermissions[role].filter(item => item !== permission)
|
||||
}
|
||||
return { permissions, can, rolePermissions, updateRolePermission, createRole, createPermission, removePermission }
|
||||
}
|
||||
Reference in new issue
Block a user