Skip to content
Snippets Groups Projects
Commit abe23d86 authored by Jure's avatar Jure
Browse files

Merge branch 'authsome_helper' into 'master'

feat: expand access to custom models in authsome

See merge request pubsweet/pubsweet!446
parents 2632fa8f 8017fca9
No related branches found
No related tags found
No related merge requests found
...@@ -9,7 +9,11 @@ module.exports = async (userId, operation, object, context) => { ...@@ -9,7 +9,11 @@ module.exports = async (userId, operation, object, context) => {
} }
if (operation === 'publishManuscript') { if (operation === 'publishManuscript') {
const isAuthor = object.current.owners.includes(user.id) // Try to fetch the current Manuscript using the context
// to verify that you can, in fact, do it.
const manuscript = await context.models.Manuscript.find(object.current.id)
const isAuthor = manuscript.owners.includes(user.id)
if (isAuthor && user.admin) { if (isAuthor && user.admin) {
return true return true
} else if (isAuthor) { } else if (isAuthor) {
......
...@@ -5,26 +5,27 @@ const mode = require(config.get('authsome.mode')) ...@@ -5,26 +5,27 @@ const mode = require(config.get('authsome.mode'))
const models = require('../models') const models = require('../models')
const authsome = new Authsome( // be lenient with custom/extended data models based on BaseModel
{ ...config.authsome, mode }, // and allow them through to authsome in their entirety. If you use this
{ // you are responsible for providing a similar interface in the client
// restrict methods passed to mode since these have to be shimmed on client // as well - if you want your authsome modes to be usable on both platforms.
// any changes here should be reflected in the `withAuthsome` component of `pubsweet-client` const context = { models: Object.assign({}, models) }
models: {
Collection: { // more restrictive with core models, restrict methods passed to mode since
find: id => models.Collection.find(id), // these have to be shimmed in the client (withAuthsome, AuthorizeWithGraphQL)
}, context.models.Collection = {
Fragment: { find: models.Collection.find.bind(models.Collection),
find: id => models.Fragment.find(id), }
}, context.models.Fragment = {
User: { find: models.Fragment.find.bind(models.Fragment),
find: id => models.User.find(id), }
}, context.models.User = {
Team: { find: models.User.find.bind(models.User),
find: id => models.Team.find(id), }
}, context.models.Team = {
}, find: models.Team.find.bind(models.Team),
}, }
)
const authsome = new Authsome({ ...config.authsome, mode }, context)
module.exports = authsome module.exports = authsome
const config = require('config')
// core models
const models = { const models = {
Collection: './Collection', Collection: './Collection',
Fragment: './Fragment', Fragment: './Fragment',
...@@ -8,3 +11,16 @@ const models = { ...@@ -8,3 +11,16 @@ const models = {
Object.keys(models).forEach((key, _) => { Object.keys(models).forEach((key, _) => {
module.exports[key] = require(models[key]) module.exports[key] = require(models[key])
}) })
const requireRelative = m =>
require(require.resolve(m, { paths: [process.cwd()] }))
// custom data models
if (config.has('pubsweet.components')) {
config.get('pubsweet.components').forEach(componentName => {
const component = requireRelative(componentName)
if (component.modelName) {
module.exports[component.modelName] = component.model
}
})
}
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