AbstractConfig.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Controller\Adminhtml\System;
  7. use Magento\Framework\Exception\LocalizedException;
  8. /**
  9. * System Configuration Abstract Controller
  10. * @api
  11. * @since 100.0.2
  12. */
  13. abstract class AbstractConfig extends \Magento\Backend\App\AbstractAction
  14. {
  15. /**
  16. * Authorization level of a basic admin session
  17. *
  18. * @see _isAllowed()
  19. */
  20. const ADMIN_RESOURCE = 'Magento_Config::config';
  21. /**
  22. * @var \Magento\Config\Model\Config\Structure
  23. */
  24. protected $_configStructure;
  25. /**
  26. * @deprecated 101.0.0
  27. */
  28. protected $_sectionChecker;
  29. /**
  30. * @param \Magento\Backend\App\Action\Context $context
  31. * @param \Magento\Config\Model\Config\Structure $configStructure
  32. * @param mixed $sectionChecker - deprecated
  33. */
  34. public function __construct(
  35. \Magento\Backend\App\Action\Context $context,
  36. \Magento\Config\Model\Config\Structure $configStructure,
  37. $sectionChecker
  38. ) {
  39. parent::__construct($context);
  40. $this->_configStructure = $configStructure;
  41. $this->_sectionChecker = $sectionChecker;
  42. }
  43. /**
  44. * Check if current section is found and is allowed
  45. *
  46. * @param \Magento\Framework\App\RequestInterface $request
  47. * @return \Magento\Framework\App\ResponseInterface
  48. */
  49. public function dispatch(\Magento\Framework\App\RequestInterface $request)
  50. {
  51. if (!$request->getParam('section')) {
  52. try {
  53. $request->setParam('section', $this->_configStructure->getFirstSection()->getId());
  54. } catch (LocalizedException $e) {
  55. /** If visible section not found need to show only config index page without sections if it allow. */
  56. $this->messageManager->addWarningMessage($e->getMessage());
  57. }
  58. }
  59. return parent::dispatch($request);
  60. }
  61. /**
  62. * Check is allow modify system configuration
  63. *
  64. * @return bool
  65. */
  66. protected function _isAllowed()
  67. {
  68. $sectionId = $this->_request->getParam('section');
  69. return parent::_isAllowed()
  70. || $this->_configStructure->getElement($sectionId)->isAllowed();
  71. }
  72. /**
  73. * Save state of configuration field sets
  74. *
  75. * @param array $configState
  76. * @return bool
  77. */
  78. protected function _saveState($configState = [])
  79. {
  80. if (is_array($configState)) {
  81. $configState = $this->sanitizeConfigState($configState);
  82. $adminUser = $this->_auth->getUser();
  83. $extra = $adminUser->getExtra();
  84. if (!is_array($extra)) {
  85. $extra = [];
  86. }
  87. if (!isset($extra['configState'])) {
  88. $extra['configState'] = [];
  89. }
  90. foreach ($configState as $fieldset => $state) {
  91. $extra['configState'][$fieldset] = $state;
  92. }
  93. $adminUser->saveExtra($extra);
  94. }
  95. return true;
  96. }
  97. /**
  98. * Sanitize config state data
  99. *
  100. * @param array $configState
  101. * @return array
  102. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  103. * @since 100.1.0
  104. */
  105. protected function sanitizeConfigState($configState)
  106. {
  107. $sectionList = $this->_configStructure->getSectionList();
  108. $sanitizedConfigState = $configState;
  109. foreach ($configState as $sectionId => $value) {
  110. if (array_key_exists($sectionId, $sectionList)) {
  111. $sanitizedConfigState[$sectionId] = (bool)$sanitizedConfigState[$sectionId] ? '1' : '0';
  112. } else {
  113. unset($sanitizedConfigState[$sectionId]);
  114. }
  115. }
  116. return $sanitizedConfigState;
  117. }
  118. }