@@ -281,7 +281,7 @@ async function createOrg (req, res, next) {
281281 const body = req . ctx . body
282282 let returnValue
283283 // Do not allow the user to pass in a UUID
284- if ( body ?. UUID ?? null ) return res . status ( 400 ) . json ( error . uuidProvided ( 'org' ) )
284+ if ( ( body ?. UUID ?? null ) || ( body ?. uuid ?? null ) ) return res . status ( 400 ) . json ( error . uuidProvided ( 'org' ) )
285285
286286 try {
287287 session . startTransaction ( )
@@ -441,14 +441,26 @@ async function createUser (req, res, next) {
441441 try {
442442 const body = req . ctx . body
443443 const userRepo = req . ctx . repositories . getBaseUserRepository ( )
444+ const orgRepo = req . ctx . repositories . getBaseOrgRepository ( )
444445 const orgShortName = req . ctx . params . shortname
445446 let returnValue
446447
448+ // Check to make sure Org Exists first
449+ const orgUUID = await orgRepo . getOrgUUID ( orgShortName )
450+ if ( ! orgUUID ) {
451+ logger . info ( { uuid : req . ctx . uuid , message : 'The user could not be created because ' + orgShortName + ' organization does not exist.' } )
452+ return res . status ( 404 ) . json ( error . orgDnePathParam ( orgShortName ) )
453+ }
454+
447455 // Do not allow the user to pass in a UUID
448- if ( body ?. UUID ?? null ) {
456+ if ( ( body ?. UUID ?? null ) || ( body ?. uuid ?? null ) ) {
449457 return res . status ( 400 ) . json ( error . uuidProvided ( 'user' ) )
450458 }
451459
460+ if ( ( body ?. org_UUID ?? null ) || ( body ?. org_uuid ?? null ) ) {
461+ return res . status ( 400 ) . json ( error . uuidProvided ( 'org' ) )
462+ }
463+
452464 try {
453465 session . startTransaction ( )
454466 if ( req . useRegistry ) {
@@ -461,6 +473,10 @@ async function createUser (req, res, next) {
461473 await session . abortTransaction ( )
462474 return res . status ( 400 ) . json ( { message : 'Parameters were invalid' , errors : result . errors } )
463475 }
476+ } else {
477+ if ( ! body ?. username || typeof body ?. username !== 'string' || ! body ?. username . length > 0 ) {
478+ return res . status ( 400 ) . json ( { message : 'Parameters were invalid' , details : [ { param : 'username' , msg : 'Parameter must be a non empty string' } ] } )
479+ }
464480 }
465481
466482 // Ask repo if user already exists
@@ -476,7 +492,7 @@ async function createUser (req, res, next) {
476492 }
477493
478494 const users = await userRepo . findUsersByOrgShortname ( orgShortName , { session } )
479- if ( users . toObject ( ) . length >= 100 ) {
495+ if ( users . length >= 100 ) {
480496 await session . abortTransaction ( )
481497 return res . status ( 400 ) . json ( error . userLimitReached ( ) )
482498 }
@@ -552,6 +568,19 @@ async function updateUser (req, res, next) {
552568 return res . status ( 404 ) . json ( error . orgDnePathParam ( shortNameParams ) )
553569 }
554570
571+ if ( shortNameParams !== requesterShortName && ! isRequesterSecretariat ) {
572+ logger . info ( { uuid : req . ctx . uuid , message : `${ shortNameParams } organization data can only be modified by users of the same organization or the Secretariat.` } )
573+ await session . abortTransaction ( )
574+ return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
575+ }
576+
577+ // Specific check for org_short_name (Secretariat only)
578+ if ( queryParametersJson . org_short_name && ! isRequesterSecretariat ) {
579+ logger . info ( { uuid : req . ctx . uuid , message : 'Only Secretariat can reassign user organization.' } )
580+ await session . abortTransaction ( )
581+ return res . status ( 403 ) . json ( error . notAllowedToChangeOrganization ( ) )
582+ }
583+
555584 if ( ! isRequesterSecretariat && ! isAdmin ) {
556585 if ( targetUserUUID !== requesterUUID ) {
557586 if ( ! targetUserUUID ) {
@@ -565,29 +594,41 @@ async function updateUser (req, res, next) {
565594 }
566595 }
567596
568- if ( ! targetUserUUID ) {
569- logger . info ( { uuid : req . ctx . uuid , message : 'User DNE' } )
570- await session . abortTransaction ( )
571- return res . status ( 404 ) . json ( error . userDne ( usernameParams ) )
597+ const newOrgShortNameToMoveTo = queryParametersJson . org_short_name
598+
599+ if ( newOrgShortNameToMoveTo ) {
600+ if ( newOrgShortNameToMoveTo === shortNameParams ) {
601+ logger . info ( { uuid : req . ctx . uuid , message : `User ${ usernameParams } is already in organization ${ newOrgShortNameToMoveTo } .` } )
602+ await session . abortTransaction ( )
603+ return res . status ( 403 ) . json ( error . alreadyInOrg ( newOrgShortNameToMoveTo , usernameParams ) )
604+ }
605+
606+ const newTargetRegistryOrgUUID = await orgRepo . getOrgUUID ( newOrgShortNameToMoveTo , { session } )
607+
608+ if ( ! newTargetRegistryOrgUUID ) {
609+ logger . info ( { uuid : req . ctx . uuid , message : `New target organization ${ newOrgShortNameToMoveTo } does not exist.` } )
610+ await session . abortTransaction ( )
611+ return res . status ( 404 ) . json ( error . orgDne ( newOrgShortNameToMoveTo , 'org_short_name' , 'query' ) )
612+ }
572613 }
573614
574- if ( shortNameParams !== requesterShortName && ! isRequesterSecretariat ) {
575- logger . info ( { uuid : req . ctx . uuid , message : `${ shortNameParams } organization data can only be modified by users of the same organization or the Secretariat.` } )
576- await session . abortTransaction ( )
577- return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
615+ if ( queryParametersJson . active ) {
616+ if ( requesterUUID === targetUserUUID ) {
617+ await session . abortTransaction ( )
618+ return res . status ( 403 ) . json ( error . notOrgAdminOrSecretariatUpdate ( ) )
619+ }
578620 }
579621
580- if ( await userRepo . orgHasUser ( shortNameParams , targetUserUUID , { session } ) ) {
581- logger . info ( { uuid : req . ctx . uuid , message : ` User ${ usernameParams } does not exist for ${ shortNameParams } organization.` } )
622+ if ( ! targetUserUUID ) {
623+ logger . info ( { uuid : req . ctx . uuid , message : ' User DNE' } )
582624 await session . abortTransaction ( )
583625 return res . status ( 404 ) . json ( error . userDne ( usernameParams ) )
584626 }
585627
586- // Specific check for org_short_name (Secretariat only)
587- if ( queryParametersJson . org_short_name && ! isRequesterSecretariat ) {
588- logger . info ( { uuid : req . ctx . uuid , message : 'Only Secretariat can reassign user organization.' } )
628+ if ( ! await userRepo . orgHasUserByUUID ( shortNameParams , targetUserUUID , { session } ) ) {
629+ logger . info ( { uuid : req . ctx . uuid , message : `User ${ usernameParams } does not exist for ${ shortNameParams } organization.` } )
589630 await session . abortTransaction ( )
590- return res . status ( 403 ) . json ( error . notAllowedToChangeOrganization ( ) )
631+ return res . status ( 404 ) . json ( error . userDne ( usernameParams ) )
591632 }
592633
593634 // General permission check for fields requiring admin/secretariat
@@ -609,13 +650,6 @@ async function updateUser (req, res, next) {
609650 }
610651 }
611652
612- if ( queryParametersJson . active ) {
613- if ( requesterUUID === targetUserUUID ) {
614- await session . abortTransaction ( )
615- return res . status ( 403 ) . json ( error . notOrgAdminOrSecretariatUpdate ( ) )
616- }
617- }
618-
619653 // This is a special case, and needs to be handled in the controller, and not in the repository
620654 const rolesFromQuery = queryParametersJson [ 'active_roles.remove' ] ?? [ ]
621655 const removeRolesCollector = [ ]
@@ -633,25 +667,7 @@ async function updateUser (req, res, next) {
633667 }
634668 }
635669
636- const newOrgShortNameToMoveTo = queryParametersJson . org_short_name
637-
638- if ( newOrgShortNameToMoveTo ) {
639- if ( newOrgShortNameToMoveTo === shortNameParams ) {
640- logger . info ( { uuid : req . ctx . uuid , message : `User ${ usernameParams } is already in organization ${ newOrgShortNameToMoveTo } .` } )
641- await session . abortTransaction ( )
642- return res . status ( 403 ) . json ( error . alreadyInOrg ( newOrgShortNameToMoveTo , usernameParams ) )
643- }
644-
645- const newTargetRegistryOrgUUID = await orgRepo . getOrgUUID ( newOrgShortNameToMoveTo , { session } )
646-
647- if ( ! newTargetRegistryOrgUUID ) {
648- logger . info ( { uuid : req . ctx . uuid , message : `New target organization ${ newOrgShortNameToMoveTo } does not exist.` } )
649- await session . abortTransaction ( )
650- return res . status ( 404 ) . json ( error . orgDne ( newOrgShortNameToMoveTo , 'org_short_name' , 'query' ) )
651- }
652- }
653-
654- const payload = await userRepo . updateUser ( usernameParams , shortNameParams , queryParametersJson , { session } )
670+ const payload = await userRepo . updateUser ( usernameParams , shortNameParams , queryParametersJson , { session } , ! req . useRegistry )
655671 await session . commitTransaction ( )
656672 return res . status ( 200 ) . json ( { message : `${ usernameParams } was successfully updated.` , updated : payload } )
657673 } catch ( err ) {
@@ -707,24 +723,29 @@ async function resetSecret (req, res, next) {
707723 const requesterUserUUID = await userRepo . getUserUUID ( requesterUsername , requesterOrgShortName , { session } , ! req . useRegistry )
708724
709725 const isRequesterSecretariat = await orgRepo . isSecretariatByShortName ( requesterOrgShortName , { session } )
710- // const isAdmin = await userRepo.isAdmin(requesterUsername, targetOrgShortName, { session })
711- if ( ! isRequesterSecretariat && ( requesterOrgShortName !== targetOrgShortName ) ) {
726+
727+ if ( ! isRequesterSecretariat ) {
728+ // If they are in the same organization, they must be the target user themselves OR an admin of the target org.
729+
730+ // 1. WE are not the same user
712731 if ( requesterUserUUID !== targetUserUUID ) {
713- 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.' } )
714- await session . abortTransaction ( )
715- return res . status ( 403 ) . json ( error . notSameUserOrSecretariat ( ) )
732+ // Check to see if we are the admin of the target organization
733+ const isAdminOfTargetOrg = await userRepo . isAdmin ( requesterUsername , targetOrgShortName , { session } )
734+ // The tests say we have to check the org next:
735+ if ( requesterOrgShortName !== targetOrgShortName && ! isAdminOfTargetOrg ) {
736+ 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.' } )
737+ await session . abortTransaction ( )
738+ return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
739+ }
740+
741+ if ( ! isAdminOfTargetOrg ) {
742+ 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.' } )
743+ await session . abortTransaction ( )
744+ return res . status ( 403 ) . json ( error . notSameUserOrSecretariat ( ) )
745+ }
716746 }
717- 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.' } )
718- await session . abortTransaction ( )
719- return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
720- }
721- // Check if requester is either admin of target org or secretariat, or is same as target user
722- const isAdminOrSecretariat = await userRepo . isAdminOrSecretariat ( targetOrgShortName , requesterUsername , requesterOrgShortName , { session } , ! req . useRegistry )
723- if ( ! isAdminOrSecretariat && ( requesterUserUUID !== targetUserUUID ) ) {
724- 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.' } )
725- await session . abortTransaction ( )
726- return res . status ( 403 ) . json ( error . notSameUserOrSecretariat ( ) )
727747 }
748+
728749 const updatedSecret = await userRepo . resetSecret ( targetUsername , targetOrgShortName , { session } , ! req . useRegistry )
729750
730751 logger . info ( { uuid : req . ctx . uuid , message : `The API secret was successfully reset and sent to ${ targetUsername } ` } )
0 commit comments