Skip to content

Commit

Permalink
feat: filter mango queries (find) for mine and by groups
Browse files Browse the repository at this point in the history
  • Loading branch information
stropitek committed Jun 8, 2022
1 parent fd43522 commit 8ef41d5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
11 changes: 9 additions & 2 deletions src/couch/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const debug = require('debug')('main:find');

const { getUserGroups } = require('../util/groups');

const validateMethods = require('./validate');

const methods = {
Expand All @@ -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,
};
Expand Down
27 changes: 8 additions & 19 deletions src/couch/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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 = [];
Expand Down
28 changes: 28 additions & 0 deletions src/util/groups.js
Original file line number Diff line number Diff line change
@@ -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,
};
24 changes: 24 additions & 0 deletions test/unit/mangoQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 8ef41d5

Please sign in to comment.