Skip to content

Commit 0c6ca3b

Browse files
committed
update loginc for validating project member roles
1 parent 6dc2075 commit 0c6ca3b

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

src/permissions/copilotAndAbove.js

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import _ from 'lodash';
22
import util from '../util';
33
import {
4-
USER_ROLE,
54
PROJECT_MEMBER_ROLE,
65
ADMIN_ROLES,
76
} from '../constants';
@@ -23,29 +22,24 @@ module.exports = req => new Promise((resolve, reject) => {
2322
return resolve(true);
2423
}
2524

26-
const isManagerOrCopilot = util.hasRoles(req, [
27-
PROJECT_MEMBER_ROLE.MANAGER,
28-
PROJECT_MEMBER_ROLE.COPILOT,
29-
USER_ROLE.MANAGER,
30-
USER_ROLE.COPILOT,
31-
USER_ROLE.COPILOT_MANAGER,
32-
]);
25+
return models.ProjectMember.getActiveProjectMembers(projectId)
26+
.then((members) => {
27+
req.context = req.context || {};
28+
req.context.currentProjectMembers = members;
29+
const validMemberProjectRoles = [
30+
PROJECT_MEMBER_ROLE.MANAGER,
31+
PROJECT_MEMBER_ROLE.COPILOT,
32+
];
33+
// check if the copilot or manager has access to this project
34+
const isMember = _.some(
35+
members,
36+
m => m.userId === req.authUser.userId && validMemberProjectRoles.includes(m.role),
37+
);
3338

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'));
39+
if (!isMember) {
40+
// the copilot or manager is not a registered project member
41+
return reject(new Error('You do not have permissions to perform this action'));
42+
}
43+
return resolve(true);
44+
});
5145
});

0 commit comments

Comments
 (0)