b2b.systemd
This commit is contained in:
@@ -2,6 +2,25 @@ import { defineStore } from 'pinia'
|
||||
import api from 'src/services/api'
|
||||
import { useAuthStore } from 'stores/authStore'
|
||||
|
||||
const ACTION_ALIASES = {
|
||||
read: ['read', 'view'],
|
||||
view: ['read', 'view'],
|
||||
write: ['write', 'insert'],
|
||||
insert: ['write', 'insert'],
|
||||
update: ['update'],
|
||||
delete: ['delete'],
|
||||
export: ['export']
|
||||
}
|
||||
|
||||
function normalizeToken (value) {
|
||||
return String(value || '').trim().toLowerCase()
|
||||
}
|
||||
|
||||
function actionCandidates (action) {
|
||||
const key = normalizeToken(action)
|
||||
return ACTION_ALIASES[key] || [key]
|
||||
}
|
||||
|
||||
|
||||
export const usePermissionStore = defineStore('permission', {
|
||||
|
||||
@@ -44,11 +63,13 @@ export const usePermissionStore = defineStore('permission', {
|
||||
|
||||
if (apiPathOrPerm.includes(':')) {
|
||||
|
||||
const [module, action] = apiPathOrPerm.split(':')
|
||||
const [moduleRaw, actionRaw] = apiPathOrPerm.split(':')
|
||||
const module = normalizeToken(moduleRaw)
|
||||
const actions = actionCandidates(actionRaw)
|
||||
|
||||
return state.matrix.some(p =>
|
||||
p.module === module &&
|
||||
p.action === action &&
|
||||
normalizeToken(p.module) === module &&
|
||||
actions.includes(normalizeToken(p.action)) &&
|
||||
p.allowed === true
|
||||
)
|
||||
}
|
||||
@@ -94,12 +115,13 @@ export const usePermissionStore = defineStore('permission', {
|
||||
hasModule: (state) => (module) => {
|
||||
|
||||
const auth = useAuthStore()
|
||||
const moduleKey = normalizeToken(module)
|
||||
|
||||
if (auth.isAdmin) return true
|
||||
if (!state.loaded) return false
|
||||
|
||||
return state.matrix.some(p =>
|
||||
p.module === module &&
|
||||
normalizeToken(p.module) === moduleKey &&
|
||||
p.allowed === true
|
||||
)
|
||||
},
|
||||
@@ -110,13 +132,15 @@ export const usePermissionStore = defineStore('permission', {
|
||||
hasPermission: (state) => (module, action) => {
|
||||
|
||||
const auth = useAuthStore()
|
||||
const moduleKey = normalizeToken(module)
|
||||
const actions = actionCandidates(action)
|
||||
|
||||
if (auth.isAdmin) return true
|
||||
if (!state.loaded) return false
|
||||
|
||||
return state.matrix.some(p =>
|
||||
p.module === module &&
|
||||
p.action === action &&
|
||||
normalizeToken(p.module) === moduleKey &&
|
||||
actions.includes(normalizeToken(p.action)) &&
|
||||
p.allowed === true
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user