forked from ddrilling/asb_cloud_front
Добавлен хук получения разрешений
This commit is contained in:
parent
99b3978418
commit
fa8e2d234d
@ -1,8 +1,33 @@
|
||||
import { useMemo } from 'react'
|
||||
|
||||
import { getUserPermissions, getUserRoles } from './storage'
|
||||
|
||||
export type Role = string
|
||||
export type Permission = string
|
||||
|
||||
export type ServiceName = string
|
||||
export type ServiceRequestType = 'get' | 'edit' | 'delete'
|
||||
export type PermissionRequest = `${ServiceName}.${ServiceRequestType}`
|
||||
|
||||
export function isRequestType(value: string): value is ServiceRequestType {
|
||||
return ['get', 'edit', 'delete'].includes(value)
|
||||
}
|
||||
|
||||
export const usePermissions = (...values: PermissionRequest[]) => {
|
||||
const result = useMemo(() => {
|
||||
const permissions: Record<string, Partial<Record<ServiceRequestType, boolean>>> = {}
|
||||
values.forEach((key) => {
|
||||
const [service, type] = key.toLowerCase().split('.')
|
||||
if (!isRequestType(type)) return
|
||||
permissions[service] = permissions[service] ?? {}
|
||||
permissions[service][type] = hasPermission(key)
|
||||
})
|
||||
return permissions
|
||||
}, [values])
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export const hasPermission = (permission?: Permission | Permission[], userPermissions?: Permission[]): boolean => {
|
||||
if (!Array.isArray(permission) && typeof permission !== 'string')
|
||||
return true
|
||||
|
Loading…
Reference in New Issue
Block a user