@@ -3,54 +3,89 @@ import config from 'config';
33import _ from 'lodash' ;
44import { middleware as tcMiddleware } from 'tc-core-library-js' ;
55import 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' ;
78import lookerSerivce from '../../services/lookerService' ;
89
910const permissions = tcMiddleware . permissions ;
1011
1112
1213module . 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