|
| 1 | +const argon2 = require('argon2') |
1 | 2 | const uuid = require('uuid') |
2 | 3 | const logger = require('../../middleware/logger') |
3 | 4 | const { getConstants } = require('../../constants') |
4 | 5 | const RegistryOrg = require('../../model/registry-org') |
5 | 6 | const RegistryUser = require('../../model/registry-user') |
6 | 7 | const errors = require('./error') |
| 8 | +const cryptoRandomString = require('crypto-random-string') |
7 | 9 | const error = new errors.RegistryOrgControllerError() |
8 | 10 | const validateUUID = require('uuid').validate |
9 | 11 |
|
@@ -98,7 +100,7 @@ async function createOrg (req, res, next) { |
98 | 100 | } else if (k === 'short_name') { |
99 | 101 | newOrg.short_name = body[k] |
100 | 102 | } else if (k === 'aliases') { |
101 | | - newOrg.aliases = [...new Set(body[k].active_roles)] |
| 103 | + newOrg.aliases = [...new Set(body[k])] |
102 | 104 | } else if (k === 'cve_program_org_function') { |
103 | 105 | newOrg.cve_program_org_function = body[k] |
104 | 106 | } else if (k === 'authority') { |
@@ -197,7 +199,6 @@ async function updateOrg (req, res, next) { |
197 | 199 | const shortName = req.ctx.params.shortname |
198 | 200 | const userRepo = req.ctx.repositories.getRegistryUserRepository() |
199 | 201 | const registryOrgRepo = req.ctx.repositories.getRegistryOrgRepository() |
200 | | - // const shortName = req.ctx.params.shortname |
201 | 202 |
|
202 | 203 | const org = await registryOrgRepo.findOneByShortName(shortName) |
203 | 204 | if (!org) { |
@@ -375,8 +376,86 @@ async function getUsers (req, res, next) { |
375 | 376 | } |
376 | 377 | } |
377 | 378 |
|
378 | | -function createUserByOrg (req, res, next) { |
379 | | - console.log('HERE') |
| 379 | +async function createUserByOrg (req, res, next) { |
| 380 | + try { |
| 381 | + const requesterUsername = req.ctx.user |
| 382 | + const requesterShortName = req.ctx.org |
| 383 | + const shortName = req.ctx.params.shortname |
| 384 | + |
| 385 | + const registryUserRepo = req.ctx.repositories.getRegistryUserRepository() |
| 386 | + const registryOrgRepo = req.ctx.repositories.getRegistryOrgRepository() |
| 387 | + const orgUUID = await registryOrgRepo.getOrgUUID(shortName) |
| 388 | + const requesterOrgUUID = await registryOrgRepo.getOrgUUID(requesterShortName) |
| 389 | + const body = req.ctx.body |
| 390 | + |
| 391 | + const isSecretariat = await registryOrgRepo.isSecretariat(shortName) |
| 392 | + const isAdmin = await registryUserRepo.isAdmin(requesterUsername, requesterShortName) |
| 393 | + |
| 394 | + if (!isSecretariat && !isAdmin) { // may be redundant after validation check is implemented |
| 395 | + return res.status(403).json(error.notOrgAdminOrSecretariat()) // User must be secretariat or an admin |
| 396 | + } |
| 397 | + if (!orgUUID) { |
| 398 | + return res.status(404).json(error.orgDnePathParam(shortName)) // Org must exist |
| 399 | + } |
| 400 | + if (!isSecretariat) { // Admins can only create user within the same org |
| 401 | + if (orgUUID !== requesterOrgUUID) { |
| 402 | + return res.status(403).json(error.notOrgAdminOrSecretariat()) // The Admin user must belong to the new user's organization |
| 403 | + } |
| 404 | + } |
| 405 | + |
| 406 | + // Creating a new user under specific org |
| 407 | + const newUser = new RegistryUser() |
| 408 | + Object.keys(body).map(k => k.toLowerCase()).forEach(k => { |
| 409 | + if (k === 'user_id' || k === 'username') { |
| 410 | + newUser.user_id = body[k] |
| 411 | + } else if (k === 'name') { |
| 412 | + newUser.name = { |
| 413 | + first: '', |
| 414 | + last: '', |
| 415 | + middle: '', |
| 416 | + suffix: '', |
| 417 | + ...body.name |
| 418 | + } |
| 419 | + } else if (k === 'org_affiliations') { |
| 420 | + // TODO: dedupe |
| 421 | + } else if (k === 'cve_program_org_membership') { |
| 422 | + // TODO: dedupe |
| 423 | + } else if (k === 'uuid') { |
| 424 | + return res.status(400).json(error.uuidProvided('user')) |
| 425 | + } |
| 426 | + }) |
| 427 | + |
| 428 | + newUser.UUID = uuid.v4() |
| 429 | + const randomKey = cryptoRandomString({ length: getConstants().CRYPTO_RANDOM_STRING_LENGTH }) |
| 430 | + newUser.secret = await argon2.hash(randomKey) |
| 431 | + newUser.last_active = null |
| 432 | + newUser.deactivation_date = null |
| 433 | + |
| 434 | + await registryUserRepo.updateByUUID(newUser.UUID, newUser, { upsert: true }) |
| 435 | + const agt = setAggregateUserObj({ UUID: newUser.UUID }) |
| 436 | + let result = await registryUserRepo.aggregate(agt) |
| 437 | + result = result.length > 0 ? result[0] : null |
| 438 | + |
| 439 | + const payload = { |
| 440 | + action: 'create_registry_user', |
| 441 | + change: result.user_id + ' was successfully created.', |
| 442 | + req_UUID: req.ctx.uuid, |
| 443 | + org_UUID: await registryOrgRepo.getOrgUUID(req.ctx.org), |
| 444 | + user: result |
| 445 | + } |
| 446 | + payload.user_UUID = await registryUserRepo.getUserUUID(req.ctx.user, payload.org_UUID) |
| 447 | + logger.info(JSON.stringify(payload)) |
| 448 | + |
| 449 | + result.secret = randomKey |
| 450 | + const responseMessage = { |
| 451 | + message: result.user_id + ' was successfully created.', |
| 452 | + created: result |
| 453 | + } |
| 454 | + |
| 455 | + return res.status(200).json(responseMessage) |
| 456 | + } catch (err) { |
| 457 | + next(err) |
| 458 | + } |
380 | 459 | } |
381 | 460 |
|
382 | 461 | function setAggregateUserObj (query) { |
|
0 commit comments