@@ -218,65 +218,6 @@ async function getOrgIdQuota (req, res, next) {
218218 }
219219}
220220
221- async function registryCreateOrg ( req , res , next ) {
222- try {
223- const session = await mongoose . startSession ( )
224- const repo = req . ctx . repositories . getBaseOrgRepository ( )
225- const body = req . ctx . body
226- let returnValue
227- // Do not allow the user to pass in a UUID
228- if ( body ?. uuid ?? null ) {
229- return res . status ( 400 ) . json ( error . uuidProvided ( 'org' ) )
230- }
231-
232- try {
233- session . startTransaction ( )
234- // Because Discriminators are used to handle the different types, mongoose automatically drops ALL fields that are not defined in EITHER the base or sub schema
235-
236- const result = repo . validateOrg ( req . ctx . body , { session } )
237- if ( ! result . isValid ) {
238- logger . error ( JSON . stringify ( { uuid : req . ctx . uuid , message : 'CVE JSON schema validation FAILED.' } ) )
239- await session . abortTransaction ( )
240- if ( ! Array . isArray ( body ?. authority ) || body ?. authority . some ( item => typeof item !== 'string' ) ) {
241- return res . status ( 400 ) . json ( { error : 'BAD_INPUT' , message : 'Parameters were invalid' , details : [ { param : 'authority' , msg : 'Parameter must be a one-dimensional array of strings' } ] } )
242- }
243- return res . status ( 400 ) . json ( { error : 'BAD_INPUT' , message : 'Parameters were invalid' , errors : result . errors } )
244- }
245-
246- // Check to see if the org already exists
247- if ( await repo . orgExists ( body . short_name , { session } ) ) {
248- logger . info ( { uuid : req . ctx . uuid , message : body . short_name + ' organization was not created because it already exists.' } )
249- await session . abortTransaction ( )
250- return res . status ( 400 ) . json ( error . orgExists ( body . short_name ) )
251- }
252-
253- // If we get here, we know we are good to create
254- const userRepo = req . ctx . repositories . getBaseUserRepository ( )
255- const requestingUserUUID = await userRepo . getUserUUID ( req . ctx . user , req . ctx . org , { session } )
256- const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org , { session } )
257- returnValue = await repo . createOrg ( req . ctx . body , { session, upsert : true } , false , requestingUserUUID , isSecretariat )
258-
259- await session . commitTransaction ( )
260- logger . info ( {
261- action : 'create_org' ,
262- change : returnValue . short_name + ' organization was successfully created.' ,
263- req_UUID : req . ctx . uuid ,
264- org_UUID : returnValue . UUID ,
265- org : returnValue
266- } )
267- } catch ( error ) {
268- await session . abortTransaction ( )
269- throw error
270- } finally {
271- await session . endSession ( )
272- }
273-
274- return res . status ( 200 ) . json ( { message : returnValue . short_name + ' organization was successfully created.' , created : returnValue } )
275- } catch ( err ) {
276- next ( err )
277- }
278- }
279-
280221/**
281222 * Creates a new org only if the org doesn't exist for the specified shortname.
282223 * If the org exists, we do not update the org.
@@ -293,15 +234,32 @@ async function createOrg (req, res, next) {
293234
294235 try {
295236 session . startTransaction ( )
296- if ( await repo . orgExists ( body ?. short_name , { session } , true ) ) {
237+
238+ if ( req . useRegistry ) {
239+ // If we are creating an org via the registry flag, we can do a full validation.
240+ const result = await repo . validateOrg ( body , { session } )
241+ if ( ! result . isValid ) {
242+ logger . error ( JSON . stringify ( { uuid : req . ctx . uuid , message : 'CVE JSON schema validation FAILED.' } ) )
243+ await session . abortTransaction ( )
244+ if ( ! Array . isArray ( body ?. authority ) || body ?. authority . some ( item => typeof item !== 'string' ) ) {
245+ return res . status ( 400 ) . json ( { error : 'BAD_INPUT' , message : 'Parameters were invalid' , details : [ { param : 'authority' , msg : 'Parameter must be a one-dimensional array of strings' } ] } )
246+ }
247+ return res . status ( 400 ) . json ( { error : 'BAD_INPUT' , message : 'Parameters were invalid' , errors : result . errors } )
248+ }
249+ }
250+
251+ // 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.
253+ if ( await repo . orgExists ( body ?. short_name , { session } , ! req . useRegistry ) ) {
297254 logger . info ( { uuid : req . ctx . uuid , message : body ?. short_name + ' organization was not created because it already exists.' } )
298255 await session . abortTransaction ( )
299256 return res . status ( 400 ) . json ( error . orgExists ( body ?. short_name ) )
300257 }
301- const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org , { session } )
258+
302259 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
260+ const isSecretariat = await repo . isSecretariatByShortName ( req . ctx . org , { session } )
303261 const requestingUserUUID = await userRepo . getUserUUID ( req . ctx . user , req . ctx . org , { session } )
304- returnValue = await repo . createOrg ( req . ctx . body , { session, upsert : true } , true , requestingUserUUID , isSecretariat )
262+ returnValue = await repo . createOrg ( req . ctx . body , { session, upsert : true } , ! req . useRegistry , requestingUserUUID , isSecretariat )
305263
306264 await session . commitTransaction ( )
307265 } catch ( error ) {
@@ -332,72 +290,6 @@ async function createOrg (req, res, next) {
332290 }
333291}
334292
335- // update org for registry
336- // /api/registry/org/{shortname}
337- async function registryUpdateOrg ( req , res , next ) {
338- const session = await mongoose . startSession ( )
339- const shortNameUrlParameter = req . ctx . params . shortname
340- let responseMessage
341- const orgRepository = req . ctx . repositories . getBaseOrgRepository ( )
342-
343- // Get the query parameters as JSON
344- // These are validated by the middleware in org/index.js
345- const queryParametersJson = req . ctx . query
346-
347- // Try for network request
348- try {
349- // Try for database, if we were catching things more specifically, we could move to 1 try statement
350- try {
351- session . startTransaction ( )
352- if ( queryParametersJson [ 'active_roles.add' ] ) {
353- if ( ! Array . isArray ( queryParametersJson . active_roles ?. add ) || queryParametersJson . active_roles ?. add . some ( item => typeof item !== 'string' ) ) {
354- await session . abortTransaction ( )
355- return res . status ( 400 ) . json ( { message : 'Parameters were invalid' , details : [ { param : 'authority' , msg : 'Parameter must be a one-dimensional array of strings' } ] } )
356- }
357- }
358-
359- if ( queryParametersJson [ 'active_roles.remove' ] ) {
360- if ( ! Array . isArray ( queryParametersJson . active_roles ?. remove ) || queryParametersJson . active_roles ?. remove . some ( item => typeof item !== 'string' ) ) {
361- await session . abortTransaction ( )
362- return res . status ( 400 ) . json ( { message : 'Parameters were invalid' , details : [ { param : 'authority' , msg : 'Parameter must be a one-dimensional array of strings' } ] } )
363- }
364- }
365-
366- if ( ! ( await orgRepository . orgExists ( shortNameUrlParameter , { session } ) ) ) {
367- logger . info ( { uuid : req . ctx . uuid , message : `Organization ${ shortNameUrlParameter } not found.` } )
368- await session . abortTransaction ( )
369- return res . status ( 404 ) . json ( error . orgDnePathParam ( shortNameUrlParameter ) )
370- }
371-
372- if ( Object . hasOwn ( queryParametersJson , 'new_short_name' ) && ( await orgRepository . orgExists ( queryParametersJson . new_short_name , { session } ) ) ) {
373- await session . abortTransaction ( )
374- return res . status ( 403 ) . json ( error . duplicateShortname ( queryParametersJson . new_short_name ) )
375- }
376-
377- const userRepo = req . ctx . repositories . getBaseUserRepository ( )
378- const requestingUserUUID = await userRepo . getUserUUID ( req . ctx . user , req . ctx . org , { session } )
379- const isSecretariat = await orgRepository . isSecretariatByShortName ( req . ctx . org , { session } )
380- const isAdmin = await userRepo . isAdmin ( req . ctx . user , req . ctx . org , { session } )
381- const updatedOrg = await orgRepository . updateOrg ( shortNameUrlParameter , queryParametersJson , { session } , false , requestingUserUUID , isAdmin , isSecretariat )
382-
383- responseMessage = { message : `${ updatedOrg . short_name } organization was successfully updated.` , updated : updatedOrg } // Clarify message
384- const payload = { action : 'update_org' , change : `${ updatedOrg . short_name } organization was successfully updated.` , org : updatedOrg }
385- payload . user_UUID = await userRepo . getUserUUID ( req . ctx . user , updatedOrg . UUID , { session } )
386- payload . org_UUID = updatedOrg . UUID
387- payload . req_UUID = req . ctx . uuid
388- await session . commitTransaction ( )
389- } catch ( error ) {
390- await session . abortTransaction ( )
391- throw error
392- } finally {
393- await session . endSession ( )
394- }
395- return res . status ( 200 ) . json ( responseMessage )
396- } catch ( err ) {
397- next ( err )
398- }
399- }
400-
401293/**
402294 * Updates an org only if the org exist for the specified shortname.
403295 * If no org exists, we do not create the org.
@@ -416,6 +308,24 @@ async function updateOrg (req, res, next) {
416308 try {
417309 try {
418310 session . startTransaction ( )
311+
312+ // TODO: Check to see if this check is needed for both options
313+ if ( req . useRegistry ) {
314+ if ( queryParametersJson [ 'active_roles.add' ] ) {
315+ if ( ! Array . isArray ( queryParametersJson . active_roles ?. add ) || queryParametersJson . active_roles ?. add . some ( item => typeof item !== 'string' ) ) {
316+ await session . abortTransaction ( )
317+ return res . status ( 400 ) . json ( { message : 'Parameters were invalid' , details : [ { param : 'authority' , msg : 'Parameter must be a one-dimensional array of strings' } ] } )
318+ }
319+ }
320+
321+ if ( queryParametersJson [ 'active_roles.remove' ] ) {
322+ if ( ! Array . isArray ( queryParametersJson . active_roles ?. remove ) || queryParametersJson . active_roles ?. remove . some ( item => typeof item !== 'string' ) ) {
323+ await session . abortTransaction ( )
324+ return res . status ( 400 ) . json ( { message : 'Parameters were invalid' , details : [ { param : 'authority' , msg : 'Parameter must be a one-dimensional array of strings' } ] } )
325+ }
326+ }
327+ }
328+
419329 if ( ! ( await orgRepository . orgExists ( shortNameUrlParameter , { session } ) ) ) {
420330 logger . info ( { uuid : req . ctx . uuid , message : `Organization ${ shortNameUrlParameter } not found.` } )
421331 return res . status ( 404 ) . json ( error . orgDnePathParam ( shortNameUrlParameter ) )
@@ -426,11 +336,10 @@ async function updateOrg (req, res, next) {
426336 }
427337
428338 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
339+ const requestingUserUUID = await userRepo . getUserUUID ( req . ctx . user , req . ctx . org , { session } )
429340 const isSecretariat = await orgRepository . isSecretariatByShortName ( req . ctx . org , { session } )
430341 const isAdmin = await userRepo . isAdmin ( req . ctx . user , req . ctx . org , { session } )
431- const requestingUserUUID = await userRepo . getUserUUID ( req . ctx . user , req . ctx . org , { session } )
432-
433- const updatedOrg = await orgRepository . updateOrg ( shortNameUrlParameter , queryParametersJson , { session } , true , requestingUserUUID , isAdmin , isSecretariat )
342+ const updatedOrg = await orgRepository . updateOrg ( shortNameUrlParameter , queryParametersJson , { session } , ! req . useRegistry , requestingUserUUID , isAdmin , isSecretariat )
434343
435344 responseMessage = { message : `${ updatedOrg . short_name } organization was successfully updated.` , updated : updatedOrg } // Clarify message
436345 const payload = { action : 'update_org' , change : `${ updatedOrg . short_name } organization was successfully updated.` , org : updatedOrg }
@@ -804,7 +713,5 @@ module.exports = {
804713 USER_SINGLE : getUser ,
805714 USER_CREATE_SINGLE : createUser ,
806715 USER_UPDATE_SINGLE : updateUser ,
807- USER_RESET_SECRET : resetSecret ,
808- REGISTRY_CREATE_ORG : registryCreateOrg ,
809- REGISTRY_UPDATE_ORG : registryUpdateOrg
716+ USER_RESET_SECRET : resetSecret
810717}
0 commit comments