@@ -5,6 +5,7 @@ import models from '../../models';
55import { INVITE_STATUS , PROJECT_MEMBER_NON_CUSTOMER_ROLES } from '../../constants' ;
66import util from '../../util' ;
77import { PERMISSION } from '../../permissions/constants' ;
8+ import permissionUtils from '../../utils/permissions' ;
89
910const ES_PROJECT_INDEX = config . get ( 'elasticsearchConfig.indexName' ) ;
1011const ES_PROJECT_TYPE = config . get ( 'elasticsearchConfig.docType' ) ;
@@ -654,17 +655,30 @@ module.exports = [
654655 // so we don't want DB to return unrelated data, ref issue #450
655656 if ( _ . intersection ( _ . keys ( filters ) , SUPPORTED_FILTERS ) . length > 0 ) {
656657 req . log . debug ( 'Don\'t fallback to DB because some filters are defined.' ) ;
657- return util . setPaginationHeaders ( req , res ,
658- util . postProcessInvites ( '$.rows[*].invites[?(@.email)]' , result , req ) ) ;
658+
659+ return result ;
659660 }
660661
661- return retrieveProjectsFromDB ( req , criteria , sort , req . query . fields )
662- . then ( r => util . setPaginationHeaders ( req , res ,
663- util . postProcessInvites ( '$.rows[*].invites[?(@.email)]' , r , req ) ) ) ;
662+ return retrieveProjectsFromDB ( req , criteria , sort , req . query . fields ) ;
664663 }
664+
665665 req . log . debug ( 'Projects found in ES' ) ;
666- return util . setPaginationHeaders ( req , res ,
667- util . postProcessInvites ( '$.rows[*].invites[?(@.email)]' , result , req ) ) ;
666+
667+ return result ;
668+ } ) . then ( ( result ) => {
669+ const postProcessedResult = util . postProcessInvites ( '$.rows[*].invites[?(@.email)]' , result , req ) ;
670+
671+ postProcessedResult . rows . forEach ( ( project ) => {
672+ // filter out attachments which user cannot see
673+ if ( project . attachments ) {
674+ // eslint-disable-next-line no-param-reassign
675+ project . attachments = project . attachments . filter ( attachment =>
676+ permissionUtils . hasReadAccessToAttachment ( attachment , req ) ,
677+ ) ;
678+ }
679+ } ) ;
680+
681+ return util . setPaginationHeaders ( req , res , postProcessedResult ) ;
668682 } )
669683 . catch ( err => next ( err ) ) ;
670684 } ,
0 commit comments