From 39fc908de1945f2aaea18d14e6bce703f6bf0c06 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Fri, 29 Apr 2022 15:26:26 +0500 Subject: [PATCH] fix: update permission model --- api/src/model/Permission.ts | 158 +++--------------------------------- 1 file changed, 13 insertions(+), 145 deletions(-) diff --git a/api/src/model/Permission.ts b/api/src/model/Permission.ts index cd21ef1..6343e4f 100644 --- a/api/src/model/Permission.ts +++ b/api/src/model/Permission.ts @@ -1,38 +1,16 @@ -import { string } from 'joi' import mongoose, { Schema, model, Document, Model } from 'mongoose' const AutoIncrement = require('mongoose-sequence')(mongoose) -export interface PermissionPayload { - /** - * Name of affected resource - * @example "/SASjsApi/code/execute" - */ +interface IPermissionDocument extends Document { uri: string -} - -interface IPermissionDocument extends PermissionPayload, Document { + setting: string permissionId: number - users: [{ id: Schema.Types.ObjectId; setting: string }] - groups: [{ id: Schema.Types.ObjectId; setting: string }] - clients: [{ id: Schema.Types.ObjectId; setting: string }] + user: Schema.Types.ObjectId + group: Schema.Types.ObjectId + client: Schema.Types.ObjectId } -interface IPermission extends IPermissionDocument { - addPermission( - objectId: Schema.Types.ObjectId, - objectType: string, - setting: string - ): Promise - updatePermission( - objectId: Schema.Types.ObjectId, - objectType: string, - setting: string - ): Promise - removePermission( - objectId: Schema.Types.ObjectId, - objectType: string - ): Promise -} +interface IPermission extends IPermissionDocument {} interface IPermissionModel extends Model {} @@ -41,127 +19,17 @@ const permissionSchema = new Schema({ type: String, required: true }, - users: [ - { - id: { type: Schema.Types.ObjectId, ref: 'User' }, - setting: { type: String } - } - ], - groups: [ - { - id: { type: Schema.Types.ObjectId, ref: 'Group' }, - setting: { type: String } - } - ], - clients: [ - { - id: { type: Schema.Types.ObjectId, ref: 'Client' }, - setting: { type: String } - } - ] + setting: { + type: String, + required: true + }, + user: { type: Schema.Types.ObjectId, ref: 'User' }, + group: { type: Schema.Types.ObjectId, ref: 'Group' }, + client: { type: Schema.Types.ObjectId, ref: 'Client' } }) permissionSchema.plugin(AutoIncrement, { inc_field: 'permissionId' }) -// Instance Methods -permissionSchema.method( - 'addPermission', - async function ( - objectId: Schema.Types.ObjectId, - objectType: string, - setting: string - ) { - switch (objectType) { - case 'User': - const user = this.users.find((obj) => obj.id === objectId) - if (!user) { - this.users.push({ id: objectId, setting }) - } - this.markModified('users') - break - case 'Group': - const group = this.groups.find((obj) => obj.id === objectId) - if (!group) { - this.groups.push({ id: objectId, setting }) - } - this.markModified('groups') - break - case 'Client': - const client = this.clients.find((obj) => obj.id === objectId) - if (!client) { - this.clients.push({ id: objectId, setting }) - } - this.markModified('clients') - break - } - return this.save() - } -) - -permissionSchema.method( - 'updatePermission', - async function ( - objectId: Schema.Types.ObjectId, - objectType: string, - setting: string - ) { - switch (objectType) { - case 'User': - const user = this.users.find( - (obj) => obj.id === objectId && obj.setting !== setting - ) - if (user) user.setting = setting - this.markModified('users') - break - case 'Group': - const group = this.groups.find( - (obj) => obj.id === objectId && obj.setting !== setting - ) - if (group) group.setting = setting - this.markModified('groups') - break - case 'Client': - const client = this.clients.find( - (obj) => obj.id === objectId && obj.setting !== setting - ) - if (client) client.setting = setting - this.markModified('clients') - break - } - return this.save() - } -) - -permissionSchema.method( - 'removePermission', - async function (objectId: Schema.Types.ObjectId, objectType: string) { - switch (objectType) { - case 'User': - const userIndex = this.users.findIndex((obj) => obj.id === objectId) - if (userIndex !== -1) { - this.users.splice(userIndex, 1) - } - this.markModified('users') - break - case 'Group': - const groupIndex = this.groups.findIndex((obj) => obj.id === objectId) - if (groupIndex !== -1) { - this.groups.splice(groupIndex, 1) - } - this.markModified('groups') - break - case 'Client': - const clientIndex = this.clients.findIndex((obj) => obj.id === objectId) - if (clientIndex !== -1) { - this.clients.splice(clientIndex, 1) - } - this.markModified('clients') - break - } - return this.save() - } -) - export const Permission: IPermissionModel = model< IPermission, IPermissionModel