From cdafc3bb40a60f3b8764818048c75fa5bf8f5db0 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 7 Sep 2019 12:21:12 -0700 Subject: [PATCH] fix: More membership scopes --- app/stores/MembershipsStore.js | 4 ++-- server/api/collections.js | 4 +++- server/api/collections.test.js | 21 ++++++++++++++++++++- server/api/documents.js | 4 +++- server/api/documents.test.js | 8 +++++--- server/index.js | 4 +++- server/models/Document.js | 12 ++++++++---- 7 files changed, 44 insertions(+), 13 deletions(-) diff --git a/app/stores/MembershipsStore.js b/app/stores/MembershipsStore.js index a929316bfd..0849f5b487 100644 --- a/app/stores/MembershipsStore.js +++ b/app/stores/MembershipsStore.js @@ -72,11 +72,11 @@ export default class MembershipsStore extends BaseStore { } @action - removeCollectionId(collectionId: string) { + removeCollectionId = (collectionId: string) => { this.data.forEach((membership, key) => { if (key.includes(collectionId)) { this.remove(key); } }); - } + }; } diff --git a/server/api/collections.js b/server/api/collections.js index 799ccdcf67..ffafed4f84 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -229,7 +229,9 @@ router.post('collections.export', auth(), async ctx => { ctx.assertUuid(id, 'id is required'); const user = ctx.state.user; - const collection = await Collection.findByPk(id); + const collection = await Collection.scope({ + method: ['withMembership', user.id], + }).findByPk(id); authorize(user, 'export', collection); const filePath = await archiveCollection(collection); diff --git a/server/api/collections.test.js b/server/api/collections.test.js index ddf2af75cc..2c54b8a45c 100644 --- a/server/api/collections.test.js +++ b/server/api/collections.test.js @@ -68,7 +68,7 @@ describe('#collections.list', async () => { }); describe('#collections.export', async () => { - it('should require user to be a member', async () => { + it('should now allow export of private collection not a member', async () => { const { user } = await seed(); const collection = await buildCollection({ private: true, @@ -81,6 +81,25 @@ describe('#collections.export', async () => { expect(res.status).toEqual(403); }); + it('should allow export of private collection', async () => { + const { user, collection } = await seed(); + collection.private = true; + await collection.save(); + + await CollectionUser.create({ + createdById: user.id, + collectionId: collection.id, + userId: user.id, + permission: 'read', + }); + + const res = await server.post('/api/collections.export', { + body: { token: user.getJwtToken(), id: collection.id }, + }); + + expect(res.status).toEqual(200); + }); + it('should require authentication', async () => { const res = await server.post('/api/collections.export'); const body = await res.json(); diff --git a/server/api/documents.js b/server/api/documents.js index 9f14880b01..bac761fc43 100644 --- a/server/api/documents.js +++ b/server/api/documents.js @@ -457,7 +457,9 @@ router.post('documents.search', auth(), pagination(), async ctx => { if (collectionId) { ctx.assertUuid(collectionId, 'collectionId must be a UUID'); - const collection = await Collection.findByPk(collectionId); + const collection = await Collection.scope({ + method: ['withMembership', user.id], + }).findByPk(collectionId); authorize(user, 'read', collection); } diff --git a/server/api/documents.test.js b/server/api/documents.test.js index 81fd820576..15eb748249 100644 --- a/server/api/documents.test.js +++ b/server/api/documents.test.js @@ -665,8 +665,9 @@ describe('#documents.search', async () => { }); it('should return documents for a specific private collection', async () => { - const { user } = await seed(); - const collection = await buildCollection({ private: true }); + const { user, collection } = await seed(); + collection.private = true; + await collection.save(); await CollectionUser.create({ createdById: user.id, @@ -679,13 +680,14 @@ describe('#documents.search', async () => { title: 'search term', text: 'search term', teamId: user.teamId, + collectionId: collection.id, }); const res = await server.post('/api/documents.search', { body: { token: user.getJwtToken(), query: 'search term', - collectionId: document.collectionId, + collectionId: collection.id, }, }); const body = await res.json(); diff --git a/server/index.js b/server/index.js index a130d17b50..21c2256eca 100644 --- a/server/index.js +++ b/server/index.js @@ -53,7 +53,9 @@ if (process.env.WEBSOCKETS_ENABLED === 'true') { // allow the client to request to join rooms based on // new collections being created. socket.on('join', async event => { - const collection = await Collection.findByPk(event.roomId); + const collection = await Collection.scope({ + method: ['withMembership', user.id], + }).findByPk(event.roomId); if (can(user, 'read', collection)) { socket.join(`collection-${event.roomId}`); diff --git a/server/models/Document.js b/server/models/Document.js index e8fa68e077..641ac49c77 100644 --- a/server/models/Document.js +++ b/server/models/Document.js @@ -303,14 +303,18 @@ Document.searchForUser = async ( }); // Final query to get associated document data - const documents = await Document.scope({ - method: ['withViews', user.id], - }).findAll({ + const documents = await Document.scope( + { + method: ['withViews', user.id], + }, + { + method: ['withCollection', user.id], + } + ).findAll({ where: { id: map(results, 'id'), }, include: [ - { model: Collection, as: 'collection' }, { model: User, as: 'createdBy', paranoid: false }, { model: User, as: 'updatedBy', paranoid: false }, ],