Skip to content
Snippets Groups Projects
Commit cfa36be9 authored by Sebastian's avatar Sebastian
Browse files

feat(component-invite): add authsome to he teams

parent fff2450e
No related branches found
No related tags found
No related merge requests found
...@@ -67,7 +67,7 @@ module.exports = async ( ...@@ -67,7 +67,7 @@ module.exports = async (
try { try {
let user = await models.User.findByEmail(email) let user = await models.User.findByEmail(email)
let team = teamHelper.getTeamByGroupAndCollection( let team = await teamHelper.getTeamByGroupAndCollection(
collectionId, collectionId,
role, role,
models.Team, models.Team,
...@@ -79,11 +79,13 @@ module.exports = async ( ...@@ -79,11 +79,13 @@ module.exports = async (
collectionId, collectionId,
role, 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) { if (user.invitations === undefined) {
user = await inviteHelper.setupInvitation( user = await inviteHelper.setupInvitation(
user, user,
......
...@@ -74,7 +74,7 @@ const setupEiCTeams = async (models, user) => { ...@@ -74,7 +74,7 @@ const setupEiCTeams = async (models, user) => {
const setupManuscriptTeam = async (models, user, collectionId, role) => { const setupManuscriptTeam = async (models, user, collectionId, role) => {
const teams = await models.Team.all() const teams = await models.Team.all()
user.teams = [] user.teams = user.teams || []
const filteredTeams = teams.filter( const filteredTeams = teams.filter(
team => team =>
team.group === role && team.group === role &&
...@@ -89,12 +89,16 @@ const setupManuscriptTeam = async (models, user, collectionId, role) => { ...@@ -89,12 +89,16 @@ const setupManuscriptTeam = async (models, user, collectionId, role) => {
try { try {
team = await team.updateProperties(team) team = await team.updateProperties(team)
team = await team.save() team = await team.save()
user.teams.push(team.id)
await user.save()
return team return team
} catch (e) { } catch (e) {
logger.error(e) logger.error(e)
} }
} else { } else {
const team = await createNewTeam(collectionId, role, user.id, models.Team) const team = await createNewTeam(collectionId, role, user.id, models.Team)
user.teams.push(team.id)
await user.save()
return team return team
} }
} }
......
...@@ -3,30 +3,34 @@ const pickBy = require('lodash/pickBy') ...@@ -3,30 +3,34 @@ const pickBy = require('lodash/pickBy')
const omit = require('lodash/omit') const omit = require('lodash/omit')
async function teamPermissions(user, operation, object, context) { async function teamPermissions(user, operation, object, context) {
const collection = get(object, 'collection') const heTeamsProm = user.teams
.map(async teamId => {
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 team = await context.models.Team.find(teamId) const team = await context.models.Team.find(teamId)
if (team.teamType.permissions === 'handlingEditor') {
if ( return team
team.teamType.permissions === 'handlingEditor' &&
team.object.id === collection.id &&
operation === 'GET'
) {
console.log('team', team)
return true
} }
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) { function unauthenticatedUser(operation, object) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment