InitializeAuthRoles.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorization\Setup\Patch\Data;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Setup\ModuleDataSetupInterface;
  9. use Magento\Framework\Setup\Patch\DataPatchInterface;
  10. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  11. use Magento\Authorization\Model\Acl\Role\Group as RoleGroup;
  12. use Magento\Authorization\Model\UserContextInterface;
  13. /**
  14. * Class InitializeAuthRoles
  15. * @package Magento\Authorization\Setup\Patch
  16. */
  17. class InitializeAuthRoles implements DataPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var ModuleDataSetupInterface
  21. */
  22. private $moduleDataSetup;
  23. /**
  24. * @var \Magento\Authorization\Setup\AuthorizationFactory
  25. */
  26. private $authFactory;
  27. /**
  28. * InitializeAuthRoles constructor.
  29. * @param ModuleDataSetupInterface $moduleDataSetup
  30. * @param \Magento\Authorization\Setup\AuthorizationFactory $authorizationFactory
  31. */
  32. public function __construct(
  33. ModuleDataSetupInterface $moduleDataSetup,
  34. \Magento\Authorization\Setup\AuthorizationFactory $authorizationFactory
  35. ) {
  36. $this->moduleDataSetup = $moduleDataSetup;
  37. $this->authFactory = $authorizationFactory;
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function apply()
  43. {
  44. $roleCollection = $this->authFactory->createRoleCollection()
  45. ->addFieldToFilter('parent_id', 0)
  46. ->addFieldToFilter('tree_level', 1)
  47. ->addFieldToFilter('role_type', RoleGroup::ROLE_TYPE)
  48. ->addFieldToFilter('user_id', 0)
  49. ->addFieldToFilter('user_type', UserContextInterface::USER_TYPE_ADMIN)
  50. ->addFieldToFilter('role_name', 'Administrators');
  51. if ($roleCollection->count() == 0) {
  52. $admGroupRole = $this->authFactory->createRole()->setData(
  53. [
  54. 'parent_id' => 0,
  55. 'tree_level' => 1,
  56. 'sort_order' => 1,
  57. 'role_type' => RoleGroup::ROLE_TYPE,
  58. 'user_id' => 0,
  59. 'user_type' => UserContextInterface::USER_TYPE_ADMIN,
  60. 'role_name' => 'Administrators',
  61. ]
  62. )->save();
  63. } else {
  64. /** @var \Magento\Authorization\Model\ResourceModel\Role $item */
  65. foreach ($roleCollection as $item) {
  66. $admGroupRole = $item;
  67. break;
  68. }
  69. }
  70. $rulesCollection = $this->authFactory->createRulesCollection()
  71. ->addFieldToFilter('role_id', $admGroupRole->getId())
  72. ->addFieldToFilter('resource_id', 'all');
  73. if ($rulesCollection->count() == 0) {
  74. $this->authFactory->createRules()->setData(
  75. [
  76. 'role_id' => $admGroupRole->getId(),
  77. 'resource_id' => 'Magento_Backend::all',
  78. 'privileges' => null,
  79. 'permission' => 'allow',
  80. ]
  81. )->save();
  82. } else {
  83. /** @var \Magento\Authorization\Model\Rules $rule */
  84. foreach ($rulesCollection as $rule) {
  85. $rule->setData('resource_id', 'Magento_Backend::all')->save();
  86. }
  87. }
  88. /**
  89. * Delete rows by condition from authorization_rule
  90. */
  91. $tableName = $this->moduleDataSetup->getTable('authorization_rule');
  92. if ($tableName) {
  93. $this->moduleDataSetup->getConnection()->delete(
  94. $tableName,
  95. ['resource_id = ?' => 'admin/system/tools/compiler']
  96. );
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public static function getDependencies()
  103. {
  104. return [];
  105. }
  106. /**
  107. * {@inheritdoc}
  108. */
  109. public static function getVersion()
  110. {
  111. return '2.0.0';
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function getAliases()
  117. {
  118. return [];
  119. }
  120. }