@@ -170,10 +170,12 @@ async function getUsers (req, res, next) {
170170 **/
171171async function getUser ( req , res , next ) {
172172 try {
173+ const isRegistry = req . query . registry === 'true'
173174 const shortName = req . ctx . org
174175 const username = req . ctx . params . username
175176 const orgShortName = req . ctx . params . shortname
176- const orgRepo = req . ctx . repositories . getOrgRepository ( )
177+
178+ const orgRepo = isRegistry ? req . ctx . repositories . getRegistryOrgRepository ( ) : req . ctx . repositories . getOrgRepository ( )
177179 const isSecretariat = await orgRepo . isSecretariat ( shortName )
178180
179181 if ( orgShortName !== shortName && ! isSecretariat ) {
@@ -187,8 +189,8 @@ async function getUser (req, res, next) {
187189 return res . status ( 404 ) . json ( error . orgDnePathParam ( orgShortName ) )
188190 }
189191
190- const userRepo = req . ctx . repositories . getUserRepository ( )
191- const agt = setAggregateUserObj ( { username : username , org_UUID : orgUUID } )
192+ const userRepo = isRegistry ? req . ctx . repositories . getRegistryUserRepository ( ) : req . ctx . repositories . getUserRepository ( )
193+ const agt = isRegistry ? setAggregateRegistryUserObj ( { user_id : username , 'cve_program_org_membership.program_org' : orgUUID } ) : setAggregateUserObj ( { username : username , org_UUID : orgUUID } )
192194 let result = await userRepo . aggregate ( agt )
193195 result = result . length > 0 ? result [ 0 ] : null
194196
@@ -1016,47 +1018,84 @@ async function updateUser (req, res, next) {
10161018
10171019// Called by PUT /org/{shortname}/user/{username}/reset_secret
10181020async function resetSecret ( req , res , next ) {
1021+ const session = await mongoose . startSession ( )
1022+ session . startTransaction ( )
10191023 try {
1024+ let randomKey
1025+
10201026 const requesterShortName = req . ctx . org
10211027 const requesterUsername = req . ctx . user
10221028 const username = req . ctx . params . username
10231029 const orgShortName = req . ctx . params . shortname
1030+
10241031 const userRepo = req . ctx . repositories . getUserRepository ( )
10251032 const orgRepo = req . ctx . repositories . getOrgRepository ( )
1026- const isSecretariat = await orgRepo . isSecretariat ( requesterShortName )
1027- const orgUUID = await orgRepo . getOrgUUID ( orgShortName ) // userUUID may be null if user does not exist
1028- if ( ! orgUUID ) {
1029- logger . info ( { uuid : req . ctx . uuid , messsage : orgShortName + ' organization does not exist.' } )
1030- return res . status ( 404 ) . json ( error . orgDnePathParam ( orgShortName ) )
1031- }
10321033
1033- if ( orgShortName !== requesterShortName && ! isSecretariat ) {
1034- logger . info ( { uuid : req . ctx . uuid , message : orgShortName + ' organization can only be viewed by the users of the same organization or the Secretariat.' } )
1035- return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
1036- }
1034+ const userRegistryRepo = req . ctx . repositories . getRegistryUserRepository ( )
1035+ const orgRegistryRepo = req . ctx . repositories . getRegistryOrgRepository ( )
10371036
1038- const oldUser = await userRepo . findOneByUserNameAndOrgUUID ( username , orgUUID )
1039- if ( ! oldUser ) {
1040- logger . info ( { uuid : req . ctx . uuid , messsage : username + ' user does not exist.' } )
1041- return res . status ( 404 ) . json ( error . userDne ( username ) )
1042- }
1037+ try {
1038+ const isSecretariatLeg = await orgRepo . isSecretariat ( requesterShortName , { session } )
1039+ const isSecretariatReg = await orgRegistryRepo . isSecretariat ( requesterShortName , { session } )
1040+ const isSecretariat = isSecretariatLeg && isSecretariatReg
10431041
1044- const isAdmin = await userRepo . isAdmin ( requesterUsername , requesterShortName )
1045- // check if the user is not the requester or if the requester is not a secretariat
1046- if ( ( orgShortName !== requesterShortName || username !== requesterUsername ) && ! isSecretariat ) {
1042+ const orgUUID = await orgRepo . getOrgUUID ( orgShortName , { session } ) // userUUID may be null if user does not exist
1043+ const orgRegUUID = await orgRegistryRepo . getOrgUUID ( orgShortName , { session } )
1044+
1045+ // check if orgUUID and orgRegUUID are the same
1046+ if ( orgUUID . toString ( ) !== orgRegUUID . toString ( ) ) {
1047+ logger . info ( { uuid : req . ctx . uuid , message : 'The organization UUID and the organization registry UUID are not the same.' } )
1048+ return res . status ( 500 ) . json ( error . internalServerError ( ) )
1049+ }
1050+
1051+ if ( ! orgUUID && ! orgRegUUID ) {
1052+ logger . info ( { uuid : req . ctx . uuid , messsage : orgShortName + ' organization does not exist.' } )
1053+ return res . status ( 404 ) . json ( error . orgDnePathParam ( orgShortName ) )
1054+ }
1055+
1056+ if ( orgShortName !== requesterShortName && ! isSecretariat ) {
1057+ logger . info ( { uuid : req . ctx . uuid , message : orgShortName + ' organization can only be viewed by the users of the same organization or the Secretariat.' } )
1058+ return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
1059+ }
1060+
1061+ const oldUser = await userRepo . findOneByUserNameAndOrgUUID ( username , orgUUID , null , { session } )
1062+ const oldUserRegistry = await userRegistryRepo . findOneByUserNameAndOrgUUID ( username , orgRegUUID )
1063+
1064+ if ( ! oldUser && ! oldUserRegistry ) {
1065+ logger . info ( { uuid : req . ctx . uuid , messsage : username + ' user does not exist.' } )
1066+ return res . status ( 404 ) . json ( error . userDne ( username ) )
1067+ }
1068+
1069+ const isLegAdmin = await userRepo . isAdmin ( requesterUsername , requesterShortName , { session } )
1070+ const isRegAdmin = await userRegistryRepo . isAdmin ( requesterUsername , orgRegUUID , { session } )
1071+ const isAdmin = isLegAdmin && isRegAdmin
1072+
1073+ // check if the user is not the requester or if the requester is not a secretariat
1074+ if ( ( orgShortName !== requesterShortName || username !== requesterUsername ) && ! isSecretariat ) {
10471075 // check if the requester is not and admin; if admin, the requester must be from the same org as the user
1048- if ( ! isAdmin || ( isAdmin && orgShortName !== requesterShortName ) ) {
1049- logger . info ( { uuid : req . ctx . uuid , message : 'The api secret can only be reset by the Secretariat, an Org admin or if the requester is the user.' } )
1050- return res . status ( 403 ) . json ( error . notSameUserOrSecretariat ( ) )
1076+ if ( ! isAdmin || ( isAdmin && orgShortName !== requesterShortName ) ) {
1077+ logger . info ( { uuid : req . ctx . uuid , message : 'The api secret can only be reset by the Secretariat, an Org admin or if the requester is the user.' } )
1078+ return res . status ( 403 ) . json ( error . notSameUserOrSecretariat ( ) )
1079+ }
10511080 }
1052- }
10531081
1054- const randomKey = cryptoRandomString ( { length : getConstants ( ) . CRYPTO_RANDOM_STRING_LENGTH } )
1055- oldUser . secret = await argon2 . hash ( randomKey ) // store in db
1056- const user = await userRepo . updateByUserNameAndOrgUUID ( oldUser . username , orgUUID , oldUser )
1057- if ( user . n === 0 ) {
1058- logger . info ( { uuid : req . ctx . uuid , message : 'The user could not be updated because ' + username + ' does not exist for ' + orgShortName + ' organization.' } )
1059- return res . status ( 404 ) . json ( error . userDne ( username ) )
1082+ randomKey = cryptoRandomString ( { length : getConstants ( ) . CRYPTO_RANDOM_STRING_LENGTH } )
1083+ oldUser . secret = await argon2 . hash ( randomKey ) // store in db
1084+ oldUserRegistry . secret = await argon2 . hash ( randomKey ) // store in db
1085+
1086+ const user = await userRepo . updateByUserNameAndOrgUUID ( oldUser . username , orgUUID , oldUser , { session } )
1087+ const userReg = await userRegistryRepo . updateByUserNameAndOrgUUID ( oldUserRegistry . user_id , orgRegUUID , oldUserRegistry , { session } )
1088+
1089+ if ( user . matchedCount === 0 || userReg . matchedCount === 0 ) {
1090+ logger . info ( { uuid : req . ctx . uuid , message : 'The user could not be updated because ' + username + ' does not exist for ' + orgShortName + ' organization.' } )
1091+ return res . status ( 404 ) . json ( error . userDne ( username ) )
1092+ }
1093+ await session . commitTransaction ( )
1094+ } catch ( error ) {
1095+ await session . abortTransaction ( )
1096+ throw error
1097+ } finally {
1098+ session . endSession ( )
10601099 }
10611100
10621101 logger . info ( { uuid : req . ctx . uuid , message : `The API secret was successfully reset and sent to ${ username } ` } )
0 commit comments