@@ -552,6 +552,19 @@ async function updateUser (req, res, next) {
552552 return res . status ( 404 ) . json ( error . orgDnePathParam ( shortNameParams ) )
553553 }
554554
555+ if ( shortNameParams !== requesterShortName && ! isRequesterSecretariat ) {
556+ logger . info ( { uuid : req . ctx . uuid , message : `${ shortNameParams } organization data can only be modified by users of the same organization or the Secretariat.` } )
557+ await session . abortTransaction ( )
558+ return res . status ( 403 ) . json ( error . notSameOrgOrSecretariat ( ) )
559+ }
560+
561+ // Specific check for org_short_name (Secretariat only)
562+ if ( queryParametersJson . org_short_name && ! isRequesterSecretariat ) {
563+ logger . info ( { uuid : req . ctx . uuid , message : 'Only Secretariat can reassign user organization.' } )
564+ await session . abortTransaction ( )
565+ return res . status ( 403 ) . json ( error . notAllowedToChangeOrganization ( ) )
566+ }
567+
555568 if ( ! isRequesterSecretariat && ! isAdmin ) {
556569 if ( targetUserUUID !== requesterUUID ) {
557570 if ( ! targetUserUUID ) {
@@ -565,29 +578,41 @@ async function updateUser (req, res, next) {
565578 }
566579 }
567580
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 ) )
581+ const newOrgShortNameToMoveTo = queryParametersJson . org_short_name
582+
583+ if ( newOrgShortNameToMoveTo ) {
584+ if ( newOrgShortNameToMoveTo === shortNameParams ) {
585+ logger . info ( { uuid : req . ctx . uuid , message : `User ${ usernameParams } is already in organization ${ newOrgShortNameToMoveTo } .` } )
586+ await session . abortTransaction ( )
587+ return res . status ( 403 ) . json ( error . alreadyInOrg ( newOrgShortNameToMoveTo , usernameParams ) )
588+ }
589+
590+ const newTargetRegistryOrgUUID = await orgRepo . getOrgUUID ( newOrgShortNameToMoveTo , { session } )
591+
592+ if ( ! newTargetRegistryOrgUUID ) {
593+ logger . info ( { uuid : req . ctx . uuid , message : `New target organization ${ newOrgShortNameToMoveTo } does not exist.` } )
594+ await session . abortTransaction ( )
595+ return res . status ( 404 ) . json ( error . orgDne ( newOrgShortNameToMoveTo , 'org_short_name' , 'query' ) )
596+ }
572597 }
573598
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 ( ) )
599+ if ( queryParametersJson . active ) {
600+ if ( requesterUUID === targetUserUUID ) {
601+ await session . abortTransaction ( )
602+ return res . status ( 403 ) . json ( error . notOrgAdminOrSecretariatUpdate ( ) )
603+ }
578604 }
579605
580- if ( await userRepo . orgHasUser ( shortNameParams , targetUserUUID , { session } ) ) {
581- logger . info ( { uuid : req . ctx . uuid , message : ` User ${ usernameParams } does not exist for ${ shortNameParams } organization.` } )
606+ if ( ! targetUserUUID ) {
607+ logger . info ( { uuid : req . ctx . uuid , message : ' User DNE' } )
582608 await session . abortTransaction ( )
583609 return res . status ( 404 ) . json ( error . userDne ( usernameParams ) )
584610 }
585611
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.' } )
612+ if ( ! await userRepo . orgHasUserByUUID ( shortNameParams , targetUserUUID , { session } ) ) {
613+ logger . info ( { uuid : req . ctx . uuid , message : `User ${ usernameParams } does not exist for ${ shortNameParams } organization.` } )
589614 await session . abortTransaction ( )
590- return res . status ( 403 ) . json ( error . notAllowedToChangeOrganization ( ) )
615+ return res . status ( 404 ) . json ( error . userDne ( usernameParams ) )
591616 }
592617
593618 // General permission check for fields requiring admin/secretariat
@@ -609,13 +634,6 @@ async function updateUser (req, res, next) {
609634 }
610635 }
611636
612- if ( queryParametersJson . active ) {
613- if ( requesterUUID === targetUserUUID ) {
614- await session . abortTransaction ( )
615- return res . status ( 403 ) . json ( error . notOrgAdminOrSecretariatUpdate ( ) )
616- }
617- }
618-
619637 // This is a special case, and needs to be handled in the controller, and not in the repository
620638 const rolesFromQuery = queryParametersJson [ 'active_roles.remove' ] ?? [ ]
621639 const removeRolesCollector = [ ]
@@ -633,24 +651,6 @@ async function updateUser (req, res, next) {
633651 }
634652 }
635653
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-
654654 const payload = await userRepo . updateUser ( usernameParams , shortNameParams , queryParametersJson , { session } )
655655 await session . commitTransaction ( )
656656 return res . status ( 200 ) . json ( { message : `${ usernameParams } was successfully updated.` , updated : payload } )
0 commit comments