Ocelot-Social/src/middleware/permissionsMiddleware.js
Robert Schäfer 803e613c4b Clean way to seed the database
1. Temporarily open another backend with permissions disabled
2. Connect to this backend and run seed data
2018-11-20 21:43:13 +01:00

36 lines
929 B
JavaScript

import { rule, shield, and, or, not, allow } from 'graphql-shield'
const isAuthenticated = rule()(async (parent, args, ctx, info) => {
return ctx.user !== null
})
const isOwner = rule()(async (parent, args, ctx, info) => {
return ctx.user.id === parent.id
})
const isAdmin = rule()(async (parent, args, ctx, info) => {
return ctx.user.role === 'ADMIN'
})
const isModerator = rule()(async (parent, args, ctx, info) => {
return ctx.user.role === 'MODERATOR'
})
// Permissions
const permissions = shield({
Query: {
statistics: isAdmin,
// fruits: and(isAuthenticated, or(isAdmin, isModerator)),
// customers: and(isAuthenticated, isAdmin)
},
Mutation: {
// addFruitToBasket: isAuthenticated
CreateUser: allow
},
// TODO: re-activate this after fixing the initial seed
// User: {
// email: isOwner,
// password: isOwner
// },
Post: isAuthenticated
})
export default permissions