Skip to content
Snippets Groups Projects
Commit 18319e8f authored by Yannis Barlas's avatar Yannis Barlas
Browse files

Merge branch 'auth-helpers' into 'master'

feat(middleware): add helpers for authorization middleware

Closes #1

See merge request !2
parents 9773acd3 557dc56d
No related branches found
No related tags found
1 merge request!2feat(middleware): add helpers for authorization middleware
...@@ -9,6 +9,8 @@ const { ...@@ -9,6 +9,8 @@ const {
not, not,
} = require('graphql-shield') } = require('graphql-shield')
const { isAdmin, isAuthenticated } = require('./src/helpers')
module.exports = { module.exports = {
rule, rule,
inputRule, inputRule,
...@@ -18,4 +20,6 @@ module.exports = { ...@@ -18,4 +20,6 @@ module.exports = {
chain, chain,
or, or,
not, not,
isAuthenticated,
isAdmin,
} }
const config = require('config') const config = require('config')
const isEmpty = require('lodash/isEmpty')
const { applyMiddleware } = require('graphql-middleware') const { applyMiddleware } = require('graphql-middleware')
const { shield } = require('graphql-shield') const { shield } = require('graphql-shield')
let schema = require('pubsweet-server/src/graphql/schema') let schema = require('pubsweet-server/src/graphql/schema')
if (config.has('permissions')) { const permissions = config.has('permissions') && config.get('permissions')
schema = applyMiddleware(schema, shield(config.get('permissions')))
if (permissions && !isEmpty(permissions)) {
schema = applyMiddleware(schema, shield(permissions))
} }
module.exports = schema module.exports = schema
const { rule } = require('graphql-shield')
const isAuthenticated = rule()(async (parent, args, ctx, info) => {
return !!ctx.user
})
const isAdmin = rule()(
async (parent, args, { user: userId, connectors: { User } }, info) => {
if (!userId) {
return false
}
const user = await User.model.findById(userId)
return user.admin
},
)
module.exports = {
isAuthenticated,
isAdmin,
}
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