Skip to content

Commit d0f6757

Browse files
authored
Merge pull request #651 from topcoder-platform/PLAT-3368
Validate the handles in constraint
2 parents 3375fb7 + cda6c3a commit d0f6757

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/common/challenge-helper.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class ChallengeHelper {
9999

100100
// check groups authorization
101101
await helper.ensureAccessibleByGroupsAccess(currentUser, challenge);
102+
103+
if (challenge.constraints) {
104+
await this.validateChallengeConstraints(data.constraints);
105+
}
102106
}
103107

104108
async validateChallengeUpdateRequest(currentUser, challenge, data) {
@@ -196,6 +200,32 @@ class ChallengeHelper {
196200
`Cannot set winners for challenge with non-completed ${challenge.status} status`
197201
);
198202
}
203+
204+
if (data.constraints) {
205+
await this.validateChallengeConstraints(data.constraints);
206+
}
207+
}
208+
209+
async validateChallengeConstraints(constraints) {
210+
if (!_.isEmpty(constraints.allowedRegistrants)) {
211+
await this.validateAllowedRegistrants(constraints.allowedRegistrants);
212+
}
213+
}
214+
215+
async validateAllowedRegistrants(allowedRegistrants) {
216+
const members = await helper.getMembersByHandles(allowedRegistrants);
217+
const incorrectHandles = _.difference(
218+
allowedRegistrants,
219+
_.map(members, (m) => _.lowerCase(m.handle))
220+
);
221+
if (incorrectHandles.length > 0) {
222+
throw new errors.BadRequestError(
223+
`Cannot create challenge with invalid handle in constraints. [${_.join(
224+
incorrectHandles,
225+
","
226+
)}]`
227+
);
228+
}
199229
}
200230

201231
sanitizeRepeatedFieldsInUpdateRequest(data) {

src/common/helper.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,22 @@ async function getMemberByHandle(handle) {
10801080
return res.data || {};
10811081
}
10821082

1083+
/**
1084+
* Get members by handles
1085+
* @param {Array<String>} handles the user handle
1086+
* @returns {Object}
1087+
*/
1088+
async function getMembersByHandles(handles) {
1089+
const token = await m2mHelper.getM2MToken();
1090+
const res = await axios.get(
1091+
`${config.MEMBERS_API_URL}/?fields=handle&handlesLower=["${_.join(handles, '","')}"]`,
1092+
{
1093+
headers: { Authorization: `Bearer ${token}` },
1094+
}
1095+
);
1096+
return res.data;
1097+
}
1098+
10831099
/**
10841100
* Send self service notification
10851101
* @param {String} type the notification type
@@ -1199,6 +1215,7 @@ module.exports = {
11991215
cancelPayment,
12001216
sendSelfServiceNotification,
12011217
getMemberByHandle,
1218+
getMembersByHandles,
12021219
submitZendeskRequest,
12031220
updateSelfServiceProjectInfo,
12041221
getFromInternalCache,

src/services/ChallengeService.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,7 +1167,7 @@ createChallenge.schema = {
11671167
legacyId: Joi.number().integer().positive(),
11681168
constraints: Joi.object()
11691169
.keys({
1170-
allowedRegistrants: Joi.array().items(Joi.string()).optional(),
1170+
allowedRegistrants: Joi.array().items(Joi.string().trim().lowercase()).optional(),
11711171
})
11721172
.optional(),
11731173
startDate: Joi.date().iso(),
@@ -1998,7 +1998,7 @@ updateChallenge.schema = {
19981998
legacyId: Joi.number().integer().positive(),
19991999
constraints: Joi.object()
20002000
.keys({
2001-
allowedRegistrants: Joi.array().items(Joi.string()).optional(),
2001+
allowedRegistrants: Joi.array().items(Joi.string().trim().lowercase()).optional(),
20022002
})
20032003
.optional(),
20042004
status: Joi.string().valid(_.values(constants.challengeStatuses)),

0 commit comments

Comments
 (0)