Skip to content

Commit 588cc8d

Browse files
committed
Validate role field on user create
1 parent 05845b4 commit 588cc8d

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/controller/org.controller/org.controller.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ async function updateOrg (req, res, next) {
452452
/**
453453
* Creates a user only if the org exists and
454454
* the user does not exist for the specified shortname and username
455-
* Called by POST /api/org/{shortname}/user
455+
* Called by POST /api/registry/org/{shortname}/user, POST /api/org/{shortname}/user
456456
**/
457457
async function createUser (req, res, next) {
458458
const session = await mongoose.startSession()
@@ -461,6 +461,7 @@ async function createUser (req, res, next) {
461461
const userRepo = req.ctx.repositories.getBaseUserRepository()
462462
const orgRepo = req.ctx.repositories.getBaseOrgRepository()
463463
const orgShortName = req.ctx.params.shortname
464+
const constants = getConstants()
464465
let returnValue
465466

466467
// Check to make sure Org Exists first
@@ -486,6 +487,9 @@ async function createUser (req, res, next) {
486487
if (body?.role && typeof body?.role !== 'string') {
487488
return res.status(400).json({ message: 'Parameters were invalid', details: [{ param: 'role', msg: 'Parameter must be a string' }] })
488489
}
490+
if (body?.role && !constants.USER_ROLES.includes(body?.role)) {
491+
return res.status(400).json({ message: 'Parameters were invalid', details: [{ param: 'role', msg: `Role must be one of the following: ${constants.USER_ROLES}` }] })
492+
}
489493
if (!result.isValid) {
490494
logger.error(JSON.stringify({ uuid: req.ctx.uuid, message: 'User JSON schema validation FAILED.' }))
491495
await session.abortTransaction()
@@ -548,7 +552,7 @@ async function createUser (req, res, next) {
548552
/**
549553
* Updates a user only if the user exist for the specified username.
550554
* If no user exists, it does not create the user.
551-
* Called by PUT /org/{shortname}/user/{username}
555+
* Called by PUT /org/{shortname}/user/{username}, PUT /org/{shortname}/user/{username}
552556
**/
553557
async function updateUser (req, res, next) {
554558
const session = await mongoose.startSession()

src/repositories/baseUserRepository.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ class BaseUserRepository extends BaseRepository {
241241
delete rawRegistryUserJson._id
242242
delete rawRegistryUserJson.__v
243243
delete rawRegistryUserJson.authority
244+
delete rawRegistryUserJson.role
244245
return deepRemoveEmpty(rawRegistryUserJson)
245246
}
246247

0 commit comments

Comments
 (0)