mirror of
https://github.com/sasjs/server.git
synced 2025-12-11 03:34:35 +00:00
fix: update permission model
This commit is contained in:
@@ -1,38 +1,16 @@
|
|||||||
import { string } from 'joi'
|
|
||||||
import mongoose, { Schema, model, Document, Model } from 'mongoose'
|
import mongoose, { Schema, model, Document, Model } from 'mongoose'
|
||||||
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
const AutoIncrement = require('mongoose-sequence')(mongoose)
|
||||||
|
|
||||||
export interface PermissionPayload {
|
interface IPermissionDocument extends Document {
|
||||||
/**
|
|
||||||
* Name of affected resource
|
|
||||||
* @example "/SASjsApi/code/execute"
|
|
||||||
*/
|
|
||||||
uri: string
|
uri: string
|
||||||
}
|
setting: string
|
||||||
|
|
||||||
interface IPermissionDocument extends PermissionPayload, Document {
|
|
||||||
permissionId: number
|
permissionId: number
|
||||||
users: [{ id: Schema.Types.ObjectId; setting: string }]
|
user: Schema.Types.ObjectId
|
||||||
groups: [{ id: Schema.Types.ObjectId; setting: string }]
|
group: Schema.Types.ObjectId
|
||||||
clients: [{ id: Schema.Types.ObjectId; setting: string }]
|
client: Schema.Types.ObjectId
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IPermission extends IPermissionDocument {
|
interface IPermission extends IPermissionDocument {}
|
||||||
addPermission(
|
|
||||||
objectId: Schema.Types.ObjectId,
|
|
||||||
objectType: string,
|
|
||||||
setting: string
|
|
||||||
): Promise<IPermission>
|
|
||||||
updatePermission(
|
|
||||||
objectId: Schema.Types.ObjectId,
|
|
||||||
objectType: string,
|
|
||||||
setting: string
|
|
||||||
): Promise<IPermission>
|
|
||||||
removePermission(
|
|
||||||
objectId: Schema.Types.ObjectId,
|
|
||||||
objectType: string
|
|
||||||
): Promise<IPermission>
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IPermissionModel extends Model<IPermission> {}
|
interface IPermissionModel extends Model<IPermission> {}
|
||||||
|
|
||||||
@@ -41,127 +19,17 @@ const permissionSchema = new Schema<IPermissionDocument>({
|
|||||||
type: String,
|
type: String,
|
||||||
required: true
|
required: true
|
||||||
},
|
},
|
||||||
users: [
|
setting: {
|
||||||
{
|
type: String,
|
||||||
id: { type: Schema.Types.ObjectId, ref: 'User' },
|
required: true
|
||||||
setting: { type: String }
|
},
|
||||||
}
|
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
||||||
],
|
group: { type: Schema.Types.ObjectId, ref: 'Group' },
|
||||||
groups: [
|
client: { type: Schema.Types.ObjectId, ref: 'Client' }
|
||||||
{
|
|
||||||
id: { type: Schema.Types.ObjectId, ref: 'Group' },
|
|
||||||
setting: { type: String }
|
|
||||||
}
|
|
||||||
],
|
|
||||||
clients: [
|
|
||||||
{
|
|
||||||
id: { type: Schema.Types.ObjectId, ref: 'Client' },
|
|
||||||
setting: { type: String }
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
|
|
||||||
permissionSchema.plugin(AutoIncrement, { inc_field: 'permissionId' })
|
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<
|
export const Permission: IPermissionModel = model<
|
||||||
IPermission,
|
IPermission,
|
||||||
IPermissionModel
|
IPermissionModel
|
||||||
|
|||||||
Reference in New Issue
Block a user