/
Url.php
445 lines (413 loc) · 12.7 KB
/
Url.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Backend\Model;
/**
* Class \Magento\Backend\Model\UrlInterface
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Url extends \Magento\Framework\Url implements \Magento\Backend\Model\UrlInterface
{
/**
* Whether to use a security key in the backend
*
* @bug Currently, this constant is slightly misleading: it says "form key", but in fact it is used by URLs, too
*/
const XML_PATH_USE_SECURE_KEY = 'admin/security/use_form_key';
/**
* Authentication session
*
* @var \Magento\Backend\Model\Auth\Session
*/
protected $_session;
/**
* @var \Magento\Backend\Model\Menu
*/
protected $_menu;
/**
* Startup page url from config
*
* @var string
*/
protected $_startupMenuItemId;
/**
* @var \Magento\Backend\Helper\Data
*/
protected $_backendHelper;
/**
* Menu config
*
* @var \Magento\Backend\Model\Menu\Config
*/
protected $_menuConfig;
/**
* @var \Magento\Framework\App\CacheInterface
*/
protected $_cache;
/**
* @var \Magento\Framework\Encryption\EncryptorInterface
*/
protected $_encryptor;
/**
* @var \Magento\Store\Model\StoreFactory
*/
protected $_storeFactory;
/**
* @var \Magento\Framework\Data\Form\FormKey
*/
protected $formKey;
/**
* @var \Magento\Store\Model\Store
*/
protected $_scope;
/**
* @param \Magento\Framework\App\Route\ConfigInterface $routeConfig
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Url\SecurityInfoInterface $urlSecurityInfo
* @param \Magento\Framework\Url\ScopeResolverInterface $scopeResolver
* @param \Magento\Framework\Session\Generic $session
* @param \Magento\Framework\Session\SidResolverInterface $sidResolver
* @param \Magento\Framework\Url\RouteParamsResolverFactory $routeParamsResolverFactory
* @param \Magento\Framework\Url\QueryParamsResolverInterface $queryParamsResolver
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Framework\Url\RouteParamsPreprocessorInterface $routeParamsPreprocessor
* @param string $scopeType
* @param \Magento\Backend\Helper\Data $backendHelper
* @param Menu\Config $menuConfig
* @param \Magento\Framework\App\CacheInterface $cache
* @param Auth\Session $authSession
* @param \Magento\Framework\Encryption\EncryptorInterface $encryptor
* @param \Magento\Store\Model\StoreFactory $storeFactory
* @param \Magento\Framework\Data\Form\FormKey $formKey
* @param array $data
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Framework\App\Route\ConfigInterface $routeConfig,
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Url\SecurityInfoInterface $urlSecurityInfo,
\Magento\Framework\Url\ScopeResolverInterface $scopeResolver,
\Magento\Framework\Session\Generic $session,
\Magento\Framework\Session\SidResolverInterface $sidResolver,
\Magento\Framework\Url\RouteParamsResolverFactory $routeParamsResolverFactory,
\Magento\Framework\Url\QueryParamsResolverInterface $queryParamsResolver,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\Url\RouteParamsPreprocessorInterface $routeParamsPreprocessor,
$scopeType,
\Magento\Backend\Helper\Data $backendHelper,
\Magento\Backend\Model\Menu\Config $menuConfig,
\Magento\Framework\App\CacheInterface $cache,
\Magento\Backend\Model\Auth\Session $authSession,
\Magento\Framework\Encryption\EncryptorInterface $encryptor,
\Magento\Store\Model\StoreFactory $storeFactory,
\Magento\Framework\Data\Form\FormKey $formKey,
array $data = []
) {
$this->_encryptor = $encryptor;
parent::__construct(
$routeConfig,
$request,
$urlSecurityInfo,
$scopeResolver,
$session,
$sidResolver,
$routeParamsResolverFactory,
$queryParamsResolver,
$scopeConfig,
$routeParamsPreprocessor,
$scopeType,
$data
);
$this->_backendHelper = $backendHelper;
$this->_menuConfig = $menuConfig;
$this->_cache = $cache;
$this->_session = $authSession;
$this->formKey = $formKey;
$this->_storeFactory = $storeFactory;
}
/**
* Retrieve is secure mode for ULR logic
*
* @return bool
*/
protected function _isSecure()
{
if ($this->hasData('secure_is_forced')) {
return $this->getData('secure');
}
return $this->_scopeConfig->isSetFlag('web/secure/use_in_adminhtml');
}
/**
* Force strip secret key param if _nosecret param specified
*
* @param array $data
* @param bool $unsetOldParams
* @return $this
*/
protected function _setRouteParams(array $data, $unsetOldParams = true)
{
if (isset($data['_nosecret'])) {
$this->setNoSecret(true);
unset($data['_nosecret']);
} else {
$this->setNoSecret(false);
}
unset($data['_scope_to_url']);
return parent::_setRouteParams($data, $unsetOldParams);
}
/**
* Custom logic to retrieve Urls
*
* @param string $routePath
* @param array $routeParams
* @return string
*/
public function getUrl($routePath = null, $routeParams = null)
{
if (filter_var($routePath, FILTER_VALIDATE_URL)) {
return $routePath;
}
$cacheSecretKey = false;
if (is_array($routeParams) && isset($routeParams['_cache_secret_key'])) {
unset($routeParams['_cache_secret_key']);
$cacheSecretKey = true;
}
$result = parent::getUrl($routePath, $routeParams);
if (!$this->useSecretKey()) {
return $result;
}
$this->_setRoutePath($routePath);
$routeName = $this->_getRouteName('*');
$controllerName = $this->_getControllerName(self::DEFAULT_CONTROLLER_NAME);
$actionName = $this->_getActionName(self::DEFAULT_ACTION_NAME);
if ($cacheSecretKey) {
$secret = [self::SECRET_KEY_PARAM_NAME => "\${$routeName}/{$controllerName}/{$actionName}\$"];
} else {
$secret = [
self::SECRET_KEY_PARAM_NAME => $this->getSecretKey($routeName, $controllerName, $actionName),
];
}
if (is_array($routeParams)) {
$routeParams = array_merge($secret, $routeParams);
} else {
$routeParams = $secret;
}
if (is_array($this->_getRouteParams())) {
$routeParams = array_merge($this->_getRouteParams(), $routeParams);
}
return parent::getUrl("{$routeName}/{$controllerName}/{$actionName}", $routeParams);
}
/**
* Generate secret key for controller and action based on form key
*
* @param string $routeName
* @param string $controller Controller name
* @param string $action Action name
* @return string
*/
public function getSecretKey($routeName = null, $controller = null, $action = null)
{
$salt = $this->formKey->getFormKey();
$request = $this->_getRequest();
if (!$routeName) {
if ($request->getBeforeForwardInfo('route_name') !== null) {
$routeName = $request->getBeforeForwardInfo('route_name');
} else {
$routeName = $request->getRouteName();
}
}
if (!$controller) {
if ($request->getBeforeForwardInfo('controller_name') !== null) {
$controller = $request->getBeforeForwardInfo('controller_name');
} else {
$controller = $request->getControllerName();
}
}
if (!$action) {
if ($request->getBeforeForwardInfo('action_name') !== null) {
$action = $request->getBeforeForwardInfo('action_name');
} else {
$action = $request->getActionName();
}
}
$secret = $routeName . $controller . $action . $salt;
return $this->_encryptor->getHash($secret);
}
/**
* Return secret key settings flag
*
* @return bool
*/
public function useSecretKey()
{
return $this->_scopeConfig->isSetFlag(self::XML_PATH_USE_SECURE_KEY) && !$this->getNoSecret();
}
/**
* Enable secret key using
*
* @return $this
*/
public function turnOnSecretKey()
{
$this->setNoSecret(false);
return $this;
}
/**
* Disable secret key using
*
* @return $this
*/
public function turnOffSecretKey()
{
$this->setNoSecret(true);
return $this;
}
/**
* Refresh admin menu cache etc.
*
* @return void
*/
public function renewSecretUrls()
{
$this->_cache->clean([\Magento\Backend\Block\Menu::CACHE_TAGS]);
}
/**
* Find admin start page url
*
* @return string
*/
public function getStartupPageUrl()
{
$menuItem = $this->_getMenu()->get(
$this->_scopeConfig->getValue(self::XML_PATH_STARTUP_MENU_ITEM, $this->_scopeType)
);
if ($menuItem !== null) {
if ($menuItem->isAllowed() && $menuItem->getAction()) {
return $menuItem->getAction();
}
}
return $this->findFirstAvailableMenu();
}
/**
* Find first menu item that user is able to access
*
* @return string
*/
public function findFirstAvailableMenu()
{
/* @var $menu \Magento\Backend\Model\Menu\Item */
$menu = $this->_getMenu();
$item = $menu->getFirstAvailable();
$action = $item ? $item->getAction() : null;
if (!$item) {
$user = $this->_getSession()->getUser();
if ($user) {
$user->setHasAvailableResources(false);
}
$action = '*/*/denied';
}
return $action;
}
/**
* Get Menu model
*
* @return \Magento\Backend\Model\Menu
*/
protected function _getMenu()
{
if ($this->_menu === null) {
$this->_menu = $this->_menuConfig->getMenu();
}
return $this->_menu;
}
/**
* Set custom auth session
*
* @param \Magento\Backend\Model\Auth\Session $session
* @return $this
*/
public function setSession(\Magento\Backend\Model\Auth\Session $session)
{
$this->_session = $session;
return $this;
}
/**
* Retrieve auth session
*
* @return \Magento\Backend\Model\Auth\Session
*/
protected function _getSession()
{
return $this->_session;
}
/**
* Return backend area front name, defined in configuration
*
* @return string
*/
public function getAreaFrontName()
{
if (!$this->_getData('area_front_name')) {
$this->setData('area_front_name', $this->_backendHelper->getAreaFrontName());
}
return $this->_getData('area_front_name');
}
/**
* Retrieve action path.
* Add backend area front name as a prefix to action path
*
* @return string
*/
protected function _getActionPath()
{
$path = parent::_getActionPath();
if ($path) {
if ($this->getAreaFrontName()) {
$path = $this->getAreaFrontName() . '/' . $path;
}
}
return $path;
}
/**
* Get scope for the url instance
*
* @return \Magento\Store\Model\Store
*/
protected function _getScope()
{
if (!$this->_scope) {
$this->_scope = $this->_storeFactory->create(
[
'url' => $this,
'data' => ['code' => 'admin', 'force_disable_rewrites' => false, 'disable_store_in_url' => true],
]
);
}
return $this->_scope;
}
/**
* Get cache id for config path
*
* @param string $path
* @return string
*/
protected function _getConfigCacheId($path)
{
return 'admin/' . $path;
}
/**
* Get config data by path
* Use only global config values for backend
*
* @param string $path
* @return null|string
*/
protected function _getConfig($path)
{
return $this->_scopeConfig->getValue($path);
}
}