1 import * as express from 'express'
2 import { getFormattedObjects } from '../../helpers/utils'
10 } from '../../middlewares'
11 import { pluginsSortValidator } from '../../middlewares/validators'
12 import { PluginModel } from '../../models/server/plugin'
13 import { UserRight } from '../../../shared/models/users'
15 existingPluginValidator,
16 installPluginValidator,
18 uninstallPluginValidator,
19 updatePluginSettingsValidator
20 } from '../../middlewares/validators/plugins'
21 import { PluginManager } from '../../lib/plugins/plugin-manager'
22 import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
23 import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
25 const pluginRouter = express.Router()
29 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
35 asyncMiddleware(listPlugins)
38 pluginRouter.get('/:npmName',
40 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
41 asyncMiddleware(existingPluginValidator),
45 pluginRouter.get('/:npmName/registered-settings',
47 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
48 asyncMiddleware(existingPluginValidator),
49 asyncMiddleware(getPluginRegisteredSettings)
52 pluginRouter.put('/:npmName/settings',
54 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
55 updatePluginSettingsValidator,
56 asyncMiddleware(existingPluginValidator),
57 asyncMiddleware(updatePluginSettings)
60 pluginRouter.post('/install',
62 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
63 installPluginValidator,
64 asyncMiddleware(installPlugin)
67 pluginRouter.post('/uninstall',
69 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
70 uninstallPluginValidator,
71 asyncMiddleware(uninstallPlugin)
74 // ---------------------------------------------------------------------------
80 // ---------------------------------------------------------------------------
82 async function listPlugins (req: express.Request, res: express.Response) {
83 const type = req.query.type
85 const resultList = await PluginModel.listForApi({
87 start: req.query.start,
88 count: req.query.count,
92 return res.json(getFormattedObjects(resultList.data, resultList.total))
95 function getPlugin (req: express.Request, res: express.Response) {
96 const plugin = res.locals.plugin
98 return res.json(plugin.toFormattedJSON())
101 async function installPlugin (req: express.Request, res: express.Response) {
102 const body: InstallPlugin = req.body
104 await PluginManager.Instance.install(body.npmName)
106 return res.sendStatus(204)
109 async function uninstallPlugin (req: express.Request, res: express.Response) {
110 const body: ManagePlugin = req.body
112 await PluginManager.Instance.uninstall(body.npmName)
114 return res.sendStatus(204)
117 async function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
118 const plugin = res.locals.plugin
120 const settings = await PluginManager.Instance.getSettings(plugin.name)
127 async function updatePluginSettings (req: express.Request, res: express.Response) {
128 const plugin = res.locals.plugin
130 plugin.settings = req.body.settings
133 return res.sendStatus(204)