diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-10 16:59:53 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | ad91e7006e41f8ee5b8dcefee30f99e8ca44133a (patch) | |
tree | d860f20e05b036fa1a96e049c74deffd7f5d2b00 /server/middlewares/validators/plugins.ts | |
parent | ffb321bedca46d6987c7b31dd58e5dea96ea2ea2 (diff) | |
download | PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.tar.gz PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.tar.zst PeerTube-ad91e7006e41f8ee5b8dcefee30f99e8ca44133a.zip |
WIP plugins: plugin settings on server side
Diffstat (limited to 'server/middlewares/validators/plugins.ts')
-rw-r--r-- | server/middlewares/validators/plugins.ts | 89 |
1 files changed, 85 insertions, 4 deletions
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { param } from 'express-validator/check' | 2 | import { param, query, body } from 'express-validator/check' |
3 | import { logger } from '../../helpers/logger' | 3 | import { logger } from '../../helpers/logger' |
4 | import { areValidationErrors } from './utils' | 4 | import { areValidationErrors } from './utils' |
5 | import { isPluginNameValid, isPluginVersionValid } from '../../helpers/custom-validators/plugins' | 5 | import { isPluginNameValid, isPluginTypeValid, isPluginVersionValid, isNpmPluginNameValid } from '../../helpers/custom-validators/plugins' |
6 | import { PluginManager } from '../../lib/plugins/plugin-manager' | 6 | import { PluginManager } from '../../lib/plugins/plugin-manager' |
7 | import { isSafePath } from '../../helpers/custom-validators/misc' | 7 | import { isBooleanValid, isSafePath } from '../../helpers/custom-validators/misc' |
8 | import { PluginModel } from '../../models/server/plugin' | ||
8 | 9 | ||
9 | const servePluginStaticDirectoryValidator = [ | 10 | const servePluginStaticDirectoryValidator = [ |
10 | param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), | 11 | param('pluginName').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), |
@@ -28,8 +29,88 @@ const servePluginStaticDirectoryValidator = [ | |||
28 | } | 29 | } |
29 | ] | 30 | ] |
30 | 31 | ||
32 | const listPluginsValidator = [ | ||
33 | query('type') | ||
34 | .optional() | ||
35 | .custom(isPluginTypeValid).withMessage('Should have a valid plugin type'), | ||
36 | query('uninstalled') | ||
37 | .optional() | ||
38 | .toBoolean() | ||
39 | .custom(isBooleanValid).withMessage('Should have a valid uninstalled attribute'), | ||
40 | |||
41 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
42 | logger.debug('Checking listPluginsValidator parameters', { parameters: req.query }) | ||
43 | |||
44 | if (areValidationErrors(req, res)) return | ||
45 | |||
46 | return next() | ||
47 | } | ||
48 | ] | ||
49 | |||
50 | const installPluginValidator = [ | ||
51 | body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), | ||
52 | |||
53 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
54 | logger.debug('Checking installPluginValidator parameters', { parameters: req.body }) | ||
55 | |||
56 | if (areValidationErrors(req, res)) return | ||
57 | |||
58 | return next() | ||
59 | } | ||
60 | ] | ||
61 | |||
62 | const uninstallPluginValidator = [ | ||
63 | body('npmName').custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'), | ||
64 | |||
65 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
66 | logger.debug('Checking managePluginValidator parameters', { parameters: req.body }) | ||
67 | |||
68 | if (areValidationErrors(req, res)) return | ||
69 | |||
70 | return next() | ||
71 | } | ||
72 | ] | ||
73 | |||
74 | const enabledPluginValidator = [ | ||
75 | body('name').custom(isPluginNameValid).withMessage('Should have a valid plugin name'), | ||
76 | |||
77 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
78 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body }) | ||
79 | |||
80 | if (areValidationErrors(req, res)) return | ||
81 | |||
82 | const plugin = await PluginModel.load(req.body.name) | ||
83 | if (!plugin) { | ||
84 | return res.status(404) | ||
85 | .json({ error: 'Plugin not found' }) | ||
86 | .end() | ||
87 | } | ||
88 | |||
89 | res.locals.plugin = plugin | ||
90 | |||
91 | return next() | ||
92 | } | ||
93 | ] | ||
94 | |||
95 | const updatePluginSettingsValidator = [ | ||
96 | body('settings').exists().withMessage('Should have settings'), | ||
97 | |||
98 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | ||
99 | logger.debug('Checking enabledPluginValidator parameters', { parameters: req.body }) | ||
100 | |||
101 | if (areValidationErrors(req, res)) return | ||
102 | |||
103 | return next() | ||
104 | } | ||
105 | ] | ||
106 | |||
31 | // --------------------------------------------------------------------------- | 107 | // --------------------------------------------------------------------------- |
32 | 108 | ||
33 | export { | 109 | export { |
34 | servePluginStaticDirectoryValidator | 110 | servePluginStaticDirectoryValidator, |
111 | updatePluginSettingsValidator, | ||
112 | uninstallPluginValidator, | ||
113 | enabledPluginValidator, | ||
114 | installPluginValidator, | ||
115 | listPluginsValidator | ||
35 | } | 116 | } |