]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/validators/plugins.ts
Give moderators access to edit channels (#4608)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / plugins.ts
index 2c47ec5bbd03aef78f0a852c98cae228d2f7357a..c1e9ebefbb25f8aa2af3a9d6bf642426ab2d24a1 100644 (file)
@@ -1,6 +1,6 @@
-import * as express from 'express'
+import express from 'express'
 import { body, param, query, ValidationChain } from 'express-validator'
-import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
+import { HttpStatusCode } from '../../../shared/models/http/http-error-codes'
 import { PluginType } from '../../../shared/models/plugins/plugin.type'
 import { InstallOrUpdatePlugin } from '../../../shared/models/plugins/server/api/install-plugin.model'
 import { exists, isBooleanValid, isSafePath, toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc'
@@ -9,7 +9,7 @@ import { logger } from '../../helpers/logger'
 import { CONFIG } from '../../initializers/config'
 import { PluginManager } from '../../lib/plugins/plugin-manager'
 import { PluginModel } from '../../models/server/plugin'
-import { areValidationErrors } from './utils'
+import { areValidationErrors } from './shared'
 
 const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
   const validators: (ValidationChain | express.Handler)[] = [
@@ -31,8 +31,18 @@ const getPluginValidator = (pluginType: PluginType, withVersion = true) => {
       const npmName = PluginModel.buildNpmName(req.params.pluginName, pluginType)
       const plugin = PluginManager.Instance.getRegisteredPluginOrTheme(npmName)
 
-      if (!plugin) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
-      if (withVersion && plugin.version !== req.params.pluginVersion) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
+      if (!plugin) {
+        return res.fail({
+          status: HttpStatusCode.NOT_FOUND_404,
+          message: 'No plugin found named ' + npmName
+        })
+      }
+      if (withVersion && plugin.version !== req.params.pluginVersion) {
+        return res.fail({
+          status: HttpStatusCode.NOT_FOUND_404,
+          message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
+        })
+      }
 
       res.locals.registeredPlugin = plugin
 
@@ -50,10 +60,20 @@ const getExternalAuthValidator = [
     if (areValidationErrors(req, res)) return
 
     const plugin = res.locals.registeredPlugin
-    if (!plugin.registerHelpers) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
+    if (!plugin.registerHelpers) {
+      return res.fail({
+        status: HttpStatusCode.NOT_FOUND_404,
+        message: 'No registered helpers were found for this plugin'
+      })
+    }
 
     const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName)
-    if (!externalAuth) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
+    if (!externalAuth) {
+      return res.fail({
+        status: HttpStatusCode.NOT_FOUND_404,
+        message: 'No external auths were found for this plugin'
+      })
+    }
 
     res.locals.externalAuth = externalAuth
 
@@ -96,6 +116,9 @@ const installOrUpdatePluginValidator = [
   body('npmName')
     .optional()
     .custom(isNpmPluginNameValid).withMessage('Should have a valid npm name'),
+  body('pluginVersion')
+    .optional()
+    .custom(isPluginVersionValid).withMessage('Should have a valid plugin version'),
   body('path')
     .optional()
     .custom(isSafePath).withMessage('Should have a valid safe path'),
@@ -107,8 +130,10 @@ const installOrUpdatePluginValidator = [
 
     const body: InstallOrUpdatePlugin = req.body
     if (!body.path && !body.npmName) {
-      return res.status(HttpStatusCode.BAD_REQUEST_400)
-                .json({ error: 'Should have either a npmName or a path' })
+      return res.fail({ message: 'Should have either a npmName or a path' })
+    }
+    if (body.pluginVersion && !body.npmName) {
+      return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' })
     }
 
     return next()
@@ -137,12 +162,13 @@ const existingPluginValidator = [
 
     const plugin = await PluginModel.loadByNpmName(req.params.npmName)
     if (!plugin) {
-      return res.status(HttpStatusCode.NOT_FOUND_404)
-                .json({ error: 'Plugin not found' })
+      return res.fail({
+        status: HttpStatusCode.NOT_FOUND_404,
+        message: 'Plugin not found'
+      })
     }
 
     res.locals.plugin = plugin
-
     return next()
   }
 ]
@@ -177,9 +203,7 @@ const listAvailablePluginsValidator = [
     if (areValidationErrors(req, res)) return
 
     if (CONFIG.PLUGINS.INDEX.ENABLED === false) {
-      return res.status(HttpStatusCode.BAD_REQUEST_400)
-                .json({ error: 'Plugin index is not enabled' })
-                .end()
+      return res.fail({ message: 'Plugin index is not enabled' })
     }
 
     return next()