@@ -3,54 +3,89 @@ import config from 'config';
3
3
import _ from 'lodash' ;
4
4
import { middleware as tcMiddleware } from 'tc-core-library-js' ;
5
5
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' ;
7
8
import lookerSerivce from '../../services/lookerService' ;
8
9
9
10
const permissions = tcMiddleware . permissions ;
10
11
11
12
12
13
module . exports = [
13
- permissions ( 'projectReporting.managers ' ) ,
14
+ permissions ( 'projectReporting.view ' ) ,
14
15
async ( req , res , next ) => {
15
16
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 ;
17
19
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
+ }
18
33
19
34
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
+ }
20
47
// check if auth user has acecss to this project
21
48
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 ) ;
23
50
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
+ }
24
56
if ( ! member && isAdmin ) {
25
57
const token = await util . getM2MToken ( ) ;
26
58
const adminUser = await util . getTopcoderUser ( authUser . userId , token , req . log ) ;
27
- req . log . debug ( adminUser , 'adminUser' ) ;
59
+ req . log . trace ( adminUser , 'adminUser' ) ;
28
60
member = {
29
61
firstName : adminUser . firstName ,
30
62
lastName : adminUser . lastName ,
31
63
userId : adminUser . userId ,
32
64
role : '' ,
33
65
} ;
34
66
}
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
+ }
35
77
// pick the report based on its name
36
78
let result = { } ;
37
- let embedUrl = null ;
38
79
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.` ) ;
49
82
if ( embedUrl ) {
50
83
result = await lookerSerivce . generateEmbedUrl ( req . authUser , project , member , embedUrl ) ;
84
+ } else {
85
+ return res . status ( 404 ) . send ( 'Report not found' ) ;
51
86
}
52
87
53
- req . log . debug ( result ) ;
88
+ req . log . trace ( result ) ;
54
89
return res . status ( 200 ) . json ( result ) ;
55
90
} catch ( err ) {
56
91
req . log . error ( err ) ;
0 commit comments