@@ -601,10 +601,21 @@ function * checkGetAccess (authUser, submission) {
601
601
* @returns {Promise }
602
602
*/
603
603
function * checkReviewGetAccess ( authUser , submission ) {
604
+ let resources
604
605
let challengeDetails
605
606
const token = yield getM2Mtoken ( )
606
607
const challengeId = yield getV5ChallengeId ( submission . challengeId )
607
608
609
+ try {
610
+ resources = yield request . get ( `${ config . RESOURCEAPI_V5_BASE_URL } /resources?challengeId=${ challengeId } ` )
611
+ . set ( 'Authorization' , `Bearer ${ token } ` )
612
+ . set ( 'Content-Type' , 'application/json' )
613
+ } catch ( ex ) {
614
+ logger . error ( `Error while accessing ${ config . RESOURCEAPI_V5_BASE_URL } /resources?challengeId=${ challengeId } ` )
615
+ logger . error ( ex )
616
+ throw new errors . HttpStatusError ( 503 , `Could not determine the user's role in the challenge with id ${ challengeId } ` )
617
+ }
618
+
608
619
try {
609
620
challengeDetails = yield request . get ( `${ config . CHALLENGEAPI_V5_URL } /${ challengeId } ` )
610
621
. set ( 'Authorization' , `Bearer ${ token } ` )
@@ -615,9 +626,32 @@ function * checkReviewGetAccess (authUser, submission) {
615
626
return false
616
627
}
617
628
618
- if ( challengeDetails ) {
629
+ // Get map of role id to role name
630
+ const resourceRolesMap = yield getRoleIdToRoleNameMap ( )
631
+
632
+ // Check if role id to role name mapping is available. If not user's role cannot be determined.
633
+ if ( resourceRolesMap == null || _ . size ( resourceRolesMap ) === 0 ) {
634
+ throw new errors . HttpStatusError ( 503 , `Could not determine the user's role in the challenge with id ${ challengeId } ` )
635
+ }
636
+
637
+ if ( resources && challengeDetails ) {
638
+ // Fetch all roles of the User pertaining to the current challenge
639
+ const currUserRoles = _ . filter ( resources . body , { memberHandle : authUser . handle } )
640
+
641
+ // Populate the role names for the current user role ids
642
+ _ . forEach ( currUserRoles , currentUserRole => {
643
+ currentUserRole . role = resourceRolesMap [ currentUserRole . roleId ]
644
+ } )
645
+
619
646
const subTrack = challengeDetails . body . legacy . subTrack
620
647
648
+ // Check if the User is a Copilot, Manager or Observer for that contest
649
+ const validRoles = [ 'Copilot' , 'Manager' , 'Observer' ]
650
+ const passedRoles = currUserRoles . filter ( a => validRoles . includes ( a . role ) )
651
+ if ( passedRoles . length !== 0 ) {
652
+ return true
653
+ }
654
+
621
655
// For Marathon Match, everyone can access review result
622
656
if ( subTrack === 'DEVELOP_MARATHON_MATCH' ) {
623
657
logger . info ( 'No access check for Marathon match' )
@@ -632,6 +666,10 @@ function * checkReviewGetAccess (authUser, submission) {
632
666
633
667
return true
634
668
}
669
+ } else {
670
+ // We don't have enough details to validate the access
671
+ logger . debug ( 'No enough details to validate the Permissions' )
672
+ throw new errors . HttpStatusError ( 503 , `Not all information could be fetched about challenge with id ${ submission . challengeId } ` )
635
673
}
636
674
}
637
675
0 commit comments