In this PR we are handling permissions when using twentyORMGlobalManager, and handling permissions for rest api and api key
87 lines
2.5 KiB
TypeScript
87 lines
2.5 KiB
TypeScript
import { ObjectRecordsPermissions } from 'twenty-shared/types';
|
|
import { QueryExpressionMap } from 'typeorm/query-builder/QueryExpressionMap';
|
|
|
|
import {
|
|
PermissionsException,
|
|
PermissionsExceptionCode,
|
|
PermissionsExceptionMessage,
|
|
} from 'src/engine/metadata-modules/permissions/permissions.exception';
|
|
import { ObjectMetadataMaps } from 'src/engine/metadata-modules/types/object-metadata-maps';
|
|
|
|
const getTargetEntityAndOperationType = (expressionMap: QueryExpressionMap) => {
|
|
const mainEntity = expressionMap.aliases[0].metadata.name;
|
|
const operationType = expressionMap.queryType;
|
|
|
|
return {
|
|
mainEntity,
|
|
operationType,
|
|
};
|
|
};
|
|
|
|
export const validateQueryIsPermittedOrThrow = (
|
|
expressionMap: QueryExpressionMap,
|
|
objectRecordsPermissions: ObjectRecordsPermissions,
|
|
objectMetadataMaps: ObjectMetadataMaps,
|
|
shouldBypassPermissionChecks: boolean,
|
|
) => {
|
|
if (shouldBypassPermissionChecks) {
|
|
return;
|
|
}
|
|
|
|
const { mainEntity, operationType } =
|
|
getTargetEntityAndOperationType(expressionMap);
|
|
|
|
const objectMetadataIdForEntity =
|
|
objectMetadataMaps.idByNameSingular[mainEntity];
|
|
|
|
const objectMetadataIsSystem =
|
|
objectMetadataMaps.byId[objectMetadataIdForEntity]?.isSystem === true;
|
|
|
|
if (objectMetadataIsSystem) {
|
|
return;
|
|
}
|
|
|
|
const permissionsForEntity = objectRecordsPermissions[mainEntity];
|
|
|
|
switch (operationType) {
|
|
case 'select':
|
|
if (!permissionsForEntity?.canRead) {
|
|
throw new PermissionsException(
|
|
PermissionsExceptionMessage.PERMISSION_DENIED,
|
|
PermissionsExceptionCode.PERMISSION_DENIED,
|
|
);
|
|
}
|
|
break;
|
|
case 'insert':
|
|
case 'update':
|
|
if (!permissionsForEntity?.canUpdate) {
|
|
throw new PermissionsException(
|
|
PermissionsExceptionMessage.PERMISSION_DENIED,
|
|
PermissionsExceptionCode.PERMISSION_DENIED,
|
|
);
|
|
}
|
|
break;
|
|
case 'delete':
|
|
if (!permissionsForEntity?.canDestroy) {
|
|
throw new PermissionsException(
|
|
PermissionsExceptionMessage.PERMISSION_DENIED,
|
|
PermissionsExceptionCode.PERMISSION_DENIED,
|
|
);
|
|
}
|
|
break;
|
|
case 'soft-delete':
|
|
if (!permissionsForEntity?.canSoftDelete) {
|
|
throw new PermissionsException(
|
|
PermissionsExceptionMessage.PERMISSION_DENIED,
|
|
PermissionsExceptionCode.PERMISSION_DENIED,
|
|
);
|
|
}
|
|
break;
|
|
default:
|
|
throw new PermissionsException(
|
|
PermissionsExceptionMessage.UNKNOWN_OPERATION_NAME,
|
|
PermissionsExceptionCode.UNKNOWN_OPERATION_NAME,
|
|
);
|
|
}
|
|
};
|