Skip to content

Commit d33094d

Browse files
committed
Fix copilotAndAbove permission to check that users is a member of the project
1 parent 5d31391 commit d33094d

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

src/permissions/copilotAndAbove.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,51 @@
1+
import _ from 'lodash';
12
import util from '../util';
2-
import { MANAGER_ROLES, USER_ROLE } from '../constants';
3+
import {
4+
USER_ROLE,
5+
PROJECT_MEMBER_MANAGER_ROLES,
6+
ADMIN_ROLES,
7+
} from '../constants';
8+
import models from '../models';
39

410

511
/**
6-
* Permission to alloow copilot and above roles to perform certain operations
12+
* Permission to allow copilot and above roles to perform certain operations
13+
* - User with Topcoder admins roles should be able to perform the operations.
14+
* - Project members with copilot and manager Project roles should be also able to perform the operations.
715
* @param {Object} req the express request instance
816
* @return {Promise} returns a promise
917
*/
1018
module.exports = req => new Promise((resolve, reject) => {
11-
const hasAccess = util.hasRoles(req, [...MANAGER_ROLES, USER_ROLE.COPILOT]);
19+
const projectId = _.parseInt(req.params.projectId);
20+
const isAdmin = util.hasRoles(req, ADMIN_ROLES);
1221

13-
if (!hasAccess) {
14-
return reject(new Error('You do not have permissions to perform this action'));
22+
if (isAdmin) {
23+
return resolve(true);
1524
}
1625

17-
return resolve(true);
26+
const isManagerOrCopilot = util.hasRoles(req, [
27+
...PROJECT_MEMBER_MANAGER_ROLES,
28+
USER_ROLE.MANAGER,
29+
USER_ROLE.TOPCODER_ACCOUNT_MANAGER,
30+
USER_ROLE.COPILOT,
31+
USER_ROLE.COPILOT_MANAGER,
32+
]);
33+
34+
if (isManagerOrCopilot) {
35+
return models.ProjectMember.getActiveProjectMembers(projectId)
36+
.then((members) => {
37+
req.context = req.context || {};
38+
req.context.currentProjectMembers = members;
39+
// check if the copilot or manager has access to this project
40+
const isMember = _.some(members, m => m.userId === req.authUser.userId);
41+
42+
if (!isMember) {
43+
// the copilot or manager is not a registered project member
44+
return reject(new Error('You do not have permissions to perform this action'));
45+
}
46+
return resolve(true);
47+
});
48+
}
49+
50+
return reject(new Error('You do not have permissions to perform this action'));
1851
});

0 commit comments

Comments
 (0)