Skip to content

Commit b62c123

Browse files
authored
Merge pull request topcoder-platform#244 from topcoder-platform/Issue_243
Issue 243
2 parents b1ed898 + f3c92d2 commit b62c123

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/common/helper.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,21 @@ function * checkGetAccess (authUser, submission) {
601601
* @returns {Promise}
602602
*/
603603
function * checkReviewGetAccess (authUser, submission) {
604+
let resources
604605
let challengeDetails
605606
const token = yield getM2Mtoken()
606607
const challengeId = yield getV5ChallengeId(submission.challengeId)
607608

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+
608619
try {
609620
challengeDetails = yield request.get(`${config.CHALLENGEAPI_V5_URL}/${challengeId}`)
610621
.set('Authorization', `Bearer ${token}`)
@@ -615,9 +626,32 @@ function * checkReviewGetAccess (authUser, submission) {
615626
return false
616627
}
617628

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+
619646
const subTrack = challengeDetails.body.legacy.subTrack
620647

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+
621655
// For Marathon Match, everyone can access review result
622656
if (subTrack === 'DEVELOP_MARATHON_MATCH') {
623657
logger.info('No access check for Marathon match')
@@ -632,6 +666,10 @@ function * checkReviewGetAccess (authUser, submission) {
632666

633667
return true
634668
}
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}`)
635673
}
636674
}
637675

src/routes/ReviewRoutes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
controller: 'ReviewController',
2525
method: 'getReview',
2626
auth: 'jwt',
27-
access: ['Administrator', 'Copilot'],
27+
access: ['Administrator', 'Copilot', 'Topcoder User'],
2828
scopes: ['read:review', 'all:review']
2929
},
3030
put: {

src/services/ReviewService.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ function * getReview (authUser, reviewId) {
7070
false
7171
)
7272
logger.info('Check User access before returning the review')
73-
yield helper.checkReviewGetAccess(authUser, submission)
73+
if (_.intersection(authUser.roles, ['Administrator', 'administrator']).length === 0 && !authUser.scopes) {
74+
yield helper.checkReviewGetAccess(authUser, submission)
75+
}
7476
// Return the review
7577
return review
7678
}

0 commit comments

Comments
 (0)