@@ -8,75 +8,44 @@ import models from '../models';
8
8
* @param {String } policy the work management permission policy
9
9
* @return {Promise } Returns a promise
10
10
*/
11
- module . exports = policy => freq => new Promise ( ( resolve , reject ) => {
12
- const projectId = _ . parseInt ( freq . params . projectId ) ;
11
+ module . exports = policy => req => new Promise ( ( resolve , reject ) => {
12
+ const projectId = _ . parseInt ( req . params . projectId ) ;
13
+
13
14
return models . Project . findOne ( {
14
15
where : {
15
16
id : projectId ,
16
17
} ,
17
18
} )
18
- . then ( ( project ) => {
19
- if ( ! project ) {
20
- return resolve ( true ) ;
21
- }
22
-
23
- if ( ! project . templateId ) {
24
- const errorMessage = 'You do not have permissions to perform this action' ;
25
- return reject ( new Error ( errorMessage ) ) ;
26
- }
27
-
28
- return models . WorkManagementPermissions . findOne ( {
29
- where : {
30
- policy,
31
- projectTemplateId : project . templateId ,
32
- } ,
33
- } ) ;
34
- } )
35
- . then ( ( management ) => {
36
- if ( ! management ) {
37
- const errorMessage = 'You do not have permissions to perform this action' ;
38
- return reject ( new Error ( errorMessage ) ) ;
39
- }
19
+ . then ( ( project ) => {
20
+ if ( ! project ) {
21
+ const apiErr = new Error ( `Project not found for id ${ projectId } ` ) ;
22
+ apiErr . status = 404 ;
23
+ return Promise . reject ( apiErr ) ;
24
+ }
40
25
41
- return models . ProjectMember . getActiveProjectMembers ( projectId )
42
- . then ( ( members ) => {
43
- const req = freq ;
44
- const mem = _ . find ( members , m => m . userId === req . authUser . userId ) ;
45
- let allowRule = false ;
46
- if ( management . allowRule ) {
47
- if ( management . allowRule . projectRoles
48
- && management . allowRule . projectRoles . length > 0
49
- && ! _ . isUndefined ( mem ) ) {
50
- allowRule = allowRule || _ . includes ( management . allowRule . projectRoles , mem . role ) ;
51
- }
52
- if ( management . allowRule . topcoderRoles && management . allowRule . topcoderRoles . length > 0 ) {
53
- allowRule = allowRule || util . hasRoles ( freq , management . allowRule . topcoderRoles ) ;
54
- }
55
- }
56
- if ( management . denyRule ) {
57
- let denyRuleProject = false ;
58
- let denyRuleTopcoder = false ;
59
- if ( management . denyRule . projectRoles
60
- && management . denyRule . projectRoles . length > 0
61
- && ! _ . isUndefined ( mem ) ) {
62
- denyRuleProject = _ . includes ( management . denyRule . projectRoles , mem . role ) ;
63
- }
64
- if ( management . denyRule . topcoderRoles && management . denyRule . topcoderRoles . length > 0 ) {
65
- denyRuleTopcoder = util . hasRoles ( freq , management . denyRule . topcoderRoles ) ;
66
- }
26
+ if ( ! project . templateId ) {
27
+ return null ;
28
+ }
67
29
68
- const denyRule = ( denyRuleProject || denyRuleTopcoder ) ;
69
- return ! denyRule && allowRule ;
70
- }
30
+ return models . WorkManagementPermissions . findOne ( {
31
+ where : {
32
+ policy,
33
+ projectTemplateId : project . templateId ,
34
+ } ,
35
+ } ) ;
36
+ } )
37
+ . then ( ( permission ) => {
38
+ if ( ! permission ) {
39
+ return false ;
40
+ }
71
41
72
- return allowRule ;
73
- } ) ;
74
- } )
75
- . then ( ( hasAccess ) => {
76
- if ( ! hasAccess ) {
77
- const errorMessage = 'You do not have permissions to perform this action' ;
78
- return reject ( new Error ( errorMessage ) ) ;
79
- }
80
- return resolve ( true ) ;
81
- } ) ;
82
- } ) ;
42
+ return util . hasPermissionForProject ( permission , req . authUser , projectId ) ;
43
+ } )
44
+ . then ( ( hasAccess ) => {
45
+ if ( ! hasAccess ) {
46
+ const errorMessage = 'You do not have permissions to perform this action' ;
47
+ return reject ( new Error ( errorMessage ) ) ;
48
+ }
49
+ return resolve ( true ) ;
50
+ } ) ;
51
+ } ) ;
0 commit comments