Skip to content

Commit

Permalink
fix(database): canModify does not check flattened group extension fie…
Browse files Browse the repository at this point in the history
…lds (#1136)

* fix(database): canModify does not check flattened extension group fields

* fix: remove unused lib
  • Loading branch information
ChrisPdgn authored Sep 9, 2024
1 parent 8604b92 commit 1eef736
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,31 @@ export function convertObjectToDotNotation(
keyMapping: any = {},
parentKey: string | null = null,
prefix: string = '',
separator: string = '_',
) {
for (const key of Object.keys(schema)) {
if (!isArray(schema[key]) && isObject(schema[key])) {
const extraction = extractObjectType(schema[key]);
if (!extraction.hasOwnProperty('type')) {
const newParentKey = parentKey ? `${parentKey}.${key}` : key;
const newPrefix = prefix ? `${prefix}_${key}` : key;
const newPrefix = prefix ? `${prefix}${separator}${key}` : key;
convertObjectToDotNotation(
extraction,
resSchema,
keyMapping,
newParentKey,
newPrefix,
separator,
);

// Remove the original key from resSchema
if (prefix) {
delete resSchema[`${prefix}_${key}`];
delete resSchema[`${prefix}${separator}${key}`];
} else {
delete resSchema[key];
}
} else {
const newKey = prefix ? `${prefix}_${key}` : key;
const newKey = prefix ? `${prefix}${separator}${key}` : key;
resSchema[newKey] = extraction;
if (parentKey || prefix) {
keyMapping[newKey] = {
Expand All @@ -78,7 +80,7 @@ export function convertObjectToDotNotation(
}
}
} else {
const newKey = prefix ? `${prefix}_${key}` : key;
const newKey = prefix ? `${prefix}${separator}${key}` : key;
resSchema[newKey] = schema[key];
if (parentKey || prefix) {
keyMapping[newKey] = {
Expand Down
14 changes: 13 additions & 1 deletion modules/database/src/permissions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DeclaredSchemaExtension, Schema } from '../interfaces/index.js';
import { ConduitModel, Indexable } from '@conduitplatform/grpc-sdk';
import { convertObjectToDotNotation } from '../adapters/sequelize-adapter/utils/extractors/index.js';

export async function canCreate(moduleName: string, schema: Schema) {
if (moduleName === 'database' && schema.originalSchema.name === '_DeclaredSchema')
Expand Down Expand Up @@ -29,7 +30,18 @@ export async function canModify(moduleName: string, schema: Schema, data?: Index
extension.ownerModule === moduleName || extension.ownerModule === 'database',
)
.map((extension: DeclaredSchemaExtension) => extension.fields)
.map((fields: ConduitModel) => Object.keys(fields))
.map((fields: ConduitModel) => {
const flattenedFields = {};
convertObjectToDotNotation(
fields,
flattenedFields,
undefined,
undefined,
'',
'.',
);
return Object.keys(flattenedFields);
})
.reduce((acc: string[], curr: string[]) => [...acc, ...curr], []);
const dataFields = Object.keys(data);
return dataFields.every((field: string) => extensionFields.includes(field));
Expand Down

0 comments on commit 1eef736

Please sign in to comment.