mirror of
https://github.com/directus/directus.git
synced 2026-02-11 04:45:34 -05:00
* Move defineInterface to shared * Split up utils into node and browser utils * Move defineDisplay to shared * Move defineLayout to shared * Move defineModule to shared * Add defineEndpoint and defineHook to define-extensions * Expose define extension functions through extension-sdk * Make route type of defineEndpoint more specific * Simplify define extension functions * Deduplicate types and local types definition * Do not allow functional components in display handler * Make interface options nullable * Deduplicate extension types definition * Fix utils/node exports
44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
import { usePermissionsStore, useUserStore } from '@/stores';
|
|
import { Permission } from '@directus/shared/types';
|
|
import generateJoi from '@/utils/generate-joi';
|
|
|
|
export function isAllowed(
|
|
collection: string,
|
|
action: Permission['action'],
|
|
value: Record<string, any> | null,
|
|
strict = false
|
|
): boolean {
|
|
const permissionsStore = usePermissionsStore();
|
|
const userStore = useUserStore();
|
|
|
|
if (userStore.isAdmin === true) return true;
|
|
|
|
const permissions = permissionsStore.permissions;
|
|
|
|
const permissionInfo = permissions.find(
|
|
(permission) => permission.action === action && permission.collection === collection
|
|
);
|
|
|
|
if (!permissionInfo) return false;
|
|
if (!permissionInfo.fields) return false;
|
|
|
|
if (strict && permissionInfo.fields.includes('*') === false && value) {
|
|
const allowedFields = permissionInfo.fields;
|
|
const attemptedFields = Object.keys(value);
|
|
|
|
if (attemptedFields.every((field) => allowedFields.includes(field)) === false) return false;
|
|
}
|
|
|
|
const schema = generateJoi(permissionInfo.permissions, {
|
|
allowUnknown: true,
|
|
});
|
|
|
|
const { error } = schema.validate(value);
|
|
|
|
if (!error) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|