@@ -102,6 +102,56 @@ const buildEsFullTextQuery = (keyword, matchType, singleFieldName) => {
102102 } ;
103103} ;
104104
105+ /**
106+ * Build ES query search request body based on userId and email
107+ *
108+ * @param {String } userId the user id
109+ * @param {String } email the email
110+ * @return {Array } query
111+ */
112+ const buildEsShouldQuery = ( userId , email ) => {
113+ const should = [ ] ;
114+ if ( userId ) {
115+ should . push ( {
116+ nested : {
117+ path : 'members' ,
118+ query : {
119+ query_string : {
120+ query : userId ,
121+ fields : [ 'members.userId' ] ,
122+ } ,
123+ } ,
124+ } ,
125+ } ) ;
126+ should . push ( {
127+ nested : {
128+ path : 'invites' ,
129+ query : {
130+ query_string : {
131+ query : userId ,
132+ fields : [ 'invites.userId' ] ,
133+ } ,
134+ } ,
135+ } ,
136+ } ) ;
137+ }
138+
139+ if ( email ) {
140+ should . push ( {
141+ nested : {
142+ path : 'invites' ,
143+ query : {
144+ query_string : {
145+ query : email ,
146+ fields : [ 'invites.email' ] ,
147+ } ,
148+ } ,
149+ } ,
150+ } ) ;
151+ }
152+ return should ;
153+ } ;
154+
105155/**
106156 * Build ES query search request body based on value, keyword, matchType and fieldName
107157 *
@@ -234,6 +284,7 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {
234284 // prepare the elasticsearch filter criteria
235285 const boolQuery = [ ] ;
236286 let mustQuery = [ ] ;
287+ let shouldQuery = [ ] ;
237288 let fullTextQuery ;
238289 if ( _ . has ( criteria , 'filters.id.$in' ) ) {
239290 boolQuery . push ( {
@@ -269,6 +320,10 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {
269320 [ 'members.firstName' , 'members.lastName' ] ) ) ;
270321 }
271322
323+ if ( _ . has ( criteria , 'filters.userId' ) || _ . has ( criteria , 'filters.email' ) ) {
324+ shouldQuery = buildEsShouldQuery ( criteria . filters . userId , criteria . filters . email ) ;
325+ }
326+
272327 if ( _ . has ( criteria , 'filters.status.$in' ) ) {
273328 // status is an array
274329 boolQuery . push ( {
@@ -348,14 +403,29 @@ const parseElasticSearchCriteria = (criteria, fields, order) => {
348403 must : mustQuery ,
349404 } ) ;
350405 }
406+
407+ if ( shouldQuery . length > 0 ) {
408+ const newBody = { query : { bool : { must : [ ] } } } ;
409+ newBody . query . bool . must . push ( {
410+ bool : {
411+ should : shouldQuery ,
412+ } ,
413+ } ) ;
414+ if ( mustQuery . length > 0 || boolQuery . length > 0 ) {
415+ newBody . query . bool . must . push ( body . query ) ;
416+ }
417+
418+ body . query = newBody . query ;
419+ }
420+
351421 if ( fullTextQuery ) {
352422 body . query = _ . merge ( body . query , fullTextQuery ) ;
353423 if ( body . query . bool ) {
354424 body . query . bool . minimum_should_match = 1 ;
355425 }
356426 }
357427
358- if ( fullTextQuery || boolQuery . length > 0 || mustQuery . length > 0 ) {
428+ if ( fullTextQuery || boolQuery . length > 0 || mustQuery . length > 0 || shouldQuery . length > 0 ) {
359429 searchCriteria . body = body ;
360430 }
361431 return searchCriteria ;
@@ -427,7 +497,6 @@ module.exports = [
427497 offset : req . query . offset || 0 ,
428498 } ;
429499 req . log . info ( criteria ) ;
430-
431500 if ( ! memberOnly
432501 && ( util . hasAdminRole ( req )
433502 || util . hasRoles ( req , MANAGER_ROLES ) ) ) {
@@ -437,32 +506,11 @@ module.exports = [
437506 . catch ( err => next ( err ) ) ;
438507 }
439508
440- // regular users can only see projects they are members of (or invited, handled bellow)
441- const getProjectIds = models . ProjectMember . getProjectIdsForUser ( req . authUser . userId ) ;
442-
443- return getProjectIds
444- . then ( ( accessibleProjectIds ) => {
445- const allowedProjectIds = accessibleProjectIds ;
446- // get projects with pending invite for current user
447- const invites = models . ProjectMemberInvite . getProjectInvitesForUser (
448- req . authUser . email ,
449- req . authUser . userId ) ;
450-
451- return invites . then ( ( ids => _ . union ( allowedProjectIds , ids ) ) ) ;
452- } )
453- . then ( ( allowedProjectIds ) => {
454- // filter based on accessible
455- if ( _ . get ( criteria . filters , 'id' , null ) ) {
456- criteria . filters . id . $in = _ . intersection (
457- allowedProjectIds ,
458- criteria . filters . id . $in ,
459- ) ;
460- } else {
461- criteria . filters . id = { $in : allowedProjectIds } ;
462- }
463- return retrieveProjects ( req , criteria , sort , req . query . fields ) ;
464- } )
465- . then ( result => res . json ( util . wrapResponse ( req . id , result . rows , result . count ) ) )
466- . catch ( err => next ( err ) ) ;
509+ // regular users can only see projects they are members of (or invited, handled below)
510+ criteria . filters . email = req . authUser . email ;
511+ criteria . filters . userId = req . authUser . userId ;
512+ return retrieveProjects ( req , criteria , sort , req . query . fields )
513+ . then ( result => res . json ( util . wrapResponse ( req . id , result . rows , result . count ) ) )
514+ . catch ( err => next ( err ) ) ;
467515 } ,
468516] ;
0 commit comments