From ad91e7006e41f8ee5b8dcefee30f99e8ca44133a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 10 Jul 2019 16:59:53 +0200 Subject: WIP plugins: plugin settings on server side --- server/middlewares/validators/plugins.ts | 89 ++++++++++++++++++++++++++++++-- server/middlewares/validators/sort.ts | 5 +- 2 files changed, 89 insertions(+), 5 deletions(-) (limited to 'server/middlewares/validators') diff --git a/server/middlewares/validators/plugins.ts b/server/middlewares/validators/plugins.ts index fcb461624..265ac7c17 100644 --- a/server/middlewares/validators/plugins.ts +++ b/server/middlewares/validators/plugins.ts @@ -1,10 +1,11 @@ import * as express from 'express' -import { param } from 'express-validator/check' +import { param, query, body } from 'express-validator/check' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' -import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' +import { isPluginNameValid, isPluginTypeValid, isPluginVersionValid, isNpmPluginNameValid } from '../../helpers/custom-validators/plugins' import { PluginManager } from '../../lib/plugins/plugin-manager' -import { isSafePath } from '../../helpers/custom-validators/misc' +import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc' +import { PluginModel } from '../../models/server/plugin' const servePluginStaticDirectoryValidator = [ param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), @@ -28,8 +29,88 @@ const servePluginStaticDirectoryValidator = [ } ] +const listPluginsValidator = [ + query('type') + .optional() + .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), + query('uninstalled') + .optional() + .toBoolean() + .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking listPluginsValidator parameters', { parameters: req.query }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +const installPluginValidator = [ + body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking installPluginValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +const uninstallPluginValidator = [ + body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking managePluginValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +const enabledPluginValidator = [ + body('name').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + const plugin = await PluginModel.load(req.body.name) + if (!plugin) { + return res.status(404) + .json({ error: 'Plugin not found' }) + .end() + } + + res.locals.plugin = plugin + + return next() + } +] + +const updatePluginSettingsValidator = [ + body('settings').exists().withMessage('Should have settings'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + // --------------------------------------------------------------------------- export { - servePluginStaticDirectoryValidator + servePluginStaticDirectoryValidator, + updatePluginSettingsValidator, + uninstallPluginValidator, + enabledPluginValidator, + installPluginValidator, + listPluginsValidator } diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index b497798d1..102db85cb 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -21,6 +21,7 @@ const SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUM const SORTABLE_SERVERS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.SERVERS_BLOCKLIST) const SORTABLE_USER_NOTIFICATIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_NOTIFICATIONS) const SORTABLE_VIDEO_PLAYLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_PLAYLISTS) +const SORTABLE_PLUGINS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.PLUGINS) const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) @@ -41,6 +42,7 @@ const accountsBlocklistSortValidator = checkSort(SORTABLE_ACCOUNTS_BLOCKLIST_COL const serversBlocklistSortValidator = checkSort(SORTABLE_SERVERS_BLOCKLIST_COLUMNS) const userNotificationsSortValidator = checkSort(SORTABLE_USER_NOTIFICATIONS_COLUMNS) const videoPlaylistsSortValidator = checkSort(SORTABLE_VIDEO_PLAYLISTS_COLUMNS) +const pluginsSortValidator = checkSort(SORTABLE_PLUGINS_COLUMNS) // --------------------------------------------------------------------------- @@ -63,5 +65,6 @@ export { accountsBlocklistSortValidator, serversBlocklistSortValidator, userNotificationsSortValidator, - videoPlaylistsSortValidator + videoPlaylistsSortValidator, + pluginsSortValidator } -- cgit v1.2.3