2019-07-10 09:59:53 -05:00
|
|
|
import * as express from 'express'
|
|
|
|
import { getFormattedObjects } from '../../helpers/utils'
|
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight,
|
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
|
|
|
} from '../../middlewares'
|
|
|
|
import { pluginsSortValidator } from '../../middlewares/validators'
|
|
|
|
import { PluginModel } from '../../models/server/plugin'
|
|
|
|
import { UserRight } from '../../../shared/models/users'
|
|
|
|
import {
|
2019-07-11 07:40:19 -05:00
|
|
|
existingPluginValidator,
|
2019-07-10 09:59:53 -05:00
|
|
|
installPluginValidator,
|
|
|
|
listPluginsValidator,
|
|
|
|
uninstallPluginValidator,
|
|
|
|
updatePluginSettingsValidator
|
|
|
|
} from '../../middlewares/validators/plugins'
|
|
|
|
import { PluginManager } from '../../lib/plugins/plugin-manager'
|
|
|
|
import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
|
|
|
|
import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
|
2019-07-11 10:23:24 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
2019-07-10 09:59:53 -05:00
|
|
|
|
|
|
|
const pluginRouter = express.Router()
|
|
|
|
|
|
|
|
pluginRouter.get('/',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
listPluginsValidator,
|
|
|
|
paginationValidator,
|
|
|
|
pluginsSortValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
asyncMiddleware(listPlugins)
|
|
|
|
)
|
|
|
|
|
2019-07-11 07:40:19 -05:00
|
|
|
pluginRouter.get('/:npmName',
|
2019-07-10 09:59:53 -05:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
2019-07-11 07:40:19 -05:00
|
|
|
asyncMiddleware(existingPluginValidator),
|
|
|
|
getPlugin
|
2019-07-10 09:59:53 -05:00
|
|
|
)
|
|
|
|
|
2019-07-11 07:40:19 -05:00
|
|
|
pluginRouter.get('/:npmName/registered-settings',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
asyncMiddleware(existingPluginValidator),
|
2019-07-11 10:23:24 -05:00
|
|
|
getPluginRegisteredSettings
|
2019-07-11 07:40:19 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
pluginRouter.put('/:npmName/settings',
|
2019-07-10 09:59:53 -05:00
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
updatePluginSettingsValidator,
|
2019-07-11 07:40:19 -05:00
|
|
|
asyncMiddleware(existingPluginValidator),
|
2019-07-10 09:59:53 -05:00
|
|
|
asyncMiddleware(updatePluginSettings)
|
|
|
|
)
|
|
|
|
|
|
|
|
pluginRouter.post('/install',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
installPluginValidator,
|
|
|
|
asyncMiddleware(installPlugin)
|
|
|
|
)
|
|
|
|
|
|
|
|
pluginRouter.post('/uninstall',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.MANAGE_PLUGINS),
|
|
|
|
uninstallPluginValidator,
|
|
|
|
asyncMiddleware(uninstallPlugin)
|
|
|
|
)
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
pluginRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
async function listPlugins (req: express.Request, res: express.Response) {
|
|
|
|
const type = req.query.type
|
|
|
|
|
|
|
|
const resultList = await PluginModel.listForApi({
|
|
|
|
type,
|
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort
|
|
|
|
})
|
|
|
|
|
|
|
|
return res.json(getFormattedObjects(resultList.data, resultList.total))
|
|
|
|
}
|
|
|
|
|
2019-07-11 07:40:19 -05:00
|
|
|
function getPlugin (req: express.Request, res: express.Response) {
|
|
|
|
const plugin = res.locals.plugin
|
|
|
|
|
|
|
|
return res.json(plugin.toFormattedJSON())
|
|
|
|
}
|
|
|
|
|
2019-07-10 09:59:53 -05:00
|
|
|
async function installPlugin (req: express.Request, res: express.Response) {
|
|
|
|
const body: InstallPlugin = req.body
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
const fromDisk = !!body.path
|
|
|
|
const toInstall = body.npmName || body.path
|
|
|
|
try {
|
|
|
|
await PluginManager.Instance.install(toInstall, undefined, fromDisk)
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot install plugin %s.', toInstall, { err })
|
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
2019-07-10 09:59:53 -05:00
|
|
|
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function uninstallPlugin (req: express.Request, res: express.Response) {
|
|
|
|
const body: ManagePlugin = req.body
|
|
|
|
|
|
|
|
await PluginManager.Instance.uninstall(body.npmName)
|
|
|
|
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
function getPluginRegisteredSettings (req: express.Request, res: express.Response) {
|
2019-07-10 09:59:53 -05:00
|
|
|
const plugin = res.locals.plugin
|
|
|
|
|
2019-07-11 10:23:24 -05:00
|
|
|
const settings = PluginManager.Instance.getSettings(plugin.name)
|
2019-07-10 09:59:53 -05:00
|
|
|
|
|
|
|
return res.json({
|
|
|
|
settings
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updatePluginSettings (req: express.Request, res: express.Response) {
|
|
|
|
const plugin = res.locals.plugin
|
|
|
|
|
|
|
|
plugin.settings = req.body.settings
|
|
|
|
await plugin.save()
|
|
|
|
|
|
|
|
return res.sendStatus(204)
|
|
|
|
}
|