1
1
import _ from 'lodash' ;
2
2
import util from '../util' ;
3
3
import {
4
- USER_ROLE ,
5
4
PROJECT_MEMBER_ROLE ,
6
5
ADMIN_ROLES ,
7
6
} from '../constants' ;
@@ -23,29 +22,24 @@ module.exports = req => new Promise((resolve, reject) => {
23
22
return resolve ( true ) ;
24
23
}
25
24
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
+ ) ;
33
38
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
+ } ) ;
51
45
} ) ;
0 commit comments