]>
Commit | Line | Data |
---|---|---|
ad91e700 C |
1 | import * as express from 'express' |
2 | import { getFormattedObjects } from '../../helpers/utils' | |
3 | import { | |
4 | asyncMiddleware, | |
5 | authenticate, | |
6 | ensureUserHasRight, | |
7 | paginationValidator, | |
8 | setDefaultPagination, | |
9 | setDefaultSort | |
10 | } from '../../middlewares' | |
11 | import { pluginsSortValidator } from '../../middlewares/validators' | |
12 | import { PluginModel } from '../../models/server/plugin' | |
13 | import { UserRight } from '../../../shared/models/users' | |
14 | import { | |
dba85a1e | 15 | existingPluginValidator, |
ad91e700 C |
16 | installPluginValidator, |
17 | listPluginsValidator, | |
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' | |
8d2be0ed | 24 | import { logger } from '../../helpers/logger' |
ad91e700 C |
25 | |
26 | const pluginRouter = express.Router() | |
27 | ||
28 | pluginRouter.get('/', | |
29 | authenticate, | |
30 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
31 | listPluginsValidator, | |
32 | paginationValidator, | |
33 | pluginsSortValidator, | |
34 | setDefaultSort, | |
35 | setDefaultPagination, | |
36 | asyncMiddleware(listPlugins) | |
37 | ) | |
38 | ||
dba85a1e | 39 | pluginRouter.get('/:npmName', |
ad91e700 C |
40 | authenticate, |
41 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
dba85a1e C |
42 | asyncMiddleware(existingPluginValidator), |
43 | getPlugin | |
ad91e700 C |
44 | ) |
45 | ||
dba85a1e C |
46 | pluginRouter.get('/:npmName/registered-settings', |
47 | authenticate, | |
48 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
49 | asyncMiddleware(existingPluginValidator), | |
8d2be0ed | 50 | getPluginRegisteredSettings |
dba85a1e C |
51 | ) |
52 | ||
53 | pluginRouter.put('/:npmName/settings', | |
ad91e700 C |
54 | authenticate, |
55 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
56 | updatePluginSettingsValidator, | |
dba85a1e | 57 | asyncMiddleware(existingPluginValidator), |
ad91e700 C |
58 | asyncMiddleware(updatePluginSettings) |
59 | ) | |
60 | ||
61 | pluginRouter.post('/install', | |
62 | authenticate, | |
63 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
64 | installPluginValidator, | |
65 | asyncMiddleware(installPlugin) | |
66 | ) | |
67 | ||
68 | pluginRouter.post('/uninstall', | |
69 | authenticate, | |
70 | ensureUserHasRight(UserRight.MANAGE_PLUGINS), | |
71 | uninstallPluginValidator, | |
72 | asyncMiddleware(uninstallPlugin) | |
73 | ) | |
74 | ||
75 | // --------------------------------------------------------------------------- | |
76 | ||
77 | export { | |
78 | pluginRouter | |
79 | } | |
80 | ||
81 | // --------------------------------------------------------------------------- | |
82 | ||
83 | async function listPlugins (req: express.Request, res: express.Response) { | |
84 | const type = req.query.type | |
85 | ||
86 | const resultList = await PluginModel.listForApi({ | |
87 | type, | |
88 | start: req.query.start, | |
89 | count: req.query.count, | |
90 | sort: req.query.sort | |
91 | }) | |
92 | ||
93 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
94 | } | |
95 | ||
dba85a1e C |
96 | function getPlugin (req: express.Request, res: express.Response) { |
97 | const plugin = res.locals.plugin | |
98 | ||
99 | return res.json(plugin.toFormattedJSON()) | |
100 | } | |
101 | ||
ad91e700 C |
102 | async function installPlugin (req: express.Request, res: express.Response) { |
103 | const body: InstallPlugin = req.body | |
104 | ||
8d2be0ed C |
105 | const fromDisk = !!body.path |
106 | const toInstall = body.npmName || body.path | |
107 | try { | |
108 | await PluginManager.Instance.install(toInstall, undefined, fromDisk) | |
109 | } catch (err) { | |
110 | logger.warn('Cannot install plugin %s.', toInstall, { err }) | |
111 | return res.sendStatus(400) | |
112 | } | |
ad91e700 C |
113 | |
114 | return res.sendStatus(204) | |
115 | } | |
116 | ||
117 | async function uninstallPlugin (req: express.Request, res: express.Response) { | |
118 | const body: ManagePlugin = req.body | |
119 | ||
120 | await PluginManager.Instance.uninstall(body.npmName) | |
121 | ||
122 | return res.sendStatus(204) | |
123 | } | |
124 | ||
8d2be0ed | 125 | function getPluginRegisteredSettings (req: express.Request, res: express.Response) { |
ad91e700 C |
126 | const plugin = res.locals.plugin |
127 | ||
8d2be0ed | 128 | const settings = PluginManager.Instance.getSettings(plugin.name) |
ad91e700 C |
129 | |
130 | return res.json({ | |
131 | settings | |
132 | }) | |
133 | } | |
134 | ||
135 | async function updatePluginSettings (req: express.Request, res: express.Response) { | |
136 | const plugin = res.locals.plugin | |
137 | ||
138 | plugin.settings = req.body.settings | |
139 | await plugin.save() | |
140 | ||
141 | return res.sendStatus(204) | |
142 | } |