1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-15 01:40:05 +00:00

chore: add principal type and permission setting enums

This commit is contained in:
2022-06-28 00:00:04 +05:00
parent 0a73a35547
commit ca64c13909
3 changed files with 28 additions and 13 deletions

View File

@@ -17,6 +17,16 @@ import Group from '../model/Group'
import { UserResponse } from './user' import { UserResponse } from './user'
import { GroupResponse } from './group' import { GroupResponse } from './group'
export enum PrincipalType {
user = 'user',
group = 'group'
}
export enum PermissionSetting {
grant = 'Grant',
deny = 'Deny'
}
interface RegisterPermissionPayload { interface RegisterPermissionPayload {
/** /**
* Name of affected resource * Name of affected resource
@@ -27,12 +37,12 @@ interface RegisterPermissionPayload {
* The indication of whether (and to what extent) access is provided * The indication of whether (and to what extent) access is provided
* @example "Grant" * @example "Grant"
*/ */
setting: string setting: PermissionSetting
/** /**
* Indicates the type of principal * Indicates the type of principal
* @example "user" * @example "user"
*/ */
principalType: string principalType: PrincipalType
/** /**
* The id of user or group to which a rule is assigned. * The id of user or group to which a rule is assigned.
* @example 123 * @example 123
@@ -45,7 +55,7 @@ interface UpdatePermissionPayload {
* The indication of whether (and to what extent) access is provided * The indication of whether (and to what extent) access is provided
* @example "Grant" * @example "Grant"
*/ */
setting: string setting: PermissionSetting
} }
export interface PermissionDetailsResponse { export interface PermissionDetailsResponse {
@@ -178,7 +188,7 @@ const createPermission = async ({
let group: GroupResponse | undefined let group: GroupResponse | undefined
switch (principalType) { switch (principalType) {
case 'user': { case PrincipalType.user: {
const userInDB = await User.findOne({ id: principalId }) const userInDB = await User.findOne({ id: principalId })
if (!userInDB) if (!userInDB)
throw { throw {
@@ -216,7 +226,7 @@ const createPermission = async ({
} }
break break
} }
case 'group': { case PrincipalType.group: {
const groupInDB = await Group.findOne({ groupId: principalId }) const groupInDB = await Group.findOne({ groupId: principalId })
if (!groupInDB) if (!groupInDB)
throw { throw {

View File

@@ -7,7 +7,9 @@ import {
UserController, UserController,
GroupController, GroupController,
ClientController, ClientController,
PermissionController PermissionController,
PrincipalType,
PermissionSetting
} from '../../../controllers/' } from '../../../controllers/'
import { import {
UserDetailsResponse, UserDetailsResponse,
@@ -33,8 +35,8 @@ const user = {
const permission = { const permission = {
uri: '/SASjsApi/code/execute', uri: '/SASjsApi/code/execute',
setting: 'Grant', setting: PermissionSetting.grant,
principalType: 'user', principalType: PrincipalType.user,
principalId: 123 principalId: 123
} }
@@ -228,9 +230,7 @@ describe('permission', () => {
}) })
.expect(400) .expect(400)
expect(res.text).toEqual( expect(res.text).toEqual('"principalType" must be one of [user, group]')
'Invalid principal type. Valid types are user or group.'
)
expect(res.body).toEqual({}) expect(res.body).toEqual({})
}) })
}) })

View File

@@ -1,5 +1,6 @@
import Joi from 'joi' import Joi from 'joi'
import { RunTimeType } from '.' import { RunTimeType } from '.'
import { PermissionSetting, PrincipalType } from '../controllers'
const usernameSchema = Joi.string().lowercase().alphanum().min(3).max(16) const usernameSchema = Joi.string().lowercase().alphanum().min(3).max(16)
const passwordSchema = Joi.string().min(6).max(1024) const passwordSchema = Joi.string().min(6).max(1024)
@@ -89,8 +90,12 @@ export const registerClientValidation = (data: any): Joi.ValidationResult =>
export const registerPermissionValidation = (data: any): Joi.ValidationResult => export const registerPermissionValidation = (data: any): Joi.ValidationResult =>
Joi.object({ Joi.object({
uri: Joi.string().required(), uri: Joi.string().required(),
setting: Joi.string().required(), setting: Joi.string()
principalType: Joi.string().required(), .required()
.valid(...Object.values(PermissionSetting)),
principalType: Joi.string()
.required()
.valid(...Object.values(PrincipalType)),
principalId: Joi.number().required() principalId: Joi.number().required()
}).validate(data) }).validate(data)