-
Notifications
You must be signed in to change notification settings - Fork 611
/
PluginStoreController.php
164 lines (141 loc) · 4.82 KB
/
PluginStoreController.php
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
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\errors\InvalidLicenseKeyException;
use craft\errors\InvalidPluginException;
use craft\helpers\App;
use craft\helpers\Json;
use craft\helpers\UrlHelper;
use craft\web\assets\pluginstore\PluginStoreAsset;
use craft\web\Controller;
use craft\web\View;
use yii\base\InvalidConfigException;
use yii\web\BadRequestHttpException;
use yii\web\Response;
/**
* The PluginStoreController class is a controller that handles various actions related to the Plugin Store.
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
* @internal
*/
class PluginStoreController extends Controller
{
/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
// All plugin store actions require an admin
$this->requireAdmin(false);
return true;
}
/**
* Plugin Store index.
*
* @return Response
* @throws \yii\base\Exception
* @throws InvalidConfigException
*/
public function actionIndex(): Response
{
$view = $this->getView();
$view->registerJsFile('https://js.stripe.com/v2/');
$variables = [
'craftIdEndpoint' => Craft::$app->getPluginStore()->craftIdEndpoint,
'craftApiEndpoint' => Craft::$app->getPluginStore()->craftApiEndpoint,
'pluginStoreAppBaseUrl' => $this->_getVueAppBaseUrl(),
'cmsInfo' => [
'version' => Craft::$app->getVersion(),
'edition' => strtolower(Craft::$app->getEditionName()),
],
'cmsLicenseKey' => App::licenseKey(),
'craftIdAccessToken' => $this->getCraftIdAccessToken(),
'phpVersion' => App::phpVersion(),
'composerPhpVersion' => Craft::$app->getComposer()->getConfig()['config']['platform']['php'] ?? null,
];
$view->registerJsWithVars(
fn($variables) => "Object.assign(window, $variables)",
[$variables],
View::POS_BEGIN
);
$view->registerAssetBundle(PluginStoreAsset::class);
return $this->renderTemplate('plugin-store/_index.twig');
}
/**
* Returns Craft data.
*
* @return Response
* @throws BadRequestHttpException
*/
public function actionCraftData(): Response
{
$this->requireAcceptsJson();
$data = [];
// Current user
$currentUser = static::currentUser();
$data['currentUser'] = $currentUser->getAttributes(['email']);
// Craft license/edition info
$data['licensedEdition'] = Craft::$app->getLicensedEdition();
$data['canTestEditions'] = Craft::$app->getCanTestEditions();
$data['CraftEdition'] = Craft::$app->getEdition();
$data['CraftSolo'] = Craft::Solo;
$data['CraftPro'] = Craft::Pro;
// Logos
$data['craftLogo'] = Craft::$app->getAssetManager()->getPublishedUrl('@app/web/assets/pluginstore/dist/', true, 'images/craft.svg');
return $this->asJson($data);
}
/**
* Save plugin license keys.
*
* @return Response
* @throws InvalidLicenseKeyException
* @throws InvalidPluginException
*/
public function actionSavePluginLicenseKeys(): Response
{
$payload = Json::decode($this->request->getRawBody(), true);
$pluginLicenseKeys = ($payload['pluginLicenseKeys'] ?? []);
$plugins = Craft::$app->getPlugins()->getAllPlugins();
foreach ($pluginLicenseKeys as $pluginLicenseKey) {
if (isset($plugins[$pluginLicenseKey['handle']])) {
Craft::$app->getPlugins()->setPluginLicenseKey($pluginLicenseKey['handle'], $pluginLicenseKey['key']);
}
}
return $this->asSuccess();
}
/**
* Returns the Plugin Store’s Vue App Base URL for Vue Router.
*
* @return string
*/
private function _getVueAppBaseUrl(): string
{
$url = UrlHelper::rootRelativeUrl(UrlHelper::url('plugin-store'));
return UrlHelper::removeParam($url, 'site');
}
/**
* Returns the Craft Console access token.
*
* @return string|null
*/
private function getCraftIdAccessToken(): ?string
{
$craftIdAccessToken = null;
$pluginStoreService = Craft::$app->getPluginStore();
$craftIdToken = $pluginStoreService->getToken();
if ($craftIdToken && $craftIdToken->accessToken !== null) {
$craftIdAccessToken = $craftIdToken->accessToken;
}
return $craftIdAccessToken;
}
}