Skip to content

Commit

Permalink
Fix schema permissions nesting (#25)
Browse files Browse the repository at this point in the history
* fix(grpc-sdk,commons,database): schema permissions modelOptions nesting

* fix(authentication,chat,cms,email,forms,push-notifications,storage,admin,config,schemas): schema permissions modelOptions nesting
  • Loading branch information
kon14 committed Feb 10, 2022
1 parent 740348f commit a60337d
Show file tree
Hide file tree
Showing 23 changed files with 40 additions and 38 deletions.
12 changes: 6 additions & 6 deletions libraries/grpc-sdk/src/interfaces/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export const ConduitModelOptionsPermModifyType = ['Everything', 'Nothing', 'Exte
export interface ConduitModelOptions {
timestamps?: boolean;
_id?: boolean;
permissions?: {
extendable: boolean,
canCreate: boolean,
canModify: 'Everything' | 'Nothing' | 'ExtensionOnly',
canDelete: boolean,
},
conduit?: {
[field: string]: any,
permissions?: {
extendable: boolean,
canCreate: boolean,
canModify: 'Everything' | 'Nothing' | 'ExtensionOnly',
canDelete: boolean,
},
};
}
2 changes: 1 addition & 1 deletion modules/authentication/src/models/AccessToken.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class AccessToken extends ConduitActiveSchema<AccessToken> {
Expand Down
2 changes: 1 addition & 1 deletion modules/authentication/src/models/RefreshToken.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class RefreshToken extends ConduitActiveSchema<RefreshToken> {
Expand Down
2 changes: 1 addition & 1 deletion modules/authentication/src/models/Service.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;
export class Service extends ConduitActiveSchema<Service> {
private static _instance: Service;
Expand Down
2 changes: 1 addition & 1 deletion modules/authentication/src/models/Token.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class Token extends ConduitActiveSchema<Token> {
Expand Down
2 changes: 1 addition & 1 deletion modules/authentication/src/models/User.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class User extends ConduitActiveSchema<User> {
Expand Down
2 changes: 1 addition & 1 deletion modules/chat/src/models/ChatRoom.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class ChatRoom extends ConduitActiveSchema<ChatRoom> {
Expand Down
2 changes: 1 addition & 1 deletion modules/chat/src/models/Message.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class ChatMessage extends ConduitActiveSchema<ChatMessage> {
Expand Down
3 changes: 2 additions & 1 deletion modules/cms/src/admin/schema.admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ export class SchemaAdmin {

private patchSchemaPerms(
schema: _DeclaredSchema,
perms: ConduitModelOptions['permissions'],
// @ts-ignore
perms: ConduitModelOptions['conduit']['permissions'],
) {
if (perms!.canModify && !(ConduitModelOptionsPermModifyType.includes(perms!.canModify))) {
throw new GrpcError(
Expand Down
2 changes: 1 addition & 1 deletion modules/cms/src/models/CustomEndpoints.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class CustomEndpoints extends ConduitActiveSchema<CustomEndpoints> {
Expand Down
7 changes: 4 additions & 3 deletions modules/database/src/adapters/DatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,15 @@ export abstract class DatabaseAdapter<T extends SchemaAdapter<any>> {
canCreate: true,
canModify: 'Everything',
canDelete: true,
};
} as const;
if (!schema.schemaOptions.hasOwnProperty('conduit')) schema.schemaOptions.conduit = {};
if (!schema.schemaOptions.conduit!.hasOwnProperty('permissions')) {
schema.schemaOptions.conduit!.permissions = defaultPermissions;
} else {
Object.keys(defaultPermissions).forEach((perm) => {
if (!schema.schemaOptions.conduit!.permissions.hasOwnProperty(perm)) {
schema.schemaOptions.conduit!.permissions[perm] = defaultPermissions[perm as keyof typeof defaultPermissions];
if (!schema.schemaOptions.conduit!.permissions!.hasOwnProperty(perm)) {
// @ts-ignore
schema.schemaOptions.conduit!.permissions![perm] = defaultPermissions[perm as keyof typeof defaultPermissions];
}
});
}
Expand Down
8 changes: 4 additions & 4 deletions modules/database/src/permissions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function canExtend(moduleName: string, schema: SchemaAdapter<any>)
}
return (
schema.originalSchema.ownerModule === moduleName ||
schema.originalSchema.schemaOptions.conduit!.permissions.extendable
schema.originalSchema.schemaOptions.conduit!.permissions!.extendable
)
}

Expand All @@ -19,7 +19,7 @@ export async function canCreate(moduleName: string, schema: SchemaAdapter<any>)
}
return (
schema.originalSchema.ownerModule === moduleName ||
schema.originalSchema.schemaOptions.conduit!.permissions.extendable
schema.originalSchema.schemaOptions.conduit!.permissions!.extendable
)
}

Expand All @@ -31,7 +31,7 @@ export async function canModify(moduleName: string, schema: SchemaAdapter<any>)
}
return (
schema.originalSchema.ownerModule === moduleName ||
(schema.originalSchema.schemaOptions.conduit!.permissions.canModify === 'Everything')
(schema.originalSchema.schemaOptions.conduit!.permissions!.canModify === 'Everything')
// TODO: Handle 'ExtensionOnly' once we get extensions
);
}
Expand All @@ -44,6 +44,6 @@ export async function canDelete(moduleName: string, schema: SchemaAdapter<any>)
}
return (
schema.originalSchema.ownerModule === moduleName ||
schema.originalSchema.schemaOptions.conduit!.permissions.canDelete
schema.originalSchema.schemaOptions.conduit!.permissions!.canDelete
);
}
2 changes: 1 addition & 1 deletion modules/email/src/models/EmailTemplate.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class EmailTemplate extends ConduitActiveSchema<EmailTemplate> {
Expand Down
2 changes: 1 addition & 1 deletion modules/forms/src/models/Forms.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class Forms extends ConduitActiveSchema<Forms> {
Expand Down
2 changes: 1 addition & 1 deletion modules/forms/src/models/Replies.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class FormReplies extends ConduitActiveSchema<FormReplies> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class NotificationToken extends ConduitActiveSchema<NotificationToken> {
Expand Down
2 changes: 1 addition & 1 deletion modules/storage/src/models/Container.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class _StorageContainer extends ConduitActiveSchema<_StorageContainer> {
Expand Down
2 changes: 1 addition & 1 deletion modules/storage/src/models/File.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class File extends ConduitActiveSchema<File> {
Expand Down
2 changes: 1 addition & 1 deletion modules/storage/src/models/Folder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class _StorageFolder extends ConduitActiveSchema<_StorageFolder> {
Expand Down
2 changes: 1 addition & 1 deletion packages/admin/src/models/Admin.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class Admin extends ConduitActiveSchema<Admin> {
Expand Down
12 changes: 6 additions & 6 deletions packages/commons/src/interfaces/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ export const ConduitModelOptionsPermModifyType = ['Everything', 'Nothing', 'Exte
export interface ConduitModelOptions {
timestamps?: boolean;
_id?: boolean;
permissions?: {
extendable: boolean,
canCreate: boolean,
canModify: 'Everything' | 'Nothing' | 'ExtensionOnly',
canDelete: boolean,
},
conduit?: {
[field: string]: any,
permissions?: {
extendable: boolean,
canCreate: boolean,
canModify: 'Everything' | 'Nothing' | 'ExtensionOnly',
canDelete: boolean,
},
};
}
2 changes: 1 addition & 1 deletion packages/config/src/models/Config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class Config extends ConduitActiveSchema<Config> {
Expand Down
2 changes: 1 addition & 1 deletion packages/security/src/models/Client.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const schemaOptions = {
canDelete: false,
},
},
};
} as const;
const collectionName = undefined;

export class Client extends ConduitActiveSchema<Client> {
Expand Down

0 comments on commit a60337d

Please sign in to comment.