|
| 1 | +import _ from 'lodash'; |
1 | 2 | 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'; |
3 | 9 |
|
4 | 10 |
|
5 | 11 | /**
|
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. |
7 | 15 | * @param {Object} req the express request instance
|
8 | 16 | * @return {Promise} returns a promise
|
9 | 17 | */
|
10 | 18 | 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); |
12 | 21 |
|
13 |
| - if (!hasAccess) { |
14 |
| - return reject(new Error('You do not have permissions to perform this action')); |
| 22 | + if (isAdmin) { |
| 23 | + return resolve(true); |
15 | 24 | }
|
16 | 25 |
|
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')); |
18 | 51 | });
|
0 commit comments