@@ -8,7 +8,7 @@ const { report, report_status, report_summary } = require('../models');
88const CONSTANTS = require ( '../resources/constants.json' ) ;
99const { formatApiResponse } = require ( '../helpers/responseFormatter' ) ;
1010const { validateAccessPath, matchAccessPath, accessPathForPrivateReports, isCreatorOfReport, roleBasedAccess } = require ( './accessPaths' ) ;
11- const { getDatasets, isReportParameterized, populateReportsWithParameters } = require ( './parameters' ) ;
11+ const { getDatasets, isReportParameterized, populateReportsWithParameters, setFrameworkCategoryParameters } = require ( './parameters' ) ;
1212const { fetchAndFormatExhaustDataset } = require ( '../helpers/dataServiceHelper' ) ;
1313
1414// checks by reportid if the report exists in our database or not
@@ -38,6 +38,7 @@ const search = async (req, res, next) => {
3838 } ) ;
3939
4040 const userDetails = req . userDetails ;
41+ await setFrameworkCategoryParameters ( req , userDetails ) ;
4142 const documents = populateReportsWithParameters ( rows , userDetails ) ;
4243
4344 //is accesspath is provided as search filter create a closure to filter reports
@@ -61,24 +62,40 @@ const search = async (req, res, next) => {
6162 2- is user report admin or not.
6263 3 - check access path for private and protected reports.
6364 */
64- filteredReports = _ . filter ( documents , row => {
65+ // Process all documents in parallel to check access
66+ const reportAccessChecks = await Promise . all ( documents . map ( async row => {
6567 const isCreator = isCreatorOfReport ( { user : userDetails , report : row } ) ;
66- if ( isCreator ) return true ;
68+ if ( isCreator ) return { row , hasAccess : true } ;
6769
68- if ( ! roleBasedAccess ( { report : row , user : userDetails } ) ) return false ;
70+ if ( ! roleBasedAccess ( { report : row , user : userDetails } ) ) {
71+ return { row, hasAccess : false } ;
72+ }
6973
7074 if ( accessPathMatchClosure ) {
7175 const isMatched = accessPathMatchClosure ( row ) ;
72- if ( ! isMatched ) return false ;
76+ if ( ! isMatched ) return { row , hasAccess : false } ;
7377 }
7478
7579 const { type } = row ;
76- if ( ! type ) return false ;
77- if ( type === CONSTANTS . REPORT_TYPE . PUBLIC ) return true ;
78- if ( ( type === CONSTANTS . REPORT_TYPE . PRIVATE ) || ( type === CONSTANTS . REPORT_TYPE . PROTECTED ) ) {
79- return validateAccessPath ( userDetails ) ( row ) ;
80+ if ( ! type ) return { row, hasAccess : false } ;
81+ if ( type === CONSTANTS . REPORT_TYPE . PUBLIC ) return { row, hasAccess : true } ;
82+
83+ if ( type === CONSTANTS . REPORT_TYPE . PRIVATE || type === CONSTANTS . REPORT_TYPE . PROTECTED ) {
84+ try {
85+ const hasAccess = await validateAccessPath ( userDetails , req ) ( row ) ;
86+ return { row, hasAccess } ;
87+ } catch ( error ) {
88+ debug ( 'Error validating access path:' , error ) ;
89+ return { row, hasAccess : false } ;
90+ }
8091 }
81- } ) ;
92+
93+ return { row, hasAccess : false } ;
94+ } ) ) ;
95+
96+ filteredReports = reportAccessChecks
97+ . filter ( ( { hasAccess } ) => hasAccess )
98+ . map ( ( { row } ) => row ) ;
8299 }
83100 return res . status ( 200 ) . json ( formatApiResponse ( { id : req . id , result : { reports : filteredReports , count : filteredReports . length } } ) ) ;
84101 } catch ( error ) {
@@ -229,6 +246,7 @@ const read = async (req, res, next) => {
229246 const userDetails = req . userDetails ;
230247 let document ;
231248 if ( ! hash ) {
249+ await setFrameworkCategoryParameters ( req , userDetails ) ;
232250 [ document ] = populateReportsWithParameters ( [ rawDocument ] , userDetails ) ;
233251 if ( ! document ) return next ( createError ( 401 , CONSTANTS . MESSAGES . FORBIDDEN ) ) ;
234252 } else {
@@ -246,7 +264,7 @@ const read = async (req, res, next) => {
246264 }
247265
248266 if ( ( type === CONSTANTS . REPORT_TYPE . PROTECTED ) || ( type === CONSTANTS . REPORT_TYPE . PRIVATE ) ) {
249- const isAuthorized = validateAccessPath ( userDetails ) ( document ) ;
267+ const isAuthorized = await validateAccessPath ( userDetails , req ) ( document ) ;
250268 if ( ! isAuthorized ) {
251269 return next ( createError ( 401 , CONSTANTS . MESSAGES . FORBIDDEN ) ) ;
252270 }
@@ -528,6 +546,7 @@ const readWithDatasets = async (req, res, next) => {
528546 const user = req . userDetails ;
529547 let document ;
530548 if ( ! hash ) {
549+ await setFrameworkCategoryParameters ( req , userDetails ) ;
531550 [ document ] = populateReportsWithParameters ( [ rawDocument ] , user ) ;
532551 if ( ! document ) return next ( createError ( 401 , CONSTANTS . MESSAGES . FORBIDDEN ) ) ;
533552 } else {
@@ -545,7 +564,7 @@ const readWithDatasets = async (req, res, next) => {
545564 }
546565
547566 if ( ( document . type === CONSTANTS . REPORT_TYPE . PRIVATE ) || ( document . type === CONSTANTS . REPORT_TYPE . PROTECTED ) ) {
548- const isAuthorized = validateAccessPath ( user ) ( document ) ;
567+ const isAuthorized = await validateAccessPath ( user , req ) ( document ) ;
549568 if ( ! isAuthorized ) {
550569 return next ( createError ( 401 , CONSTANTS . MESSAGES . FORBIDDEN ) ) ;
551570 }
0 commit comments