@@ -388,7 +388,7 @@ async function createUserByOrg (req, res, next) {
388388 const requesterOrgUUID = await registryOrgRepo . getOrgUUID ( requesterShortName )
389389 const body = req . ctx . body
390390
391- const isSecretariat = await registryOrgRepo . isSecretariat ( shortName )
391+ const isSecretariat = await registryOrgRepo . isSecretariat ( requesterShortName )
392392 const isAdmin = await registryUserRepo . isAdmin ( requesterUsername , requesterShortName )
393393
394394 if ( ! isSecretariat && ! isAdmin ) { // may be redundant after validation check is implemented
@@ -403,6 +403,15 @@ async function createUserByOrg (req, res, next) {
403403 }
404404 }
405405
406+ const username = body . user_id || body . username
407+ if ( ! username ) {
408+ return res . status ( 400 ) . json ( { message : 'user_id is required' } )
409+ }
410+ const existingUser = await registryUserRepo . findOneByUserNameAndOrgUUID ( username , orgUUID )
411+ if ( existingUser ) {
412+ return res . status ( 400 ) . json ( error . userExists ( username ) )
413+ }
414+
406415 // Creating a new user under specific org
407416 const newUser = new RegistryUser ( )
408417 Object . keys ( body ) . map ( k => k . toLowerCase ( ) ) . forEach ( k => {
@@ -417,21 +426,54 @@ async function createUserByOrg (req, res, next) {
417426 ...body . name
418427 }
419428 } else if ( k === 'org_affiliations' ) {
420- // TODO: dedupe
429+ newUser . org_affiliations = body [ k ] . map ( item => {
430+ const {
431+ orgId = '' ,
432+ email = '' ,
433+ phone = '' ,
434+ ...rest
435+ } = item
436+
437+ return {
438+ org_id : orgId ,
439+ email,
440+ phone,
441+ ...rest
442+ }
443+ } )
421444 } else if ( k === 'cve_program_org_membership' ) {
422- // TODO: dedupe
445+ newUser . cve_program_org_membership = body [ k ] . map ( item => {
446+ const {
447+ programOrg = '' ,
448+ roles = [ ] ,
449+
450+ status = false ,
451+ ...rest
452+ } = item
453+
454+ return {
455+ program_org : programOrg ,
456+ roles,
457+ status,
458+ ...rest
459+ }
460+ } )
423461 } else if ( k === 'uuid' ) {
424462 return res . status ( 400 ) . json ( error . uuidProvided ( 'user' ) )
425463 }
426464 } )
427465
428466 newUser . UUID = uuid . v4 ( )
467+
429468 const randomKey = cryptoRandomString ( { length : getConstants ( ) . CRYPTO_RANDOM_STRING_LENGTH } )
430469 newUser . secret = await argon2 . hash ( randomKey )
431470 newUser . last_active = null
432471 newUser . deactivation_date = null
433472
434- await registryUserRepo . updateByUUID ( newUser . UUID , newUser , { upsert : true } )
473+ await registryUserRepo . updateByUserNameAndOrgUUID ( newUser . user_id , orgUUID , newUser , { upsert : true } )
474+ await registryUserRepo . addOrgToUserAffiliation ( newUser . UUID , orgUUID )
475+ await registryOrgRepo . addUserToOrgList ( orgUUID , newUser . UUID , body . authority ?. active_roles ? [ ...new Set ( body . authority . active_roles ) ] . includes ( 'ADMIN' ) : false , { upsert : true } )
476+
435477 const agt = setAggregateUserObj ( { UUID : newUser . UUID } )
436478 let result = await registryUserRepo . aggregate ( agt )
437479 result = result . length > 0 ? result [ 0 ] : null
@@ -440,10 +482,10 @@ async function createUserByOrg (req, res, next) {
440482 action : 'create_registry_user' ,
441483 change : result . user_id + ' was successfully created.' ,
442484 req_UUID : req . ctx . uuid ,
443- org_UUID : await registryOrgRepo . getOrgUUID ( req . ctx . org ) ,
485+ org_UUID : orgUUID ,
444486 user : result
445487 }
446- payload . user_UUID = await registryUserRepo . getUserUUID ( req . ctx . user , payload . org_UUID )
488+ payload . user_UUID = await registryUserRepo . getUserUUID ( req . ctx . user , orgUUID )
447489 logger . info ( JSON . stringify ( payload ) )
448490
449491 result . secret = randomKey
0 commit comments