1 import * as express from 'express'
2 import { param, query, body } from 'express-validator/check'
3 import { logger } from '../../helpers/logger'
4 import { areValidationErrors } from './utils'
5 import { isPluginNameValid, isPluginTypeValid, isPluginVersionValid, isNpmPluginNameValid } from '../../helpers/custom-validators/plugins'
6 import { PluginManager } from '../../lib/plugins/plugin-manager'
7 import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc'
8 import { PluginModel } from '../../models/server/plugin'
9 import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
11 const servePluginStaticDirectoryValidator = [
12 param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
13 param('pluginVersion').custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
14 param('staticEndpoint').custom(isSafePath).withMessage('Should have a valid static endpoint'),
16 (req: express.Request, res: express.Response, next: express.NextFunction) => {
17 logger.debug('Checking servePluginStaticDirectory parameters', { parameters: req.params })
19 if (areValidationErrors(req, res)) return
21 const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(req.params.pluginName)
23 if (!plugin || plugin.version !== req.params.pluginVersion) {
24 return res.sendStatus(404)
27 res.locals.registeredPlugin = plugin
33 const listPluginsValidator = [
36 .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'),
40 .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'),
42 (req: express.Request, res: express.Response, next: express.NextFunction) => {
43 logger.debug('Checking listPluginsValidator parameters', { parameters: req.query })
45 if (areValidationErrors(req, res)) return
51 const installPluginValidator = [
54 .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
57 .custom(isSafePath).withMessage('Should have a valid safe path'),
59 (req: express.Request, res: express.Response, next: express.NextFunction) => {
60 logger.debug('Checking installPluginValidator parameters', { parameters: req.body })
62 if (areValidationErrors(req, res)) return
64 const body: InstallPlugin = req.body
65 if (!body.path && !body.npmName) {
66 return res.status(400)
67 .json({ error: 'Should have either a npmName or a path' })
75 const uninstallPluginValidator = [
76 body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
78 (req: express.Request, res: express.Response, next: express.NextFunction) => {
79 logger.debug('Checking uninstallPluginValidator parameters', { parameters: req.body })
81 if (areValidationErrors(req, res)) return
87 const existingPluginValidator = [
88 param('npmName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'),
90 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
91 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.params })
93 if (areValidationErrors(req, res)) return
95 const plugin = await PluginModel.loadByNpmName(req.params.npmName)
97 return res.status(404)
98 .json({ error: 'Plugin not found' })
102 res.locals.plugin = plugin
108 const updatePluginSettingsValidator = [
109 body('settings').exists().withMessage('Should have settings'),
111 (req: express.Request, res: express.Response, next: express.NextFunction) => {
112 logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body })
114 if (areValidationErrors(req, res)) return
120 // ---------------------------------------------------------------------------
123 servePluginStaticDirectoryValidator,
124 updatePluginSettingsValidator,
125 uninstallPluginValidator,
126 existingPluginValidator,
127 installPluginValidator,