Skip to content

Commit dd0743d

Browse files
allow any resource with full access to modify a challenge
1 parent e0cc722 commit dd0743d

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

config/default.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ module.exports = {
4747
FILE_UPLOAD_SIZE_LIMIT: process.env.FILE_UPLOAD_SIZE_LIMIT
4848
? Number(process.env.FILE_UPLOAD_SIZE_LIMIT) : 50 * 1024 * 1024, // 50M
4949
RESOURCES_API_URL: process.env.RESOURCES_API_URL || 'http://localhost:4000/v5/resources',
50+
// TODO: change this to localhost
51+
RESOURCE_ROLES_API_URL: process.env.RESOURCE_ROLES_API_URL || 'http://api.topcoder-dev.com/v5/resource-roles',
5052
GROUPS_API_URL: process.env.GROUPS_API_URL || 'http://localhost:4000/v5/groups',
5153
PROJECTS_API_URL: process.env.PROJECTS_API_URL || 'http://localhost:4000/v5/projects',
5254
TERMS_API_URL: process.env.TERMS_API_URL || 'http://localhost:4000/v5/terms',

src/common/helper.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,46 @@ async function getM2MToken () {
392392
*/
393393
async function getChallengeResources (challengeId) {
394394
const token = await getM2MToken()
395-
const url = `${config.RESOURCES_API_URL}?challengeId=${challengeId}`
396-
const res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })
395+
const perPage = 100
396+
let page = 1
397+
let result = []
398+
while (true) {
399+
const url = `${config.RESOURCES_API_URL}?challengeId=${challengeId}&perPage=${perPage}&page=${page}`
400+
const res = await axios.get(url, { headers: { Authorization: `Bearer ${token}` } })
401+
if (!res.data || res.data.length === 0) {
402+
break
403+
}
404+
result = result.concat(res.data)
405+
page += 1
406+
if (res.headers['x-total-pages'] && page > Number(res.headers['x-total-pages'])) {
407+
break
408+
}
409+
}
410+
return result
411+
}
412+
413+
/**
414+
* Get resource roles
415+
* @returns {Promise<Array>} the challenge resources
416+
*/
417+
async function getResourceRoles () {
418+
const token = await getM2MToken()
419+
const res = await axios.get(config.RESOURCE_ROLES_API_URL, { headers: { Authorization: `Bearer ${token}` } })
397420
return res.data || []
398421
}
399422

423+
/**
424+
* Check if a user has full access on a challenge
425+
* @param {String} challengeId the challenge UUID
426+
* @param {String} userId the user ID
427+
*/
428+
async function userHasFullAccess (challengeId, userId) {
429+
const resourceRoles = await getResourceRoles()
430+
const rolesWithFullAccess = _.map(_.filter(resourceRoles, r => r.fullAccess), 'id')
431+
const challengeResources = await getChallengeResources(challengeId)
432+
return _.filter(challengeResources, r => _.toString(r.memberId) === _.toString(userId) && _.includes(rolesWithFullAccess, r.roleId)).length > 0
433+
}
434+
400435
/**
401436
* Get all user groups
402437
* @param {String} userId the user id
@@ -723,5 +758,7 @@ module.exports = {
723758
getProjectBillingAccount,
724759
expandWithSubGroups,
725760
getCompleteUserGroupTreeIds,
726-
expandWithParentGroups
761+
expandWithParentGroups,
762+
getResourceRoles,
763+
userHasFullAccess
727764
}

src/services/ChallengeService.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,8 +1159,9 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
11591159
newAttachments = await helper.getByIds('Attachment', data.attachmentIds || [])
11601160
}
11611161

1162-
if (!currentUser.isMachine && !helper.hasAdminRole(currentUser) && challenge.createdBy.toLowerCase() !== currentUser.handle.toLowerCase()) {
1163-
throw new errors.ForbiddenError(`Only M2M, admin or challenge's copilot can perform modification.`)
1162+
const userHasFullAccess = await helper.userHasFullAccess(challengeId, currentUser.userId)
1163+
if (!currentUser.isMachine && !helper.hasAdminRole(currentUser) && challenge.createdBy.toLowerCase() !== currentUser.handle.toLowerCase() && !userHasFullAccess) {
1164+
throw new errors.ForbiddenError(`Only M2M, admin, challenge's copilot or users with full access can perform modification.`)
11641165
}
11651166

11661167
// Validate the challenge terms

0 commit comments

Comments
 (0)