Skip to content

Commit ada92eb

Browse files
authored
Merge pull request #855 from topcoder-platform/fix-project-exposing
[Hot Fix] Send project info only for PMs and Admin role users
2 parents 036aa45 + d81d763 commit ada92eb

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ workflows:
149149
context : org-global
150150
filters:
151151
branches:
152-
only: ['develop', 'migration-setup', 'PM-1612']
152+
only: ['develop', 'migration-setup', 'PM-1612', 'fix-project-exposing']
153153
- deployProd:
154154
context : org-global
155155
filters:

src/routes/copilotOpportunity/get.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { USER_ROLE } from '../../constants';
12
import models from '../../models';
23
import util from '../../util';
34

@@ -8,9 +9,11 @@ module.exports = [
89
return util.handleError('Invalid opportunity ID', null, req, next, 400);
910
}
1011

12+
const isAdminOrManager = util.hasRoles(req, [USER_ROLE.CONNECT_ADMIN, USER_ROLE.TOPCODER_ADMIN, USER_ROLE.PROJECT_MANAGER]);
13+
1114
return models.CopilotOpportunity.findOne({
1215
where: { id },
13-
include: [
16+
include: isAdminOrManager ? [
1417
{
1518
model: models.CopilotRequest,
1619
as: 'copilotRequest',
@@ -27,24 +30,36 @@ module.exports = [
2730
},
2831
]
2932
},
33+
]: [
34+
{
35+
model: models.CopilotRequest,
36+
as: 'copilotRequest',
37+
},
3038
],
3139
})
3240
.then((copilotOpportunity) => {
3341
const plainOpportunity = copilotOpportunity.get({ plain: true });
34-
const memberIds = plainOpportunity.project.members && plainOpportunity.project.members.map((member) => member.userId);
42+
const memberIds = (plainOpportunity.project && plainOpportunity.project.members && plainOpportunity.project.members.map((member) => member.userId)) || [];
3543
let canApplyAsCopilot = false;
3644
if (req.authUser) {
3745
canApplyAsCopilot = !memberIds.includes(req.authUser.userId)
3846
}
39-
// This shouldn't be exposed to the clientside
40-
delete plainOpportunity.project.members;
47+
48+
if (plainOpportunity.project) {
49+
// This shouldn't be exposed to the clientside
50+
delete plainOpportunity.project.members;
51+
}
4152
const formattedOpportunity = Object.assign({
4253
members: memberIds,
4354
canApplyAsCopilot,
4455
}, plainOpportunity,
4556
plainOpportunity.copilotRequest ? plainOpportunity.copilotRequest.data : {},
4657
{ copilotRequest: undefined },
4758
);
59+
60+
if (!isAdminOrManager) {
61+
delete formattedOpportunity.projectId;
62+
}
4863
res.json(formattedOpportunity);
4964
})
5065
.catch((err) => {

src/routes/copilotOpportunity/list.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import _ from 'lodash';
22

33
import models from '../../models';
44
import util from '../../util';
5-
import DEFAULT_PAGE_SIZE from '../../constants';
5+
import DEFAULT_PAGE_SIZE, { USER_ROLE } from '../../constants';
66

77
module.exports = [
88
(req, res, next) => {
@@ -15,6 +15,7 @@ module.exports = [
1515
return util.handleError('Invalid sort criteria', null, req, next);
1616
}
1717
const sortParams = sort.split(' ');
18+
const isAdminOrManager = util.hasRoles(req, [USER_ROLE.CONNECT_ADMIN, USER_ROLE.TOPCODER_ADMIN, USER_ROLE.PROJECT_MANAGER]);
1819

1920
// Extract pagination parameters
2021
const page = parseInt(req.query.page, 10) || 1;
@@ -42,7 +43,7 @@ module.exports = [
4243
baseOrder.push([sortParams[0], sortParams[1]]);
4344

4445
return models.CopilotOpportunity.findAll({
45-
include: [
46+
include: isAdminOrManager ?[
4647
{
4748
model: models.CopilotRequest,
4849
as: 'copilotRequest',
@@ -52,6 +53,11 @@ module.exports = [
5253
as: 'project',
5354
attributes: ['name'],
5455
},
56+
] : [
57+
{
58+
model: models.CopilotRequest,
59+
as: 'copilotRequest',
60+
}
5561
],
5662
order: baseOrder,
5763
limit,
@@ -60,10 +66,18 @@ module.exports = [
6066
.then((copilotOpportunities) => {
6167
const formattedOpportunities = copilotOpportunities.map((opportunity) => {
6268
const plainOpportunity = opportunity.get({ plain: true });
63-
return Object.assign({}, plainOpportunity,
69+
70+
const formatted = Object.assign({}, plainOpportunity,
6471
plainOpportunity.copilotRequest ? plainOpportunity.copilotRequest.data : {},
6572
{ copilotRequest: undefined },
6673
);
74+
75+
// For users who are not admin or manager, we dont want to expose
76+
// the project id
77+
if (!isAdminOrManager) {
78+
delete formatted.projectId;
79+
}
80+
return formatted;
6781
});
6882
return util.setPaginationHeaders(req, res, {
6983
count: copilotOpportunities.count,

src/routes/copilotRequest/list.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Op, Sequelize } from 'sequelize';
44
import models from '../../models';
55
import util from '../../util';
66
import { PERMISSION } from '../../permissions/constants';
7-
import { DEFAULT_PAGE_SIZE } from '../../constants';
7+
import { DEFAULT_PAGE_SIZE, USER_ROLE } from '../../constants';
88

99
module.exports = [
1010
(req, res, next) => {
@@ -17,6 +17,8 @@ module.exports = [
1717
return next(err);
1818
}
1919

20+
const isAdminOrManager = util.hasRoles(req, [USER_ROLE.CONNECT_ADMIN, USER_ROLE.TOPCODER_ADMIN, USER_ROLE.PROJECT_MANAGER]);
21+
2022
const page = parseInt(req.query.page, 10) || 1;
2123
const pageSize = parseInt(req.query.pageSize, 10) || DEFAULT_PAGE_SIZE;
2224
const offset = (page - 1) * pageSize;
@@ -46,7 +48,7 @@ module.exports = [
4648
let order = [[sortParams[0], sortParams[1]]];
4749
const relationBasedSortParams = ['projectName'];
4850
const jsonBasedSortParams = ['opportunityTitle', 'projectType'];
49-
if (relationBasedSortParams.includes(sortParams[0])) {
51+
if (relationBasedSortParams.includes(sortParams[0]) && isAdminOrManager) {
5052
order = [
5153
[{model: models.Project, as: 'project'}, 'name', sortParams[1]],
5254
['id', 'DESC']
@@ -64,9 +66,11 @@ module.exports = [
6466

6567
return models.CopilotRequest.findAndCountAll({
6668
where: whereCondition,
67-
include: [
69+
include: isAdminOrManager ? [
6870
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false },
6971
{ model: models.Project, as: 'project', required: false },
72+
] : [
73+
{ model: models.CopilotOpportunity, as: 'copilotOpportunity', required: false },
7074
],
7175
order,
7276
limit: pageSize,

0 commit comments

Comments
 (0)