-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathPluginsController.php
More file actions
183 lines (158 loc) · 5.63 KB
/
PluginsController.php
File metadata and controls
183 lines (158 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\base\PluginInterface;
use craft\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
/**
* The PluginsController class is a controller that handles various plugin related tasks such installing, uninstalling,
* enabling, disabling and saving plugin settings in the control panel.
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class PluginsController extends Controller
{
/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
$viewActions = ['edit-plugin-settings'];
if (!in_array($action->id, $viewActions)) {
// All other actions require an admin & allowAdminChanges
$this->requireAdmin();
}
return true;
}
/**
* Installs a plugin.
*
* @return Response|null
*/
public function actionInstallPlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$edition = $this->request->getBodyParam('edition');
$success = Craft::$app->getPlugins()->installPlugin($pluginHandle, $edition);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin installed.')) :
$this->asFailure(Craft::t('app', 'Couldn’t install plugin.'));
}
/**
* Installs a plugin.
*
* @return Response
*/
public function actionSwitchEdition(): Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$edition = $this->request->getRequiredBodyParam('edition');
Craft::$app->getPlugins()->switchEdition($pluginHandle, $edition);
return $this->asSuccess(Craft::t('app', 'Plugin edition changed.'));
}
/**
* Uninstalls a plugin.
*
* @return Response|null
*/
public function actionUninstallPlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$success = Craft::$app->getPlugins()->uninstallPlugin($pluginHandle);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin uninstalled.')) :
$this->asFailure(Craft::t('app', 'Couldn’t uninstall plugin.'));
}
/**
* Edits a plugin’s settings.
*
* @param string $handle The plugin’s handle
* @param PluginInterface|null $plugin The plugin, if there were validation errors
* @return mixed
* @throws NotFoundHttpException if the requested plugin cannot be found
*/
public function actionEditPluginSettings(string $handle, ?PluginInterface $plugin = null): mixed
{
$this->requireAdmin(false);
if (
$plugin === null &&
($plugin = Craft::$app->getPlugins()->getPlugin($handle)) === null
) {
throw new NotFoundHttpException('Plugin not found');
}
// Read-only?
if (!Craft::$app->getConfig()->getGeneral()->allowAdminChanges) {
if (!$plugin->hasReadOnlyCpSettings) {
throw new ForbiddenHttpException('Administrative changes are disallowed in this environment.');
}
return $plugin->getReadOnlySettingsResponse();
}
return $plugin->getSettingsResponse();
}
/**
* Enables a plugin.
*
* @return Response|null
*/
public function actionEnablePlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$success = Craft::$app->getPlugins()->enablePlugin($pluginHandle);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin enabled.')) :
$this->asFailure(Craft::t('app', 'Couldn’t enable plugin.'));
}
/**
* Disables a plugin.
*
* @return Response|null
*/
public function actionDisablePlugin(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$success = Craft::$app->getPlugins()->disablePlugin($pluginHandle);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin disabled.')) :
$this->asFailure(Craft::t('app', 'Couldn’t disable plugin.'));
}
/**
* Saves a plugin’s settings.
*
* @return Response|null
* @throws NotFoundHttpException if the requested plugin cannot be found
*/
public function actionSavePluginSettings(): ?Response
{
$this->requirePostRequest();
$pluginHandle = $this->request->getRequiredBodyParam('pluginHandle');
$settings = $this->request->getBodyParam('settings', []);
$plugin = Craft::$app->getPlugins()->getPlugin($pluginHandle);
if ($plugin === null) {
throw new NotFoundHttpException('Plugin not found');
}
$success = Craft::$app->getPlugins()->savePluginSettings($plugin, $settings);
return $success ?
$this->asSuccess(Craft::t('app', 'Plugin settings saved.')) :
$this->asFailure(
Craft::t('app', 'Couldn’t save plugin settings.'),
routeParams: ['plugin' => $plugin]
);
}
}