mirror of
https://github.com/sasjs/server.git
synced 2026-01-10 07:50:05 +00:00
chore: throw error when creating duplicate permission
This commit is contained in:
@@ -163,10 +163,17 @@ const createPermission = async ({
|
|||||||
let group: GroupResponse | undefined
|
let group: GroupResponse | undefined
|
||||||
|
|
||||||
switch (principalType) {
|
switch (principalType) {
|
||||||
case 'user':
|
case 'user': {
|
||||||
const userInDB = await User.findOne({ id: principalId })
|
const userInDB = await User.findOne({ id: principalId })
|
||||||
if (!userInDB) throw new Error('User not found.')
|
if (!userInDB) throw new Error('User not found.')
|
||||||
|
|
||||||
|
const alreadyExists = await Permission.findOne({
|
||||||
|
uri,
|
||||||
|
user: userInDB._id
|
||||||
|
})
|
||||||
|
if (alreadyExists)
|
||||||
|
throw new Error('Permission already exists with provided URI and User.')
|
||||||
|
|
||||||
permission.user = userInDB._id
|
permission.user = userInDB._id
|
||||||
|
|
||||||
user = {
|
user = {
|
||||||
@@ -175,10 +182,20 @@ const createPermission = async ({
|
|||||||
displayName: userInDB.displayName
|
displayName: userInDB.displayName
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case 'group':
|
}
|
||||||
|
case 'group': {
|
||||||
const groupInDB = await Group.findOne({ groupId: principalId })
|
const groupInDB = await Group.findOne({ groupId: principalId })
|
||||||
if (!groupInDB) throw new Error('Group not found.')
|
if (!groupInDB) throw new Error('Group not found.')
|
||||||
|
|
||||||
|
const alreadyExists = await Permission.findOne({
|
||||||
|
uri,
|
||||||
|
group: groupInDB._id
|
||||||
|
})
|
||||||
|
if (alreadyExists)
|
||||||
|
throw new Error(
|
||||||
|
'Permission already exists with provided URI and Group.'
|
||||||
|
)
|
||||||
|
|
||||||
permission.group = groupInDB._id
|
permission.group = groupInDB._id
|
||||||
|
|
||||||
group = {
|
group = {
|
||||||
@@ -187,6 +204,7 @@ const createPermission = async ({
|
|||||||
description: groupInDB.description
|
description: groupInDB.description
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
throw new Error('Invalid principal type. Valid types are user or group.')
|
throw new Error('Invalid principal type. Valid types are user or group.')
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user