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

add roles in config, refactor invite

parent e5987057
No related branches found
No related tags found
No related merge requests found
...@@ -5,7 +5,9 @@ const crypto = require('crypto') ...@@ -5,7 +5,9 @@ const crypto = require('crypto')
const mailService = require('pubsweet-component-mail-service') const mailService = require('pubsweet-component-mail-service')
const get = require('lodash/get') const get = require('lodash/get')
const pick = require('lodash/pick') const pick = require('lodash/pick')
const config = require('config')
const configRoles = config.get('roles')
const Invite = app => { const Invite = app => {
app.use(bodyParser.json()) app.use(bodyParser.json())
const authBearer = app.locals.passport.authenticate('bearer', { const authBearer = app.locals.passport.authenticate('bearer', {
...@@ -19,42 +21,33 @@ const Invite = app => { ...@@ -19,42 +21,33 @@ const Invite = app => {
return return
} }
const hasInviteRight = existingRole =>
configRoles.inviteRights[existingRole].includes(role)
const collectionId = get(req, 'params.collectionId') const collectionId = get(req, 'params.collectionId')
const reqUser = await app.locals.models.User.find(req.user) const reqUser = await app.locals.models.User.find(req.user)
let collection let collection
if (collectionId) { if (collectionId && reqUser.roles !== undefined) {
if (!configRoles.collection.includes(role)) {
res
.status(400)
.json({ error: `Role ${role} cannot be set on collections` })
logger.error(`invitation has been attempted with invalid role: ${role}`)
return
}
if (!reqUser.roles.some(hasInviteRight)) {
res.status(403).json({
error: `${reqUser.roles} cannot invite a ${role}`,
})
logger.error(`incorrect role when inviting a user`)
return
}
try { try {
if (role !== 'reviewer' && role !== 'handlingEditor') {
res.status(400).json({ error: 'Role does not exist for collections' })
logger.error(
`invitation has been attempted with invalid role: ${role}`,
)
return
}
if (reqUser.roles === undefined) {
res
.status(403)
.json({ error: 'Only HE or EiC can invite users to collection' })
logger.error(`request user does not have any defined roles`)
return
}
if (role === 'reviewer' && !reqUser.roles.includes('handlingEditor')) {
res.status(403).json({ error: 'Only HE can invite reviewers' })
logger.error(`incorrect role when inviting a reviewer`)
return
} else if (
role === 'handlingEditor' &&
!reqUser.roles.includes('editorInChief')
) {
res.status(403).json({ error: 'Only EiC can invite HE' })
logger.error(`incorrect role when inviting a handling editor`)
return
}
collection = await app.locals.models.Collection.find(collectionId) collection = await app.locals.models.Collection.find(collectionId)
} catch (e) { } catch (e) {
if (e.name === 'NotFoundError') { if (e.name === 'NotFoundError') {
res.status(404).json({ error: 'Collection not found' }) res.status(404).json({ error: 'Collection not found' })
logger.error(`invalid collection id when inviting ${role}`) logger.error(`invalid collection id when inviting a ${role}`)
return return
} }
...@@ -62,13 +55,23 @@ const Invite = app => { ...@@ -62,13 +55,23 @@ const Invite = app => {
logger.error(e) logger.error(e)
return return
} }
} else if (role !== 'editorInChief') { } else if (reqUser.admin === true) {
res.status(400).json({ error: 'Collection id is required' }) reqUser.roles = []
logger.error('missing collection id when trying to invite reviewer/HE') reqUser.roles.push('admin') // this should be moved in pubsweet server
} else {
res.status(403).json({
error: `${reqUser.roles ||
'undefined roles'} cannot invite a ${role} without a collection`,
})
logger.error(`request user does not have any defined roles`)
return return
} else if (reqUser.admin !== true) { }
res.status(403).json({ error: 'Only an admin can invite EiC' })
logger.error('non-admin user tried to invite an EiC') if (!reqUser.roles.some(hasInviteRight)) {
res.status(403).json({
error: `${reqUser.roles} cannot invite a ${role}`,
})
logger.error(`incorrect role when inviting a ${role}`)
return return
} }
...@@ -98,6 +101,7 @@ const Invite = app => { ...@@ -98,6 +101,7 @@ const Invite = app => {
lastName, lastName,
affiliation, affiliation,
title, title,
admin: role === 'admin',
} }
let newUser = new app.locals.models.User(userBody) let newUser = new app.locals.models.User(userBody)
newUser = await newUser.save() newUser = await newUser.save()
...@@ -180,30 +184,12 @@ const Invite = app => { ...@@ -180,30 +184,12 @@ const Invite = app => {
'/api/users/invite/password/reset', '/api/users/invite/password/reset',
bodyParser.json(), bodyParser.json(),
async (req, res) => { async (req, res) => {
const { if (!checkForUndefinedParams(req.body)) {
token,
password,
email,
firstName,
lastName,
affiliation,
title,
} = req.body
if (
!checkForUndefinedParams(
token,
password,
email,
firstName,
lastName,
affiliation,
)
) {
res.status(400).json({ error: 'missing required params' }) res.status(400).json({ error: 'missing required params' })
return return
} }
const { password } = req.body
if (password.length < 7) { if (password.length < 7) {
res res
.status(400) .status(400)
...@@ -216,16 +202,16 @@ const Invite = app => { ...@@ -216,16 +202,16 @@ const Invite = app => {
const updateFields = { const updateFields = {
password, password,
firstName, firstName: req.body.firstName,
lastName, lastName: req.body.lastName,
affiliation, affiliation: req.body.affiliation,
title, title: req.body.title,
isConfirmed: true, isConfirmed: true,
} }
const validateResponse = await validateEmailAndToken( const validateResponse = await validateEmailAndToken(
email, req.body.email,
token, req.body.token,
app.locals.models.User, app.locals.models.User,
) )
if (validateResponse.success === false) { if (validateResponse.success === false) {
...@@ -235,6 +221,11 @@ const Invite = app => { ...@@ -235,6 +221,11 @@ const Invite = app => {
return return
} }
if (validateResponse.user.isConfirmed) {
res.status(400).json({ error: 'User is already confirmed' })
return
}
let newUser = Object.assign( let newUser = Object.assign(
validateResponse.user, validateResponse.user,
updateFields, updateFields,
......
...@@ -65,6 +65,15 @@ module.exports = { ...@@ -65,6 +65,15 @@ module.exports = {
process.env.PUBSWEET_INVITE_PASSWORD_RESET_URL || process.env.PUBSWEET_INVITE_PASSWORD_RESET_URL ||
'http://localhost:3000/invite', 'http://localhost:3000/invite',
}, },
roles: {
global: ['admin', 'editorInChief', 'author'],
collection: ['handlingEditor', 'reviewer'],
inviteRights: {
admin: ['admin', 'editorInChief', 'author'],
editorInChief: ['handlingEditor'],
handlingEditor: ['reviewer'],
},
},
publicKeys: [ publicKeys: [
'pubsweet-client', 'pubsweet-client',
'authsome', 'authsome',
......
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