Files
directus/app/src/utils/is-allowed.ts
Rijk van Zanten d6846d74eb Refactor unnecessary nested app folders (#14580)
* Remove unused nested folders from components

* Remove nested folders

* Standardize composables output

* Fix import inconsistencies

* Same trick for directives

* Same for routes

* Replace reliance root grouped export in favor of explicit imports

* Replace reliance on implicit imports

* Remove nested folder structure

* Consistent use of non-default exports in utils

* Remove nested folder structure from private components

* Fix test mock

* Remove extraneous component registration for valuenull

* Fix stores provider

* Fix logo sprite
2022-07-22 15:10:28 -04:00

45 lines
1.3 KiB
TypeScript

import { usePermissionsStore } from '@/stores/permissions';
import { useUserStore } from '@/stores/user';
import { FieldFilter, Permission } from '@directus/shared/types';
import { generateJoi } from '@directus/shared/utils';
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 && action !== 'share') return false;
if (strict && action !== 'share' && permissionInfo.fields!.includes('*') === false && value) {
const allowedFields = permissionInfo.fields;
const attemptedFields = Object.keys(value);
if (attemptedFields.every((field) => allowedFields!.includes(field)) === false) return false;
}
if (!permissionInfo.permissions || Object.keys(permissionInfo.permissions).length === 0) return true;
const schema = generateJoi(permissionInfo.permissions as FieldFilter);
const { error } = schema.validate(value);
if (!error) {
return true;
}
return false;
}