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 installOrUpdatePluginValidator,
18 uninstallPluginValidator,
19 updatePluginSettingsValidator
20 } from '../../middlewares/validators/plugins'
21 import { PluginManager } from '../../lib/plugins/plugin-manager'
22 import { InstallOrUpdatePlugin } 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 installOrUpdatePluginValidator,
65 asyncMiddleware(installPlugin)
68 pluginRouter.post('/update',
70 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
71 installOrUpdatePluginValidator,
72 asyncMiddleware(updatePlugin)
75 pluginRouter.post('/uninstall',
77 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
78 uninstallPluginValidator,
79 asyncMiddleware(uninstallPlugin)
82 // ---------------------------------------------------------------------------
88 // ---------------------------------------------------------------------------
90 async function listPlugins (req: express.Request, res: express.Response) {
91 const type = req.query.type
93 const resultList = await PluginModel.listForApi({
95 start: req.query.start,
96 count: req.query.count,
100 return res.json(getFormattedObjects(resultList.data, resultList.total))
103 function getPlugin (req: express.Request, res: express.Response) {
104 const plugin = res.locals.plugin
106 return res.json(plugin.toFormattedJSON())
109 async function installPlugin (req: express.Request, res: express.Response) {
110 const body: InstallOrUpdatePlugin = req.body
112 const fromDisk = !!body.path
113 const toInstall = body.npmName || body.path
115 const plugin = await PluginManager.Instance.install(toInstall, undefined, fromDisk)
117 return res.json(plugin.toFormattedJSON())
119 logger.warn('Cannot install plugin %s.', toInstall, { err })
120 return res.sendStatus(400)
124 async function updatePlugin (req: express.Request, res: express.Response) {
125 const body: InstallOrUpdatePlugin = req.body
127 const fromDisk = !!body.path
128 const toUpdate = body.npmName || body.path
130 const plugin = await PluginManager.Instance.update(toUpdate, undefined, fromDisk)
132 return res.json(plugin.toFormattedJSON())
134 logger.warn('Cannot update plugin %s.', toUpdate, { err })
135 return res.sendStatus(400)
139 async function uninstallPlugin (req: express.Request, res: express.Response) {
140 const body: ManagePlugin = req.body
142 await PluginManager.Instance.uninstall(body.npmName)
144 return res.sendStatus(204)
147 function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
148 const settings = PluginManager.Instance.getRegisteredSettings(req.params.npmName)
155 async function updatePluginSettings (req: express.Request, res: express.Response) {
156 const plugin = res.locals.plugin
158 plugin.settings = req.body.settings
161 return res.sendStatus(204)