aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/api/plugins.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/api/plugins.ts')
-rw-r--r--server/controllers/api/plugins.ts121
1 files changed, 121 insertions, 0 deletions
diff --git a/server/controllers/api/plugins.ts b/server/controllers/api/plugins.ts
new file mode 100644
index 000000000..89cc67f54
--- /dev/null
+++ b/server/controllers/api/plugins.ts
@@ -0,0 +1,121 @@
1import * as express from 'express'
2import { getFormattedObjects } from '../../helpers/utils'
3import {
4 asyncMiddleware,
5 authenticate,
6 ensureUserHasRight,
7 paginationValidator,
8 setDefaultPagination,
9 setDefaultSort
10} from '../../middlewares'
11import { pluginsSortValidator } from '../../middlewares/validators'
12import { PluginModel } from '../../models/server/plugin'
13import { UserRight } from '../../../shared/models/users'
14import {
15 enabledPluginValidator,
16 installPluginValidator,
17 listPluginsValidator,
18 uninstallPluginValidator,
19 updatePluginSettingsValidator
20} from '../../middlewares/validators/plugins'
21import { PluginManager } from '../../lib/plugins/plugin-manager'
22import { InstallPlugin } from '../../../shared/models/plugins/install-plugin.model'
23import { ManagePlugin } from '../../../shared/models/plugins/manage-plugin.model'
24
25const pluginRouter = express.Router()
26
27pluginRouter.get('/',
28 authenticate,
29 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
30 listPluginsValidator,
31 paginationValidator,
32 pluginsSortValidator,
33 setDefaultSort,
34 setDefaultPagination,
35 asyncMiddleware(listPlugins)
36)
37
38pluginRouter.get('/:pluginName/settings',
39 authenticate,
40 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
41 asyncMiddleware(enabledPluginValidator),
42 asyncMiddleware(listPluginSettings)
43)
44
45pluginRouter.put('/:pluginName/settings',
46 authenticate,
47 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
48 updatePluginSettingsValidator,
49 asyncMiddleware(enabledPluginValidator),
50 asyncMiddleware(updatePluginSettings)
51)
52
53pluginRouter.post('/install',
54 authenticate,
55 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
56 installPluginValidator,
57 asyncMiddleware(installPlugin)
58)
59
60pluginRouter.post('/uninstall',
61 authenticate,
62 ensureUserHasRight(UserRight.MANAGE_PLUGINS),
63 uninstallPluginValidator,
64 asyncMiddleware(uninstallPlugin)
65)
66
67// ---------------------------------------------------------------------------
68
69export {
70 pluginRouter
71}
72
73// ---------------------------------------------------------------------------
74
75async function listPlugins (req: express.Request, res: express.Response) {
76 const type = req.query.type
77
78 const resultList = await PluginModel.listForApi({
79 type,
80 start: req.query.start,
81 count: req.query.count,
82 sort: req.query.sort
83 })
84
85 return res.json(getFormattedObjects(resultList.data, resultList.total))
86}
87
88async function installPlugin (req: express.Request, res: express.Response) {
89 const body: InstallPlugin = req.body
90
91 await PluginManager.Instance.install(body.npmName)
92
93 return res.sendStatus(204)
94}
95
96async function uninstallPlugin (req: express.Request, res: express.Response) {
97 const body: ManagePlugin = req.body
98
99 await PluginManager.Instance.uninstall(body.npmName)
100
101 return res.sendStatus(204)
102}
103
104async function listPluginSettings (req: express.Request, res: express.Response) {
105 const plugin = res.locals.plugin
106
107 const settings = await PluginManager.Instance.getSettings(plugin.name)
108
109 return res.json({
110 settings
111 })
112}
113
114async function updatePluginSettings (req: express.Request, res: express.Response) {
115 const plugin = res.locals.plugin
116
117 plugin.settings = req.body.settings
118 await plugin.save()
119
120 return res.sendStatus(204)
121}