From 8ef41d56eed68196a35d9d04f934e6248d3de0a3 Mon Sep 17 00:00:00 2001 From: Daniel Kostro Date: Tue, 7 Jun 2022 14:02:01 +0200 Subject: [PATCH] feat: filter mango queries (find) for mine and by groups --- src/couch/find.js | 11 +++++++++-- src/couch/query.js | 27 ++++++++------------------- src/util/groups.js | 28 ++++++++++++++++++++++++++++ test/unit/mangoQueries.js | 24 ++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 21 deletions(-) create mode 100644 src/util/groups.js diff --git a/src/couch/find.js b/src/couch/find.js index e69d6378..b4d2982b 100644 --- a/src/couch/find.js +++ b/src/couch/find.js @@ -2,6 +2,8 @@ const debug = require('debug')('main:find'); +const { getUserGroups } = require('../util/groups'); + const validateMethods = require('./validate'); const methods = { @@ -27,8 +29,13 @@ const methods = { if (hasGlobalRight) { query.selector['\\$owners'] = undefined; } else { - const userGroups = await this.getGroupsByRight(user, right); - userGroups.push(user); + const userGroups = await getUserGroups( + this, + user, + right, + options.groups, + options.mine, + ); query.selector['\\$owners'] = { $in: userGroups, }; diff --git a/src/couch/query.js b/src/couch/query.js index 54fc4e28..b96447b5 100644 --- a/src/couch/query.js +++ b/src/couch/query.js @@ -3,6 +3,7 @@ const CouchError = require('../util/CouchError'); const debug = require('../util/debug')('main:query'); const ensureStringArray = require('../util/ensureStringArray'); +const { getUserGroups } = require('../util/groups'); const validateMethods = require('./validate'); @@ -44,25 +45,13 @@ const methods = { return data; } - var userGroups = await this.getGroupsByRight(user, right); - userGroups.push(user); - if (options.groups) { - var groupsToUse = []; - if (!Array.isArray(options.groups)) { - options.groups = [options.groups]; - } - for (var i = 0; i < userGroups.length; i++) { - if (options.groups.indexOf(userGroups[i]) >= 0) { - groupsToUse.push(userGroups[i]); - } - } - userGroups = groupsToUse; - if (userGroups.indexOf(user) === -1 && options.mine) { - userGroups.push(user); - } - } else if (options.mine) { - userGroups = [user]; - } + const userGroups = await getUserGroups( + this, + user, + right, + options.groups, + options.mine, + ); const docIds = new Set(); const data = []; diff --git a/src/util/groups.js b/src/util/groups.js new file mode 100644 index 00000000..05b2330b --- /dev/null +++ b/src/util/groups.js @@ -0,0 +1,28 @@ +'use strict'; + +async function getUserGroups(ctx, user, right, groups, mine) { + var userGroups = await ctx.getGroupsByRight(user, right); + userGroups.push(user); + if (groups) { + var groupsToUse = []; + if (!Array.isArray(groups)) { + groups = [groups]; + } + for (var i = 0; i < userGroups.length; i++) { + if (groups.indexOf(userGroups[i]) >= 0) { + groupsToUse.push(userGroups[i]); + } + } + userGroups = groupsToUse; + if (userGroups.indexOf(user) === -1 && mine) { + userGroups.push(user); + } + } else if (mine) { + userGroups = [user]; + } + return userGroups; +} + +module.exports = { + getUserGroups, +}; diff --git a/test/unit/mangoQueries.js b/test/unit/mangoQueries.js index 07425dd2..c7554518 100644 --- a/test/unit/mangoQueries.js +++ b/test/unit/mangoQueries.js @@ -77,6 +77,30 @@ describe('no rights mango queries', () => { ); }); + test('filter with mine=true', async () => { + const data = await couch.findEntriesByRight('a@a.com', 'read', { + query: { + fields: ['\\$content.x'], + }, + mine: true, + }); + + expect(data.docs).toHaveLength(1); + expect(data.docs).toStrictEqual([{ $content: { x: 3 } }]); + }); + + test('filter with groups', async () => { + const data = await couch.findEntriesByRight('a@a.com', 'read', { + groups: ['groupA'], + query: { + fields: ['\\$content.x'], + }, + }); + + expect(data.docs).toHaveLength(1); + expect(data.docs).toStrictEqual([{ $content: { x: 1 } }]); + }); + test('use an index', async () => { const data = await couch.findEntriesByRight('a@a.com', 'read', { query: {