Path: blob/1.0-develop/resources/scripts/plugins/usePermissions.ts
7458 views
import { ServerContext } from '@/state/server';1import { useDeepCompareMemo } from '@/plugins/useDeepCompareMemo';23export const usePermissions = (action: string | string[]): boolean[] => {4const userPermissions = ServerContext.useStoreState((state) => state.server.permissions);56return useDeepCompareMemo(() => {7if (userPermissions[0] === '*') {8return Array(Array.isArray(action) ? action.length : 1).fill(true);9}1011return (Array.isArray(action) ? action : [action]).map(12(permission) =>13// Allows checking for any permission matching a name, for example files.*14// will return if the user has any permission under the file.XYZ namespace.15(permission.endsWith('.*') &&16userPermissions.filter((p) => p.startsWith(permission.split('.')[0])).length > 0) ||17// Otherwise just check if the entire permission exists in the array or not.18userPermissions.indexOf(permission) >= 019);20}, [action, userPermissions]);21};222324