refactor(admin): Rework permission routes and operations dashboard
This commit is contained in:
@@ -21,6 +21,8 @@ export const useUserStore = defineStore('admin-user', () => {
|
||||
const nickname = computed(() => userInfo.value?.nickname)
|
||||
const avatar = computed(() => userInfo.value?.avatar)
|
||||
const role = computed(() => userInfo.value?.role)
|
||||
const roles = computed(() => userInfo.value?.roles || [])
|
||||
const permissions = computed(() => userInfo.value?.permissions || [])
|
||||
|
||||
// Actions
|
||||
function setToken(accessToken: string, refresh?: string) {
|
||||
@@ -68,6 +70,26 @@ export const useUserStore = defineStore('admin-user', () => {
|
||||
localStorage.removeItem(USER_INFO_KEY)
|
||||
}
|
||||
|
||||
function hasPermission(permission: string | string[]) {
|
||||
const currentPermissions = permissions.value
|
||||
const currentRoles = roles.value
|
||||
if (currentRoles.includes('ADMIN_SUPER') || currentRoles.includes('super-admin')) {
|
||||
return true
|
||||
}
|
||||
if (Array.isArray(permission)) {
|
||||
return permission.some((item) => currentPermissions.includes(item))
|
||||
}
|
||||
return currentPermissions.includes(permission)
|
||||
}
|
||||
|
||||
function hasRole(targetRole: string | string[]) {
|
||||
const currentRoles = roles.value
|
||||
if (Array.isArray(targetRole)) {
|
||||
return targetRole.some((item) => currentRoles.includes(item))
|
||||
}
|
||||
return currentRoles.includes(targetRole)
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
token,
|
||||
@@ -80,12 +102,16 @@ export const useUserStore = defineStore('admin-user', () => {
|
||||
nickname,
|
||||
avatar,
|
||||
role,
|
||||
roles,
|
||||
permissions,
|
||||
// Actions
|
||||
setToken,
|
||||
setUserInfo,
|
||||
login,
|
||||
fetchUserInfo,
|
||||
logout,
|
||||
clearAuth
|
||||
clearAuth,
|
||||
hasPermission,
|
||||
hasRole
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user