Skip to content

Commit cefa5a9

Browse files
author
Vikas Agarwal
committed
Fine tuning the embed report end point to use project category and user role to map the key to report. Also, using env variable to map report to actual looker construct.
1 parent 3794c39 commit cefa5a9

File tree

4 files changed

+60
-23
lines changed

4 files changed

+60
-23
lines changed

config/custom-environment-variables.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@
6363
"CLIENT_ID": "LOOKER_API_CLIENT_ID",
6464
"CLIENT_SECRET": "LOOKER_API_CLIENT_SECRET",
6565
"TOKEN": "TOKEN",
66-
"USE_MOCK": "LOOKER_API_ENABLE_MOCK",
67-
"MOCK_EMBED_REPORT": "MOCK_EMBED_REPORT",
66+
"USE_MOCK": "ENABLE_MOCK_REPORTS",
6867
"QUERIES": {
6968
"REG_STATS": "LOOKER_API_REG_STATS_QUERY_ID",
7069
"BUDGET": "LOOKER_API_BUDGET_QUERY_ID"
71-
}
70+
},
71+
"EMBED_REPORTS_MAPPING": "EMBED_REPORTS_MAPPING",
72+
"ALLOWED_USERS": "REPORTS_ALLOWED_USERS"
7273
},
7374
"DEFAULT_M2M_USERID": "DEFAULT_M2M_USERID"
7475
}

config/default.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@
6969
"CLIENT_SECRET": "",
7070
"TOKEN": "TOKEN",
7171
"USE_MOCK": "true",
72-
"MOCK_EMBED_REPORT": "/embed/looks/2",
7372
"QUERIES": {
7473
"REG_STATS": 1234,
7574
"BUDGET": 123
76-
}
75+
},
76+
"EMBED_REPORTS_MAPPING": "{\"mock\": \"/embed/looks/2\"}",
77+
"ALLOWED_USERS": "[]"
7778
},
7879
"DEFAULT_M2M_USERID": -101
7980
}

src/permissions/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,5 +141,5 @@ module.exports = () => {
141141
Authorizer.setPolicy('projectEstimation.item.list', copilotAndAbove);
142142

143143
// Project Reporting
144-
Authorizer.setPolicy('projectReporting.managers', connectManagerOrAdmin);
144+
Authorizer.setPolicy('projectReporting.view', projectView);
145145
};

src/routes/projectReports/getEmbedReport.js

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,89 @@ import config from 'config';
33
import _ from 'lodash';
44
import { middleware as tcMiddleware } from 'tc-core-library-js';
55
import util from '../../util';
6-
import { USER_ROLE } from '../../constants';
6+
import { USER_ROLE, PROJECT_MEMBER_ROLE, PROJECT_MEMBER_MANAGER_ROLES } from '../../constants';
7+
import models from '../../models';
78
import lookerSerivce from '../../services/lookerService';
89

910
const permissions = tcMiddleware.permissions;
1011

1112

1213
module.exports = [
13-
permissions('projectReporting.managers'),
14+
permissions('projectReporting.view'),
1415
async (req, res, next) => {
1516
const projectId = Number(req.params.projectId);
16-
const reportName = config.lookerConfig.USE_MOCK === 'true' ? 'mock' : req.query.reportName;
17+
const mockReport = config.lookerConfig.USE_MOCK === 'true';
18+
let reportName = mockReport ? 'mock' : req.query.reportName;
1719
const authUser = req.authUser;
20+
let REPORTS = null;
21+
let allowedUsers = null;
22+
try {
23+
allowedUsers = JSON.parse(_.get(config, 'lookerConfig.ALLOWED_USERS', '[]'));
24+
req.log.trace(allowedUsers, 'allowedUsers');
25+
REPORTS = JSON.parse(config.lookerConfig.EMBED_REPORTS_MAPPING);
26+
} catch (error) {
27+
req.log.error(error);
28+
req.log.debug('Invalid reports mapping. Should be a valid JSON.');
29+
}
30+
if (!mockReport && !REPORTS) {
31+
return res.status(404).send('Report not found');
32+
}
1833

1934
try {
35+
if (!mockReport) {
36+
const project = await models.Project.findOne({
37+
where: { id: projectId },
38+
attributes: ['id', 'templateId'],
39+
raw: true,
40+
});
41+
const projectTemplate = project.templateId
42+
? await models.ProjectTemplate.findByPk(project.templateId, { attributes: ['category'], raw: true })
43+
: null;
44+
const projectCategory = _.get(projectTemplate, 'category', '');
45+
reportName = `${reportName}-${projectCategory}`;
46+
}
2047
// check if auth user has acecss to this project
2148
const members = req.context.currentProjectMembers;
22-
let member = _.find(members, m => m.userId === req.authUser.userId);
49+
let member = _.find(members, m => m.userId === authUser.userId);
2350
const isAdmin = util.hasRoles(req, [USER_ROLE.CONNECT_ADMIN, USER_ROLE.TOPCODER_ADMIN]);
51+
const userDisallowed = allowedUsers.length > 0 && !allowedUsers.includes(authUser.userId);
52+
if (userDisallowed) {
53+
req.log.error(`User whitelisting prevented accessing report ${reportName} to ${authUser.userId}`);
54+
return res.status(403).send('User is not allowed to access the report');
55+
}
2456
if (!member && isAdmin) {
2557
const token = await util.getM2MToken();
2658
const adminUser = await util.getTopcoderUser(authUser.userId, token, req.log);
27-
req.log.debug(adminUser, 'adminUser');
59+
req.log.trace(adminUser, 'adminUser');
2860
member = {
2961
firstName: adminUser.firstName,
3062
lastName: adminUser.lastName,
3163
userId: adminUser.userId,
3264
role: '',
3365
};
3466
}
67+
let roleKey = '';
68+
if (!mockReport) {
69+
if ([PROJECT_MEMBER_ROLE.CUSTOMER, PROJECT_MEMBER_ROLE.COPILOT].includes(member.role)) {
70+
roleKey = member.role;
71+
}
72+
if (isAdmin || PROJECT_MEMBER_MANAGER_ROLES.includes(member.role)) {
73+
roleKey = 'topcoder';
74+
}
75+
reportName = `${reportName}-${roleKey}`;
76+
}
3577
// pick the report based on its name
3678
let result = {};
37-
let embedUrl = null;
3879
const project = { id: projectId };
39-
switch (reportName) {
40-
case 'summary':
41-
embedUrl = '/embed/looks/1';
42-
break;
43-
case 'mock':
44-
embedUrl = config.lookerConfig.MOCK_EMBED_REPORT;
45-
break;
46-
default:
47-
return res.status(404).send('Report not found');
48-
}
80+
const embedUrl = REPORTS[reportName];
81+
req.log.trace(`Generating embed URL for ${reportName} report, using ${embedUrl} as embed URL.`);
4982
if (embedUrl) {
5083
result = await lookerSerivce.generateEmbedUrl(req.authUser, project, member, embedUrl);
84+
} else {
85+
return res.status(404).send('Report not found');
5186
}
5287

53-
req.log.debug(result);
88+
req.log.trace(result);
5489
return res.status(200).json(result);
5590
} catch (err) {
5691
req.log.error(err);

0 commit comments

Comments
 (0)