From 877f428efce0b90b06f1fdfe4c40511ff141bfba Mon Sep 17 00:00:00 2001 From: Yannis Barlas <yannisbarlas@gmail.com> Date: Tue, 8 Nov 2022 13:36:25 +0200 Subject: [PATCH] fix(middleware): permissions circular dependency --- authorization.js | 15 ++++++++++++++- src/helpers.js | 18 ------------------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/authorization.js b/authorization.js index eb8160b..d6ad847 100644 --- a/authorization.js +++ b/authorization.js @@ -9,7 +9,20 @@ const { not, } = require('graphql-shield') -const { isAdmin, isAuthenticated } = require('./src/helpers') +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 = { rule, diff --git a/src/helpers.js b/src/helpers.js index f2eee5f..03b6962 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -1,5 +1,4 @@ // const axios = require('axios') -const { rule } = require('graphql-shield') const path = require('path') const sharp = require('sharp') const fs = require('fs-extra') @@ -10,21 +9,6 @@ const fs = require('fs-extra') // const services = config.get('services') -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 - }, -) - const convertFileStreamIntoBuffer = async fileStream => { return new Promise((resolve, reject) => { // Store file data chunks @@ -172,8 +156,6 @@ const writeFileFromStream = async (inputStream, filePath) => { // } module.exports = { - isAuthenticated, - isAdmin, convertFileStreamIntoBuffer, getFileExtension, getImageFileMetadata, -- GitLab