diff --git a/server/api/__snapshots__/collections.test.js.snap b/server/api/__snapshots__/collections.test.js.snap index 990719b31a..c3ddf53d1b 100644 --- a/server/api/__snapshots__/collections.test.js.snap +++ b/server/api/__snapshots__/collections.test.js.snap @@ -62,6 +62,15 @@ Object { } `; +exports[`#collections.memberships should require authentication 1`] = ` +Object { + "error": "authentication_required", + "message": "Authentication required", + "ok": false, + "status": 401, +} +`; + exports[`#collections.remove_user should require user in team 1`] = ` Object { "error": "authorization_error", diff --git a/server/api/collections.js b/server/api/collections.js index 88d77984ae..6e8f0e0f17 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -3,7 +3,12 @@ import fs from 'fs'; import Router from 'koa-router'; import auth from '../middlewares/authentication'; import pagination from './middlewares/pagination'; -import { presentCollection, presentUser, presentPolicies } from '../presenters'; +import { + presentCollection, + presentUser, + presentPolicies, + presentMembership, +} from '../presenters'; import { Collection, CollectionUser, Team, Event, User } from '../models'; import { ValidationError } from '../errors'; import { exportCollections } from '../logistics'; @@ -124,6 +129,7 @@ router.post('collections.remove_user', auth(), async ctx => { }; }); +// DEPRECATED: Use collection.memberships which has pagination and permissions router.post('collections.users', auth(), async ctx => { const { id } = ctx.body; ctx.assertUuid(id, 'id is required'); @@ -138,6 +144,37 @@ router.post('collections.users', auth(), async ctx => { }; }); +router.post('collections.memberships', auth(), pagination(), async ctx => { + const { id } = ctx.body; + ctx.assertUuid(id, 'id is required'); + + const collection = await Collection.findByPk(id); + authorize(ctx.state.user, 'read', collection); + + const memberships = await CollectionUser.findAll({ + where: { + collectionId: id, + }, + order: [['createdAt', 'DESC']], + offset: ctx.state.pagination.offset, + limit: ctx.state.pagination.limit, + include: [ + { + model: User, + as: 'user', + }, + ], + }); + + ctx.body = { + pagination: ctx.state.pagination, + data: { + memberships: memberships.map(presentMembership), + users: memberships.map(membership => presentUser(membership.user)), + }, + }; +}); + router.post('collections.export', auth(), async ctx => { const { id } = ctx.body; ctx.assertUuid(id, 'id is required'); diff --git a/server/api/collections.test.js b/server/api/collections.test.js index e273d13ad4..94621cec16 100644 --- a/server/api/collections.test.js +++ b/server/api/collections.test.js @@ -3,7 +3,7 @@ import TestServer from 'fetch-test-server'; import app from '../app'; import { flushdb, seed } from '../test/support'; import { buildUser, buildCollection } from '../test/factories'; -import { Collection } from '../models'; +import { Collection, CollectionUser } from '../models'; const server = new TestServer(app.callback()); beforeEach(flushdb); @@ -272,12 +272,22 @@ describe('#collections.remove_user', async () => { }); describe('#collections.users', async () => { - it('should return members in private collection', async () => { + it('should return users in private collection', async () => { const { collection, user } = await seed(); + await CollectionUser.create({ + createdById: user.id, + collectionId: collection.id, + userId: user.id, + permission: 'read_write', + }); + const res = await server.post('/api/collections.users', { body: { token: user.getJwtToken(), id: collection.id }, }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); }); it('should require authentication', async () => { @@ -298,6 +308,46 @@ describe('#collections.users', async () => { }); }); +describe('#collections.memberships', async () => { + it('should return members in private collection', async () => { + const { collection, user } = await seed(); + await CollectionUser.create({ + createdById: user.id, + collectionId: collection.id, + userId: user.id, + permission: 'read_write', + }); + + const res = await server.post('/api/collections.memberships', { + body: { token: user.getJwtToken(), id: collection.id }, + }); + const body = await res.json(); + + expect(res.status).toEqual(200); + expect(body.data.users.length).toEqual(1); + expect(body.data.users[0].id).toEqual(user.id); + expect(body.data.memberships.length).toEqual(1); + expect(body.data.memberships[0].permission).toEqual('read_write'); + }); + + it('should require authentication', async () => { + const res = await server.post('/api/collections.memberships'); + const body = await res.json(); + + expect(res.status).toEqual(401); + expect(body).toMatchSnapshot(); + }); + + it('should require authorization', async () => { + const { collection } = await seed(); + const user = await buildUser(); + const res = await server.post('/api/collections.memberships', { + body: { token: user.getJwtToken(), id: collection.id }, + }); + expect(res.status).toEqual(403); + }); +}); + describe('#collections.info', async () => { it('should return collection', async () => { const { user, collection } = await seed(); diff --git a/server/pages/developers/Api.js b/server/pages/developers/Api.js index 50bcc38026..29ea5e70b2 100644 --- a/server/pages/developers/Api.js +++ b/server/pages/developers/Api.js @@ -228,12 +228,16 @@ export default function Api() { - + - This method allows you to list users with access to a private - collection. + This method allows you to list a collections memberships. This is + both a collections maintainers, and user permissions for read and + write if the collection is private - + diff --git a/server/presenters/index.js b/server/presenters/index.js index 17a0b074fd..7bf172beef 100644 --- a/server/presenters/index.js +++ b/server/presenters/index.js @@ -9,6 +9,7 @@ import presentApiKey from './apiKey'; import presentShare from './share'; import presentTeam from './team'; import presentIntegration from './integration'; +import presentMembership from './membership'; import presentNotificationSetting from './notificationSetting'; import presentSlackAttachment from './slackAttachment'; import presentPolicies from './policy'; @@ -24,6 +25,7 @@ export { presentShare, presentTeam, presentIntegration, + presentMembership, presentNotificationSetting, presentSlackAttachment, presentPolicies, diff --git a/server/presenters/membership.js b/server/presenters/membership.js new file mode 100644 index 0000000000..96fce2ff59 --- /dev/null +++ b/server/presenters/membership.js @@ -0,0 +1,14 @@ +// @flow +import { CollectionUser } from '../models'; + +type Membership = { + permission: string, +}; + +export default (membership: CollectionUser): Membership => { + return { + userId: membership.userId, + collectionId: membership.collectionId, + permission: membership.permission, + }; +};