Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Url.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Backend\Model;
7 
11 
20 {
26  const XML_PATH_USE_SECURE_KEY = 'admin/security/use_form_key';
27 
33  protected $_session;
34 
38  protected $_menu;
39 
46 
50  protected $_backendHelper;
51 
57  protected $_menuConfig;
58 
62  protected $_cache;
63 
67  protected $_encryptor;
68 
72  protected $_storeFactory;
73 
77  protected $formKey;
78 
82  protected $_scope;
83 
110  public function __construct(
111  \Magento\Framework\App\Route\ConfigInterface $routeConfig,
113  \Magento\Framework\Url\SecurityInfoInterface $urlSecurityInfo,
114  \Magento\Framework\Url\ScopeResolverInterface $scopeResolver,
115  \Magento\Framework\Session\Generic $session,
116  \Magento\Framework\Session\SidResolverInterface $sidResolver,
117  \Magento\Framework\Url\RouteParamsResolverFactory $routeParamsResolverFactory,
118  \Magento\Framework\Url\QueryParamsResolverInterface $queryParamsResolver,
119  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
120  \Magento\Framework\Url\RouteParamsPreprocessorInterface $routeParamsPreprocessor,
121  $scopeType,
122  \Magento\Backend\Helper\Data $backendHelper,
123  \Magento\Backend\Model\Menu\Config $menuConfig,
124  \Magento\Framework\App\CacheInterface $cache,
125  \Magento\Backend\Model\Auth\Session $authSession,
126  \Magento\Framework\Encryption\EncryptorInterface $encryptor,
127  \Magento\Store\Model\StoreFactory $storeFactory,
128  \Magento\Framework\Data\Form\FormKey $formKey,
129  array $data = [],
130  HostChecker $hostChecker = null,
131  Json $serializer = null
132  ) {
133  $this->_encryptor = $encryptor;
134  $hostChecker = $hostChecker ?: ObjectManager::getInstance()->get(HostChecker::class);
135  parent::__construct(
136  $routeConfig,
137  $request,
138  $urlSecurityInfo,
139  $scopeResolver,
140  $session,
141  $sidResolver,
142  $routeParamsResolverFactory,
143  $queryParamsResolver,
144  $scopeConfig,
146  $scopeType,
147  $data,
148  $hostChecker,
150  );
151  $this->_backendHelper = $backendHelper;
152  $this->_menuConfig = $menuConfig;
153  $this->_cache = $cache;
154  $this->_session = $authSession;
155  $this->formKey = $formKey;
156  $this->_storeFactory = $storeFactory;
157  }
158 
164  protected function _isSecure()
165  {
166  if ($this->hasData('secure_is_forced')) {
167  return $this->getData('secure');
168  }
169  return $this->_scopeConfig->isSetFlag('web/secure/use_in_adminhtml');
170  }
171 
179  protected function _setRouteParams(array $data, $unsetOldParams = true)
180  {
181  if (isset($data['_nosecret'])) {
182  $this->setNoSecret(true);
183  unset($data['_nosecret']);
184  } else {
185  $this->setNoSecret(false);
186  }
187  unset($data['_scope_to_url']);
188  return parent::_setRouteParams($data, $unsetOldParams);
189  }
190 
198  public function getUrl($routePath = null, $routeParams = null)
199  {
200  if (filter_var($routePath, FILTER_VALIDATE_URL)) {
201  return $routePath;
202  }
203 
204  $cacheSecretKey = false;
205  if (isset($routeParams['_cache_secret_key'])) {
206  unset($routeParams['_cache_secret_key']);
207  $cacheSecretKey = true;
208  }
209  $result = parent::getUrl($routePath, $routeParams);
210  if (!$this->useSecretKey()) {
211  return $result;
212  }
213 
214  $this->getRouteParamsResolver()->unsetData('route_params');
215  $this->_setRoutePath($routePath);
216  $extraParams = $this->getRouteParamsResolver()->getRouteParams();
217  $routeName = $this->_getRouteName('*');
218  $controllerName = $this->_getControllerName(self::DEFAULT_CONTROLLER_NAME);
219  $actionName = $this->_getActionName(self::DEFAULT_ACTION_NAME);
220 
221  if (!isset($routeParams[self::SECRET_KEY_PARAM_NAME])) {
222  if (!is_array($routeParams)) {
223  $routeParams = [];
224  }
225  $secretKey = $cacheSecretKey
226  ? "\${$routeName}/{$controllerName}/{$actionName}\$"
227  : $this->getSecretKey($routeName, $controllerName, $actionName);
228  $routeParams[self::SECRET_KEY_PARAM_NAME] = $secretKey;
229  }
230 
231  if (!empty($extraParams)) {
232  $routeParams = array_merge($extraParams, $routeParams);
233  }
234 
235  return parent::getUrl("{$routeName}/{$controllerName}/{$actionName}", $routeParams);
236  }
237 
246  public function getSecretKey($routeName = null, $controller = null, $action = null)
247  {
248  $salt = $this->formKey->getFormKey();
249  $request = $this->_getRequest();
250  if (!$routeName) {
251  if ($request->getBeforeForwardInfo('route_name') !== null) {
252  $routeName = $request->getBeforeForwardInfo('route_name');
253  } else {
254  $routeName = $request->getRouteName();
255  }
256  }
257  if (!$controller) {
258  if ($request->getBeforeForwardInfo('controller_name') !== null) {
259  $controller = $request->getBeforeForwardInfo('controller_name');
260  } else {
261  $controller = $request->getControllerName();
262  }
263  }
264  if (!$action) {
265  if ($request->getBeforeForwardInfo('action_name') !== null) {
266  $action = $request->getBeforeForwardInfo('action_name');
267  } else {
268  $action = $request->getActionName();
269  }
270  }
271  $secret = $routeName . $controller . $action . $salt;
272  return $this->_encryptor->getHash($secret);
273  }
274 
280  public function useSecretKey()
281  {
282  return $this->_scopeConfig->isSetFlag(self::XML_PATH_USE_SECURE_KEY) && !$this->getNoSecret();
283  }
284 
290  public function turnOnSecretKey()
291  {
292  $this->setNoSecret(false);
293  return $this;
294  }
295 
301  public function turnOffSecretKey()
302  {
303  $this->setNoSecret(true);
304  return $this;
305  }
306 
312  public function renewSecretUrls()
313  {
314  $this->_cache->clean([\Magento\Backend\Block\Menu::CACHE_TAGS]);
315  }
316 
322  public function getStartupPageUrl()
323  {
324  $menuItem = $this->_getMenu()->get(
325  $this->_scopeConfig->getValue(self::XML_PATH_STARTUP_MENU_ITEM, $this->_scopeType)
326  );
327  if ($menuItem !== null) {
328  if ($menuItem->isAllowed() && $menuItem->getAction()) {
329  return $menuItem->getAction();
330  }
331  }
332  return $this->findFirstAvailableMenu();
333  }
334 
340  public function findFirstAvailableMenu()
341  {
342  /* @var $menu \Magento\Backend\Model\Menu\Item */
343  $menu = $this->_getMenu();
344  $item = $menu->getFirstAvailable();
345  $action = $item ? $item->getAction() : null;
346  if (!$item) {
347  $user = $this->_getSession()->getUser();
348  if ($user) {
349  $user->setHasAvailableResources(false);
350  }
351  $action = '*/*/denied';
352  }
353  return $action;
354  }
355 
361  protected function _getMenu()
362  {
363  if ($this->_menu === null) {
364  $this->_menu = $this->_menuConfig->getMenu();
365  }
366  return $this->_menu;
367  }
368 
375  public function setSession(\Magento\Backend\Model\Auth\Session $session)
376  {
377  $this->_session = $session;
378  return $this;
379  }
380 
386  protected function _getSession()
387  {
388  return $this->_session;
389  }
390 
396  public function getAreaFrontName()
397  {
398  if (!$this->_getData('area_front_name')) {
399  $this->setData('area_front_name', $this->_backendHelper->getAreaFrontName());
400  }
401  return $this->_getData('area_front_name');
402  }
403 
410  protected function _getActionPath()
411  {
412  $path = parent::_getActionPath();
413  if ($path) {
414  if ($this->getAreaFrontName()) {
415  $path = $this->getAreaFrontName() . '/' . $path;
416  }
417  }
418  return $path;
419  }
420 
426  protected function _getScope()
427  {
428  if (!$this->_scope) {
429  $this->_scope = $this->_storeFactory->create(
430  [
431  'url' => $this,
432  'data' => ['code' => 'admin', 'force_disable_rewrites' => false, 'disable_store_in_url' => true],
433  ]
434  );
435  }
436  return $this->_scope;
437  }
438 
445  protected function _getConfigCacheId($path)
446  {
447  return 'admin/' . $path;
448  }
449 
457  protected function _getConfig($path)
458  {
459  return $this->_scopeConfig->getValue($path);
460  }
461 }
getSecretKey($routeName=null, $controller=null, $action=null)
Definition: Url.php:246
_getActionName($default=null)
Definition: Url.php:701
getData($key='', $index=null)
Definition: DataObject.php:119
_setRoutePath($data)
Definition: Url.php:498
_getRouteName($default=null)
Definition: Url.php:643
$user
Definition: dummy_user.php:13
getUrl($routePath=null, $routeParams=null)
Definition: Url.php:198
_setRouteParams(array $data, $unsetOldParams=true)
Definition: Url.php:179
setData($key, $value=null)
Definition: DataObject.php:72
const XML_PATH_USE_SECURE_KEY
Definition: Url.php:26
_getControllerName($default=null)
Definition: Url.php:672
setSession(\Magento\Backend\Model\Auth\Session $session)
Definition: Url.php:375
_getConfigCacheId($path)
Definition: Url.php:445
$controller
Definition: info.phtml:14
__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=[], HostChecker $hostChecker=null, Json $serializer=null)
Definition: Url.php:110