From cfa36be94d664a556eba3a3d77a29c04ff464a65 Mon Sep 17 00:00:00 2001 From: Sebastian <sebastian.mihalache@thinslices.com> Date: Mon, 26 Mar 2018 16:22:33 +0300 Subject: [PATCH] feat(component-invite): add authsome to he teams --- .../src/controllers/assignCollectionRole.js | 10 +++-- packages/component-invite/src/helpers/Team.js | 6 ++- packages/xpub-faraday/config/authsome.js | 42 ++++++++++--------- 3 files changed, 34 insertions(+), 24 deletions(-) diff --git a/packages/component-invite/src/controllers/assignCollectionRole.js b/packages/component-invite/src/controllers/assignCollectionRole.js index 7471b3c54..e810e9411 100644 --- a/packages/component-invite/src/controllers/assignCollectionRole.js +++ b/packages/component-invite/src/controllers/assignCollectionRole.js @@ -67,7 +67,7 @@ module.exports = async ( try { let user = await models.User.findByEmail(email) - let team = teamHelper.getTeamByGroupAndCollection( + let team = await teamHelper.getTeamByGroupAndCollection( collectionId, role, models.Team, @@ -79,11 +79,13 @@ module.exports = async ( collectionId, role, ) + user = await models.User.findByEmail(email) + } else { + user.teams = user.teams || [] + user.teams.push(team.id) + user = await user.save() } - // getting the updated user from the DB - creating a team also updates the user - user = await models.User.findByEmail(email) - if (user.invitations === undefined) { user = await inviteHelper.setupInvitation( user, diff --git a/packages/component-invite/src/helpers/Team.js b/packages/component-invite/src/helpers/Team.js index 298e0c508..39bbca515 100644 --- a/packages/component-invite/src/helpers/Team.js +++ b/packages/component-invite/src/helpers/Team.js @@ -74,7 +74,7 @@ const setupEiCTeams = async (models, user) => { const setupManuscriptTeam = async (models, user, collectionId, role) => { const teams = await models.Team.all() - user.teams = [] + user.teams = user.teams || [] const filteredTeams = teams.filter( team => team.group === role && @@ -89,12 +89,16 @@ const setupManuscriptTeam = async (models, user, collectionId, role) => { try { team = await team.updateProperties(team) team = await team.save() + user.teams.push(team.id) + await user.save() return team } catch (e) { logger.error(e) } } else { const team = await createNewTeam(collectionId, role, user.id, models.Team) + user.teams.push(team.id) + await user.save() return team } } diff --git a/packages/xpub-faraday/config/authsome.js b/packages/xpub-faraday/config/authsome.js index ecda1b817..978d988ca 100644 --- a/packages/xpub-faraday/config/authsome.js +++ b/packages/xpub-faraday/config/authsome.js @@ -3,30 +3,34 @@ const pickBy = require('lodash/pickBy') const omit = require('lodash/omit') async function teamPermissions(user, operation, object, context) { - const collection = get(object, 'collection') - - if (collection) { - // Go through a user's teams, if they belong to a team that's based around - // this particular collection, check what membership in that team allows - // and return accordingly - - /* eslint-disable */ - for (const teamId of user.teams) { + const heTeamsProm = user.teams + .map(async teamId => { const team = await context.models.Team.find(teamId) - - if ( - team.teamType.permissions === 'handlingEditor' && - team.object.id === collection.id && - operation === 'GET' - ) { - console.log('team', team) - return true + if (team.teamType.permissions === 'handlingEditor') { + return team } + return null + }) + .filter(Boolean) + + const heTeams = await Promise.all(heTeamsProm) + const heCollections = heTeams.map(team => team.object.id) + + if (heCollections.length > 0) { + return { + filter: collections => { + if (collections.length > 0) { + const correctColl = collections.filter(coll => + heCollections.includes(coll.id), + ) + return correctColl + } + return collections + }, } - /* eslint-enable */ } - return false + return {} } function unauthenticatedUser(operation, object) { -- GitLab