Skip to content

Create Challenge : Validate Group Ids are valid or not #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/common/challenge-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ class ChallengeHelper {
}
}

/**
* Validate Challenge groups.
* @param {Object} groups the group of a challenge
*/
async validateGroups(groups) {
const promises = [];
_.each(groups, (g) => {
promises.push(
(async () => {
const group = await helper.getGroupById(g);
if (!group || group.status !== "active") {
throw new errors.BadRequestError("The groups provided are invalid " + g);
}
})()
);
});
await Promise.all(promises);
}

async validateCreateChallengeRequest(currentUser, challenge) {
// projectId is required for non self-service challenges
if (challenge.legacy.selfService == null && challenge.projectId == null) {
Expand All @@ -98,7 +117,13 @@ class ChallengeHelper {
// helper.ensureNoDuplicateOrNullElements(challenge.events, 'events')

// check groups authorization
await helper.ensureAccessibleByGroupsAccess(currentUser, challenge);
if (challenge.groups && challenge.groups.length > 0) {
if (currentUser.isMachine || hasAdminRole(currentUser)) {
await this.validateGroups(challenge.groups);
} else {
await helper.ensureAccessibleByGroupsAccess(currentUser, challenge);
}
}

if (challenge.constraints) {
await ChallengeHelper.validateChallengeConstraints(challenge.constraints);
Expand All @@ -118,8 +143,12 @@ class ChallengeHelper {
}

// check groups access to be updated group values
if (data.groups) {
await ensureAcessibilityToModifiedGroups(currentUser, data, challenge);
if (data.groups && data.groups.length > 0) {
if (currentUser.isMachine || hasAdminRole(currentUser)) {
await this.validateGroups(data.groups);
} else {
await ensureAcessibilityToModifiedGroups(currentUser, data, challenge);
}
}

// Ensure descriptionFormat is either 'markdown' or 'html'
Expand Down