Skip to content

Commit 1312233

Browse files
clean up access checks
1 parent 39428ec commit 1312233

File tree

1 file changed

+2
-72
lines changed

1 file changed

+2
-72
lines changed

src/services/ChallengeService.js

Lines changed: 2 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -67,74 +67,6 @@ async function ensureAccessibleForChallenge(user, challenge) {
6767
}
6868
}
6969

70-
/**
71-
* Filter challenges by groups access
72-
* @param {Object} currentUser the user who perform operation
73-
* @param {Array} challenges the challenges to filter
74-
* @returns {Array} the challenges that can be accessed by current user
75-
*/
76-
async function filterChallengesByGroupsAccess(currentUser, challenges) {
77-
const res = [];
78-
let userGroups;
79-
const needToCheckForGroupAccess = !currentUser
80-
? true
81-
: !currentUser.isMachine && !hasAdminRole(currentUser);
82-
const subGroupsMap = {};
83-
for (const challenge of challenges) {
84-
challenge.groups = _.filter(
85-
challenge.groups,
86-
(g) => !_.includes(["null", "undefined"], _.toString(g).toLowerCase())
87-
);
88-
let expandedGroups = [];
89-
if (
90-
!challenge.groups ||
91-
_.get(challenge, "groups.length", 0) === 0 ||
92-
!needToCheckForGroupAccess
93-
) {
94-
res.push(challenge);
95-
} else if (currentUser) {
96-
// get user groups if not yet
97-
if (_.isNil(userGroups)) {
98-
userGroups = await helper.getUserGroups(currentUser.userId);
99-
}
100-
// Expand challenge groups by subGroups
101-
// results are being saved on a hashmap for efficiency
102-
for (const group of challenge.groups) {
103-
let subGroups;
104-
if (subGroupsMap[group]) {
105-
subGroups = subGroupsMap[group];
106-
} else {
107-
subGroups = await helper.expandWithSubGroups(group);
108-
subGroupsMap[group] = subGroups;
109-
}
110-
expandedGroups = [..._.concat(expandedGroups, subGroups)];
111-
}
112-
// check if there is matched group
113-
// logger.debug('Groups', challenge.groups, userGroups)
114-
if (_.find(expandedGroups, (group) => !!_.find(userGroups, (ug) => ug.id === group))) {
115-
res.push(challenge);
116-
}
117-
}
118-
}
119-
return res;
120-
}
121-
122-
/**
123-
* Ensure the user can access the challenge by groups access
124-
* @param {Object} currentUser the user who perform operation
125-
* @param {Object} challenge the challenge to check
126-
*/
127-
async function ensureAccessibleByGroupsAccess(currentUser, challenge) {
128-
const filtered = await filterChallengesByGroupsAccess(currentUser, [challenge]);
129-
if (filtered.length === 0) {
130-
throw new errors.ForbiddenError(`ensureAccessibleByGroupsAccess :: You don't have access to this group!
131-
Current User: ${JSON.stringify(currentUser)}
132-
Challenge: ${JSON.stringify(challenge)}
133-
Filtered: ${JSON.stringify(filtered)}
134-
`);
135-
}
136-
}
137-
13870
/**
13971
* Search challenges by legacyId
14072
* @param {Object} currentUser the user who perform operation
@@ -2288,10 +2220,8 @@ async function deleteChallenge(currentUser, challengeId) {
22882220
if (!challenge) {
22892221
throw new errors.NotFoundError(`Challenge with id: ${challengeId} doesn't exist or is not in New status`);
22902222
}
2291-
// check groups authorization
2292-
await ensureAccessibleByGroupsAccess(currentUser, challenge);
2293-
// check if user are allowed to delete the challenge
2294-
await ensureAccessibleForChallenge(currentUser, challenge);
2223+
// ensure user can modify challenge
2224+
await helper.ensureUserCanModifyChallenge(currentUser, challenge);
22952225
// delete DB record
22962226
const { items: deletedItems } = await challengeDomain.delete(getLookupCriteria("id", challengeId));
22972227
if (!_.find(deletedItems, { id: challengeId })) {

0 commit comments

Comments
 (0)