mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
feat(backend): branding middlewares (#8429)
* allow the rbanding to provide middlewares lint fix fix config * whitelist instead o blacklist when to exclude middlewares fix config whitelisting * fix lint
This commit is contained in:
parent
48c7bd0033
commit
9d5396988a
@ -23,6 +23,7 @@ COPY . .
|
|||||||
ONBUILD COPY ./branding/constants/ src/config/tmp
|
ONBUILD COPY ./branding/constants/ src/config/tmp
|
||||||
ONBUILD RUN tools/replace-constants.sh
|
ONBUILD RUN tools/replace-constants.sh
|
||||||
ONBUILD COPY ./branding/email/ src/middleware/helpers/email/
|
ONBUILD COPY ./branding/email/ src/middleware/helpers/email/
|
||||||
|
ONBUILD COPY ./branding/middlewares/ src/middleware/branding/
|
||||||
ONBUILD COPY ./branding/data/ src/db/data
|
ONBUILD COPY ./branding/data/ src/db/data
|
||||||
ONBUILD COPY ./branding/public/ public/
|
ONBUILD COPY ./branding/public/ public/
|
||||||
ONBUILD RUN yarn install --production=false --frozen-lockfile --non-interactive
|
ONBUILD RUN yarn install --production=false --frozen-lockfile --non-interactive
|
||||||
|
|||||||
0
backend/branding/middlewares/.gitkeep
Normal file
0
backend/branding/middlewares/.gitkeep
Normal file
@ -22,7 +22,9 @@ const environment = {
|
|||||||
PRODUCTION: env.NODE_ENV === 'production',
|
PRODUCTION: env.NODE_ENV === 'production',
|
||||||
// used for staging enviroments if 'PRODUCTION=true' and 'PRODUCTION_DB_CLEAN_ALLOW=true'
|
// used for staging enviroments if 'PRODUCTION=true' and 'PRODUCTION_DB_CLEAN_ALLOW=true'
|
||||||
PRODUCTION_DB_CLEAN_ALLOW: env.PRODUCTION_DB_CLEAN_ALLOW === 'true' || false, // default = false
|
PRODUCTION_DB_CLEAN_ALLOW: env.PRODUCTION_DB_CLEAN_ALLOW === 'true' || false, // default = false
|
||||||
DISABLED_MIDDLEWARES: (env.NODE_ENV !== 'production' && env.DISABLED_MIDDLEWARES) || false,
|
DISABLED_MIDDLEWARES: ['test', 'development'].includes(env.NODE_ENV as string)
|
||||||
|
? (env.DISABLED_MIDDLEWARES?.split(',') ?? [])
|
||||||
|
: [],
|
||||||
}
|
}
|
||||||
|
|
||||||
const required = {
|
const required = {
|
||||||
|
|||||||
6
backend/src/middleware/branding/brandingMiddlewares.ts
Normal file
6
backend/src/middleware/branding/brandingMiddlewares.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
// eslint-disable-next-line import/no-cycle
|
||||||
|
import { MiddlewareOrder } from '@middleware/index'
|
||||||
|
|
||||||
|
export default (): MiddlewareOrder[] => {
|
||||||
|
return []
|
||||||
|
}
|
||||||
@ -1,14 +1,14 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
||||||
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
/* eslint-disable @typescript-eslint/restrict-template-expressions */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
||||||
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
||||||
/* eslint-disable security/detect-object-injection */
|
import { applyMiddleware, IMiddleware } from 'graphql-middleware'
|
||||||
import { applyMiddleware } from 'graphql-middleware'
|
|
||||||
|
|
||||||
import CONFIG from '@config/index'
|
import CONFIG from '@config/index'
|
||||||
|
|
||||||
|
// eslint-disable-next-line import/no-cycle
|
||||||
|
import brandingMiddlewares from './branding/brandingMiddlewares'
|
||||||
import chatMiddleware from './chatMiddleware'
|
import chatMiddleware from './chatMiddleware'
|
||||||
import excerpt from './excerptMiddleware'
|
import excerpt from './excerptMiddleware'
|
||||||
import hashtags from './hashtags/hashtagsMiddleware'
|
import hashtags from './hashtags/hashtagsMiddleware'
|
||||||
@ -26,56 +26,44 @@ import userInteractions from './userInteractions'
|
|||||||
import validation from './validation/validationMiddleware'
|
import validation from './validation/validationMiddleware'
|
||||||
import xss from './xssMiddleware'
|
import xss from './xssMiddleware'
|
||||||
|
|
||||||
export default (schema) => {
|
export interface MiddlewareOrder {
|
||||||
const middlewares = {
|
order: number
|
||||||
sentry,
|
name: string
|
||||||
permissions,
|
middleware: IMiddleware
|
||||||
xss,
|
}
|
||||||
validation,
|
|
||||||
sluggify,
|
const ocelotMiddlewares: MiddlewareOrder[] = [
|
||||||
excerpt,
|
{ order: -200, name: 'sentry', middleware: sentry },
|
||||||
login,
|
{ order: -190, name: 'permissions', middleware: permissions },
|
||||||
notifications,
|
{ order: -180, name: 'xss', middleware: xss },
|
||||||
hashtags,
|
{ order: -170, name: 'validation', middleware: validation },
|
||||||
softDelete,
|
{ order: -160, name: 'userInteractions', middleware: userInteractions },
|
||||||
includedFields,
|
{ order: -150, name: 'sluggify', middleware: sluggify },
|
||||||
orderBy,
|
{ order: -140, name: 'languages', middleware: languages },
|
||||||
languages,
|
{ order: -130, name: 'excerpt', middleware: excerpt },
|
||||||
userInteractions,
|
{ order: -120, name: 'login', middleware: login },
|
||||||
chatMiddleware,
|
{ order: -110, name: 'notifications', middleware: notifications },
|
||||||
}
|
{ order: -100, name: 'hashtags', middleware: hashtags },
|
||||||
|
{ order: -90, name: 'softDelete', middleware: softDelete },
|
||||||
let order = [
|
{ order: -80, name: 'includedFields', middleware: includedFields },
|
||||||
'sentry',
|
{ order: -70, name: 'orderBy', middleware: orderBy },
|
||||||
'permissions',
|
{ order: -60, name: 'chatMiddleware', middleware: chatMiddleware },
|
||||||
'xss',
|
]
|
||||||
// 'activityPub', disabled temporarily
|
|
||||||
'validation',
|
export default (schema) => {
|
||||||
'userInteractions',
|
const middlewares = ocelotMiddlewares
|
||||||
'sluggify',
|
.concat(brandingMiddlewares())
|
||||||
'languages',
|
.sort((a, b) => a.order - b.order)
|
||||||
'excerpt',
|
|
||||||
'login',
|
const filteredMiddlewares = middlewares.filter(
|
||||||
'notifications',
|
(middleware) => !CONFIG.DISABLED_MIDDLEWARES.includes(middleware.name),
|
||||||
'hashtags',
|
)
|
||||||
'softDelete',
|
|
||||||
'includedFields',
|
// Warn if we filtered
|
||||||
'orderBy',
|
if (middlewares.length < filteredMiddlewares.length) {
|
||||||
'chatMiddleware',
|
// eslint-disable-next-line no-console
|
||||||
]
|
console.log(`Warning: Disabled "${CONFIG.DISABLED_MIDDLEWARES.join(', ')}" middleware.`)
|
||||||
|
}
|
||||||
// add permisions middleware at the first position (unless we're seeding)
|
|
||||||
if (CONFIG.DISABLED_MIDDLEWARES) {
|
return applyMiddleware(schema, ...filteredMiddlewares.map((middleware) => middleware.middleware))
|
||||||
const disabledMiddlewares = CONFIG.DISABLED_MIDDLEWARES.split(',')
|
|
||||||
order = order.filter((key) => {
|
|
||||||
if (disabledMiddlewares.includes(key)) {
|
|
||||||
/* eslint-disable-next-line no-console */
|
|
||||||
console.log(`Warning: Disabled "${disabledMiddlewares}" middleware.`)
|
|
||||||
}
|
|
||||||
return !disabledMiddlewares.includes(key)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
const appliedMiddlewares = order.map((key) => middlewares[key])
|
|
||||||
return applyMiddleware(schema, ...appliedMiddlewares)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user