@@ -170,19 +170,32 @@ export const allowedAuthoritiesForInvitation = (user, selectedRoles) => {
170170 if ( user . superUser ) {
171171 //The superuser has no organization guid, but is allowed to add one
172172 return Object . keys ( AUTHORITIES ) ;
173-
174173 }
175174 //Return only the AUTHORITIES where the user has the correct authority per selectedRole
175+ if ( user . institutionAdmin && ! isEmpty ( user . applications ) ) {
176+ const nonInstitutionalRoles = selectedRoles . filter ( role => user . organizationGUID !== role . organizationGUID ) ;
177+ if ( nonInstitutionalRoles . length === 0 ) {
178+ return Object . keys ( AUTHORITIES )
179+ . filter ( authority => authority !== AUTHORITIES . SUPER_USER ) ;
180+ } else {
181+ //If the user is an institution-admin but is also a regular inviter or manager of another non-institutional role, then filter the authorities
182+ const allowedAuthority = nonInstitutionalRoles
183+ . reduce ( ( acc , userRole ) => {
184+ if ( acc === null || AUTHORITIES_HIERARCHY [ userRole . authority ] > AUTHORITIES_HIERARCHY [ acc ] ) {
185+ return userRole . authority ;
186+ }
187+ return acc ;
188+ } , null ) || AUTHORITIES . INVITER ;
189+ return Object . keys ( AUTHORITIES )
190+ . filter ( auth => AUTHORITIES_HIERARCHY [ auth ] > AUTHORITIES_HIERARCHY [ allowedAuthority ] ) ;
191+ }
192+ }
176193 const userRolesForSelectedRoles = selectedRoles
177194 . map ( role => role . isUserRole ? role . role : role )
178195 . filter ( role => ( ! isEmpty ( user . organizationGUID ) && user . organizationGUID === role . organizationGUID ) ||
179196 user . userRoles . some ( userRole => userRole . role . id === role . id ) )
180197 . filter ( userRole => ! isEmpty ( userRole ) ) ;
181- //If the user is an institutionAdmin but is also a regular inviter or manager of this role, then filter the authorities
182- if ( user . institutionAdmin && ! isEmpty ( user . applications ) && userRolesForSelectedRoles . length === 0 ) {
183- return Object . keys ( AUTHORITIES )
184- . filter ( authority => authority !== AUTHORITIES . SUPER_USER ) ;
185- }
198+
186199 if ( ! isUserAllowed ( AUTHORITIES . INVITER , user ) ) {
187200 return [ ] ;
188201 }
@@ -193,11 +206,11 @@ export const allowedAuthoritiesForInvitation = (user, selectedRoles) => {
193206 }
194207 const leastImportantAuthority = userRolesForSelectedRoles
195208 . reduce ( ( acc , userRole ) => {
196- if ( AUTHORITIES_HIERARCHY [ userRole . authority ] < AUTHORITIES_HIERARCHY [ acc ] ) {
209+ if ( acc === null || AUTHORITIES_HIERARCHY [ userRole . authority ] > AUTHORITIES_HIERARCHY [ acc ] ) {
197210 return userRole . authority ;
198211 }
199212 return acc ;
200- } , AUTHORITIES . GUEST ) ;
213+ } , null ) || AUTHORITIES . INVITER ;
201214 return Object . keys ( AUTHORITIES )
202215 . filter ( auth => AUTHORITIES_HIERARCHY [ auth ] > AUTHORITIES_HIERARCHY [ leastImportantAuthority ] ) ;
203216
0 commit comments