Skip to content

Commit 57e9801

Browse files
authored
Merge pull request #1602 from CVEProject/dr_cleanup_jan
Various cleanup items
2 parents 6cc3dbb + f9f92c9 commit 57e9801

7 files changed

Lines changed: 451 additions & 57 deletions

File tree

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async function getUsers (req, res, next) {
122122
return res.status(403).json(error.notSameOrgOrSecretariat())
123123
}
124124

125-
const payload = await userRepo.getAllUsersByOrgShortname(orgShortName, options, !req.useRegistry)
125+
const payload = await userRepo.getAllUsersByOrgShortname(orgShortName, options, !!req.useRegistry)
126126

127127
logger.info({ uuid: req.ctx.uuid, message: `The users of ${orgShortName} organization were sent to the user.` })
128128
return res.status(200).json(payload)
@@ -157,7 +157,7 @@ async function getUser (req, res, next) {
157157

158158
const userRepo = req.ctx.repositories.getBaseUserRepository()
159159
// This is simple, we can just call our function
160-
const result = await userRepo.findOneByUsernameAndOrgShortname(username, orgShortName, {}, !req.useRegistry)
160+
const result = await userRepo.findOneByUsernameAndOrgShortname(username, orgShortName, {}, !!req.useRegistry)
161161

162162
if (!result) {
163163
logger.info({ uuid: req.ctx.uuid, message: username + ' does not exist.' })
@@ -249,7 +249,6 @@ async function createOrg (req, res, next) {
249249
}
250250

251251
// Check to see if the org already exits
252-
// Org exists funciton checks if we should "return the legacy format" NOT "IS IT" a legacy format. TODO: Fix that.
253252
if (await repo.orgExists(body?.short_name, { session }, !req.useRegistry)) {
254253
logger.info({ uuid: req.ctx.uuid, message: body?.short_name + ' organization was not created because it already exists.' })
255254
await session.abortTransaction()
@@ -413,13 +412,13 @@ async function createUser (req, res, next) {
413412
}
414413

415414
// Ask repo if user already exists
416-
if (await userRepo.orgHasUser(orgShortName, body?.username, { session }, !req.useRegistry)) {
415+
if (await userRepo.orgHasUser(orgShortName, body?.username, { session }, !!req.useRegistry)) {
417416
logger.info({ uuid: req.ctx.uuid, message: `${body?.username} user was not created because it already exists.` })
418417
await session.abortTransaction()
419418
return res.status(400).json(error.userExists(body?.username))
420419
}
421420

422-
if (!await userRepo.isAdminOrSecretariat(orgShortName, req.ctx.user, req.ctx.org, { session }, !req.useRegistry)) {
421+
if (!await userRepo.isAdminOrSecretariat(orgShortName, req.ctx.user, req.ctx.org, { session }, !!req.useRegistry)) {
423422
await session.abortTransaction()
424423
return res.status(403).json(error.notOrgAdminOrSecretariat()) // The Admin user must belong to the new user's organization
425424
}
@@ -430,7 +429,7 @@ async function createUser (req, res, next) {
430429
return res.status(400).json(error.userLimitReached())
431430
}
432431

433-
returnValue = await userRepo.createUser(orgShortName, body, { session, upsert: true }, !req.useRegistry)
432+
returnValue = await userRepo.createUser(orgShortName, body, { session, upsert: true }, !!req.useRegistry)
434433
await session.commitTransaction()
435434
} catch (error) {
436435
await session.abortTransaction()
@@ -482,8 +481,8 @@ async function updateUser (req, res, next) {
482481
const queryParametersJson = req.ctx.query
483482

484483
// Get requester UUID for later
485-
const requesterUUID = await userRepo.getUserUUID(requesterUsername, requesterShortName, { session }, !req.useRegistry)
486-
const targetUserUUID = await userRepo.getUserUUID(usernameParams, shortNameParams, { session }, !req.useRegistry)
484+
const requesterUUID = await userRepo.getUserUUID(requesterUsername, requesterShortName, { session }, !!req.useRegistry)
485+
const targetUserUUID = await userRepo.getUserUUID(usernameParams, shortNameParams, { session }, !!req.useRegistry)
487486

488487
const isRequesterSecretariat = await orgRepo.isSecretariatByShortName(requesterShortName, { session })
489488
const isAdmin = await userRepo.isAdmin(requesterUsername, requesterShortName, { session })
@@ -600,7 +599,7 @@ async function updateUser (req, res, next) {
600599
}
601600
}
602601

603-
const payload = await userRepo.updateUser(usernameParams, shortNameParams, queryParametersJson, { session }, !req.useRegistry)
602+
const payload = await userRepo.updateUser(usernameParams, shortNameParams, queryParametersJson, { session }, !!req.useRegistry)
604603
await session.commitTransaction()
605604
return res.status(200).json({ message: `${usernameParams} was successfully updated.`, updated: payload })
606605
} catch (err) {
@@ -646,14 +645,14 @@ async function resetSecret (req, res, next) {
646645
}
647646

648647
// Check if target user exists in target org
649-
const targetUserUUID = await userRepo.getUserUUID(targetUsername, targetOrgShortName, { session }, !req.useRegistry)
648+
const targetUserUUID = await userRepo.getUserUUID(targetUsername, targetOrgShortName, { session }, !!req.useRegistry)
650649
if (!targetUserUUID) {
651650
logger.info({ uuid: req.ctx.uuid, message: 'User DNE' })
652651
await session.abortTransaction()
653652
return res.status(404).json(error.userDne(targetUsername))
654653
}
655654

656-
const requesterUserUUID = await userRepo.getUserUUID(requesterUsername, requesterOrgShortName, { session }, !req.useRegistry)
655+
const requesterUserUUID = await userRepo.getUserUUID(requesterUsername, requesterOrgShortName, { session }, !!req.useRegistry)
657656

658657
const isRequesterSecretariat = await orgRepo.isSecretariatByShortName(requesterOrgShortName, { session })
659658

@@ -679,7 +678,7 @@ async function resetSecret (req, res, next) {
679678
}
680679
}
681680

682-
const updatedSecret = await userRepo.resetSecret(targetUsername, targetOrgShortName, { session }, !req.useRegistry)
681+
const updatedSecret = await userRepo.resetSecret(targetUsername, targetOrgShortName, { session }, !!req.useRegistry)
683682

684683
logger.info({ uuid: req.ctx.uuid, message: `The API secret was successfully reset and sent to ${targetUsername}` })
685684
const payload = {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,10 @@ function validateUpdateOrgParameters () {
200200
query(['new_short_name']).optional().isString().trim().notEmpty().isLength({ min: CONSTANTS.MIN_SHORTNAME_LENGTH, max: CONSTANTS.MAX_SHORTNAME_LENGTH }),
201201
query(['active_roles.add']).optional().toArray()
202202
.custom(isFlatStringArray)
203-
.customSanitizer(toUpperCaseArray)
204-
.custom(isOrgRole).withMessage(errorMsgs.ORG_ROLES),
203+
.customSanitizer(toUpperCaseArray),
205204
query(['active_roles.remove']).optional().toArray()
206205
.custom(isFlatStringArray)
207-
.customSanitizer(toUpperCaseArray)
208-
.custom(isOrgRole).withMessage(errorMsgs.ORG_ROLES),
206+
.customSanitizer(toUpperCaseArray),
209207
// Path parameter validation
210208
param(['shortname']).isString().trim().isLength({ min: CONSTANTS.MIN_SHORTNAME_LENGTH, max: CONSTANTS.MAX_SHORTNAME_LENGTH })]
211209
if (useRegistry) {

src/controller/registry-org.controller/registry-org.controller.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ async function updateOrg (req, res, next) {
234234
const requestingUser = await userRepo.findOneByUsernameAndOrgShortname(req.ctx.user, req.ctx.org, { session })
235235
const org = await repo.findOneByShortName(shortName)
236236

237+
if (!isSecretariat && (!isAdmin || shortName !== req.ctx.org)) {
238+
logger.info({ uuid: req.ctx.uuid, message: shortName + ' organization can only be updated by the users of the same organization or the Secretariat.' })
239+
await session.abortTransaction()
240+
return res.status(403).json(error.notSameOrgOrSecretariat())
241+
}
242+
237243
// Edge Case: if a user has requested an org, but it is not approved yet, then we need to check to see if if there is a review org for the shortname request.
238244

239245
if (!org) {
@@ -421,7 +427,7 @@ async function getUsers (req, res, next) {
421427
return res.status(403).json(error.notSameOrgOrSecretariat())
422428
}
423429

424-
const payload = await userRepo.getAllUsersByOrgShortname(orgShortName, options, true)
430+
const payload = await userRepo.getAllUsersByOrgShortname(orgShortName, options, false)
425431

426432
logger.info({ uuid: req.ctx.uuid, message: `The users of ${orgShortName} organization were sent to the user.` })
427433
return res.status(200).json(payload)
@@ -482,13 +488,13 @@ async function createUserByOrg (req, res, next) {
482488
}
483489

484490
// Ask repo if user already exists
485-
if (await userRepo.orgHasUser(orgShortName, body?.username, { session }, false)) {
491+
if (await userRepo.orgHasUser(orgShortName, body?.username, { session }, true)) {
486492
logger.info({ uuid: req.ctx.uuid, message: `${body?.username} user was not created because it already exists.` })
487493
await session.abortTransaction()
488494
return res.status(400).json(error.userExists(body?.username))
489495
}
490496

491-
if (!await userRepo.isAdminOrSecretariat(orgShortName, req.ctx.user, req.ctx.org, { session }, false)) {
497+
if (!await userRepo.isAdminOrSecretariat(orgShortName, req.ctx.user, req.ctx.org, { session }, true)) {
492498
await session.abortTransaction()
493499
return res.status(403).json(error.notOrgAdminOrSecretariat()) // The Admin user must belong to the new user's organization
494500
}
@@ -499,7 +505,7 @@ async function createUserByOrg (req, res, next) {
499505
return res.status(400).json(error.userLimitReached())
500506
}
501507

502-
returnValue = await userRepo.createUser(orgShortName, body, { session, upsert: true }, false)
508+
returnValue = await userRepo.createUser(orgShortName, body, { session, upsert: true }, true)
503509
await session.commitTransaction()
504510
} catch (error) {
505511
await session.abortTransaction()

src/controller/user.controller/user.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ async function getAllUsers (req, res, next) {
2626
options.page = req.ctx.query.page ? parseInt(req.ctx.query.page) : CONSTANTS.PAGINATOR_PAGE // if 'page' query parameter is not defined, set 'page' to the default page value
2727

2828
try {
29-
returnValue = await repo.getAllUsers(options, !req.useRegistry)
29+
returnValue = await repo.getAllUsers(options, !!req.useRegistry)
3030
} finally {
3131
await session.endSession()
3232
}

0 commit comments

Comments
 (0)