1
1
/**
2
2
* API to get project permissions
3
3
*/
4
- import _ from 'lodash' ;
5
4
import validate from 'express-validation' ;
6
5
import Joi from 'joi' ;
7
6
import { middleware as tcMiddleware } from 'tc-core-library-js' ;
@@ -21,7 +20,6 @@ module.exports = [
21
20
permissions ( 'permissions.view' ) ,
22
21
( req , res , next ) => {
23
22
const projectId = req . params . projectId ;
24
- let workManagementPermissions ;
25
23
return models . Project . findOne ( {
26
24
where : {
27
25
id : projectId ,
@@ -35,7 +33,7 @@ module.exports = [
35
33
}
36
34
37
35
if ( ! project . templateId ) {
38
- return Promise . resolve ( true ) ;
36
+ return Promise . resolve ( [ ] ) ;
39
37
}
40
38
41
39
return models . WorkManagementPermission . findAll ( {
@@ -44,20 +42,23 @@ module.exports = [
44
42
} ,
45
43
} ) ;
46
44
} )
47
- . then ( ( allPermissions ) => {
48
- workManagementPermissions = allPermissions ;
49
- return Promise . all ( _ . map ( workManagementPermissions , workManagementPermission =>
50
- util . hasPermissionForProject ( workManagementPermission . permission , req . authUser , projectId ) ) ,
51
- ) ;
52
- } )
53
- . then ( ( accesses ) => {
54
- const allAccess = { } ;
55
- _ . each ( workManagementPermissions , ( p , ind ) => {
56
- if ( accesses [ ind ] ) {
57
- allAccess [ `${ p . policy } ` ] = accesses [ ind ] ;
45
+ . then ( ( workManagementPermissions ) => {
46
+ const allowPermissions = { } ;
47
+
48
+ // find all allowed permissions
49
+ workManagementPermissions . forEach ( ( workManagementPermission ) => {
50
+ const isAllowed = util . hasPermission (
51
+ workManagementPermission . permission ,
52
+ req . authUser ,
53
+ req . context . currentProjectMembers ,
54
+ ) ;
55
+
56
+ if ( isAllowed ) {
57
+ allowPermissions [ workManagementPermission . policy ] = true ;
58
58
}
59
59
} ) ;
60
- res . json ( util . wrapResponse ( req . id , allAccess ) ) ;
60
+
61
+ res . json ( util . wrapResponse ( req . id , allowPermissions ) ) ;
61
62
} )
62
63
. catch ( next ) ;
63
64
} ,
0 commit comments