Role.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorization\Model\Acl\Loader;
  7. use Magento\Authorization\Model\Acl\Role\Group as RoleGroup;
  8. use Magento\Authorization\Model\Acl\Role\User as RoleUser;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Serialize\Serializer\Json;
  11. class Role implements \Magento\Framework\Acl\LoaderInterface
  12. {
  13. /**
  14. * Cache key for ACL roles cache
  15. */
  16. const ACL_ROLES_CACHE_KEY = 'authorization_role_cached_data';
  17. /**
  18. * @var \Magento\Framework\App\ResourceConnection
  19. */
  20. protected $_resource;
  21. /**
  22. * @var \Magento\Authorization\Model\Acl\Role\GroupFactory
  23. */
  24. protected $_groupFactory;
  25. /**
  26. * @var \Magento\Authorization\Model\Acl\Role\UserFactory
  27. */
  28. protected $_roleFactory;
  29. /**
  30. * @var \Magento\Framework\Acl\Data\CacheInterface
  31. */
  32. private $aclDataCache;
  33. /**
  34. * @var Json
  35. */
  36. private $serializer;
  37. /**
  38. * @var string
  39. */
  40. private $cacheKey;
  41. /**
  42. * @param \Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory
  43. * @param \Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory
  44. * @param \Magento\Framework\App\ResourceConnection $resource
  45. * @param \Magento\Framework\Acl\Data\CacheInterface $aclDataCache
  46. * @param Json $serializer
  47. * @param string $cacheKey
  48. */
  49. public function __construct(
  50. \Magento\Authorization\Model\Acl\Role\GroupFactory $groupFactory,
  51. \Magento\Authorization\Model\Acl\Role\UserFactory $roleFactory,
  52. \Magento\Framework\App\ResourceConnection $resource,
  53. \Magento\Framework\Acl\Data\CacheInterface $aclDataCache = null,
  54. Json $serializer = null,
  55. $cacheKey = self::ACL_ROLES_CACHE_KEY
  56. ) {
  57. $this->_resource = $resource;
  58. $this->_groupFactory = $groupFactory;
  59. $this->_roleFactory = $roleFactory;
  60. $this->aclDataCache = $aclDataCache ?: ObjectManager::getInstance()->get(
  61. \Magento\Framework\Acl\Data\CacheInterface::class
  62. );
  63. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  64. $this->cacheKey = $cacheKey;
  65. }
  66. /**
  67. * Populate ACL with roles from external storage
  68. *
  69. * @param \Magento\Framework\Acl $acl
  70. * @return void
  71. */
  72. public function populateAcl(\Magento\Framework\Acl $acl)
  73. {
  74. foreach ($this->getRolesArray() as $role) {
  75. $parent = $role['parent_id'] > 0 ? $role['parent_id'] : null;
  76. switch ($role['role_type']) {
  77. case RoleGroup::ROLE_TYPE:
  78. $acl->addRole($this->_groupFactory->create(['roleId' => $role['role_id']]), $parent);
  79. break;
  80. case RoleUser::ROLE_TYPE:
  81. if (!$acl->hasRole($role['role_id'])) {
  82. $acl->addRole($this->_roleFactory->create(['roleId' => $role['role_id']]), $parent);
  83. } else {
  84. $acl->addRoleParent($role['role_id'], $parent);
  85. }
  86. break;
  87. }
  88. }
  89. }
  90. /**
  91. * Get application ACL roles array
  92. *
  93. * @return array
  94. */
  95. private function getRolesArray()
  96. {
  97. $rolesCachedData = $this->aclDataCache->load($this->cacheKey);
  98. if ($rolesCachedData) {
  99. return $this->serializer->unserialize($rolesCachedData);
  100. }
  101. $roleTableName = $this->_resource->getTableName('authorization_role');
  102. $connection = $this->_resource->getConnection();
  103. $select = $connection->select()
  104. ->from($roleTableName)
  105. ->order('tree_level');
  106. $rolesArray = $connection->fetchAll($select);
  107. $this->aclDataCache->save($this->serializer->serialize($rolesArray), $this->cacheKey);
  108. return $rolesArray;
  109. }
  110. }