Skip to content

Commit f74d2f5

Browse files
committed
refactor: improve performance of get project permissions endpoint
Instead of using "util.hasPermissionForProject" which requests members list on every call, use "util.hasPermission" with prefetched project members
1 parent 5b3e816 commit f74d2f5

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

src/routes/permissions/get.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/**
22
* API to get project permissions
33
*/
4-
import _ from 'lodash';
54
import validate from 'express-validation';
65
import Joi from 'joi';
76
import { middleware as tcMiddleware } from 'tc-core-library-js';
@@ -21,7 +20,6 @@ module.exports = [
2120
permissions('permissions.view'),
2221
(req, res, next) => {
2322
const projectId = req.params.projectId;
24-
let workManagementPermissions;
2523
return models.Project.findOne({
2624
where: {
2725
id: projectId,
@@ -35,7 +33,7 @@ module.exports = [
3533
}
3634

3735
if (!project.templateId) {
38-
return Promise.resolve(true);
36+
return Promise.resolve([]);
3937
}
4038

4139
return models.WorkManagementPermission.findAll({
@@ -44,20 +42,23 @@ module.exports = [
4442
},
4543
});
4644
})
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;
5858
}
5959
});
60-
res.json(util.wrapResponse(req.id, allAccess));
60+
61+
res.json(util.wrapResponse(req.id, allowPermissions));
6162
})
6263
.catch(next);
6364
},

0 commit comments

Comments
 (0)