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'
24 import { logger } from '../../helpers/logger'
26 const pluginRouter = express.Router()
30 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
36 asyncMiddleware(listPlugins)
39 pluginRouter.get('/:npmName',
41 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
42 asyncMiddleware(existingPluginValidator),
46 pluginRouter.get('/:npmName/registered-settings',
48 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
49 asyncMiddleware(existingPluginValidator),
50 getPluginRegisteredSettings
53 pluginRouter.put('/:npmName/settings',
55 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
56 updatePluginSettingsValidator,
57 asyncMiddleware(existingPluginValidator),
58 asyncMiddleware(updatePluginSettings)
61 pluginRouter.post('/install',
63 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
64 installPluginValidator,
65 asyncMiddleware(installPlugin)
68 pluginRouter.post('/uninstall',
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 uninstallPluginValidator,
72 asyncMiddleware(uninstallPlugin)
75 // ---------------------------------------------------------------------------
81 // ---------------------------------------------------------------------------
83 async function listPlugins (req: express.Request, res: express.Response) {
84 const type = req.query.type
86 const resultList = await PluginModel.listForApi({
88 start: req.query.start,
89 count: req.query.count,
93 return res.json(getFormattedObjects(resultList.data, resultList.total))
96 function getPlugin (req: express.Request, res: express.Response) {
97 const plugin = res.locals.plugin
99 return res.json(plugin.toFormattedJSON())
102 async function installPlugin (req: express.Request, res: express.Response) {
103 const body: InstallPlugin = req.body
105 const fromDisk = !!body.path
106 const toInstall = body.npmName || body.path
108 await PluginManager.Instance.install(toInstall, undefined, fromDisk)
110 logger.warn('Cannot install plugin %s.', toInstall, { err })
111 return res.sendStatus(400)
114 return res.sendStatus(204)
117 async function uninstallPlugin (req: express.Request, res: express.Response) {
118 const body: ManagePlugin = req.body
120 await PluginManager.Instance.uninstall(body.npmName)
122 return res.sendStatus(204)
125 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
126 const plugin = res.locals.plugin
128 const settings = PluginManager.Instance.getSettings(plugin.name)
135 async function updatePluginSettings (req: express.Request, res: express.Response) {
136 const plugin = res.locals.plugin
138 plugin.settings = req.body.settings
141 return res.sendStatus(204)