AuthorizationService.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model;
  7. use Magento\Authorization\Model\ResourceModel\Role\CollectionFactory as RoleCollectionFactory;
  8. use Magento\Authorization\Model\ResourceModel\Rules\CollectionFactory as RulesCollectionFactory;
  9. use Magento\Authorization\Model\Role;
  10. use Magento\Authorization\Model\RoleFactory;
  11. use Magento\Authorization\Model\RulesFactory;
  12. use Magento\Authorization\Model\UserContextInterface;
  13. use Magento\Framework\Acl;
  14. use Magento\Framework\Acl\Builder as AclBuilder;
  15. use Magento\Framework\Acl\RootResource as RootAclResource;
  16. use Magento\Framework\Exception\LocalizedException;
  17. use Psr\Log\LoggerInterface as Logger;
  18. /**
  19. * Service for integration permissions management.
  20. *
  21. * @SuppressWarnings(PHPMD.LongVariable)
  22. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  23. */
  24. class AuthorizationService implements \Magento\Integration\Api\AuthorizationServiceInterface
  25. {
  26. /**
  27. * @var AclBuilder
  28. */
  29. protected $_aclBuilder;
  30. /**
  31. * @var RoleFactory
  32. */
  33. protected $_roleFactory;
  34. /**
  35. * @var RoleCollectionFactory
  36. */
  37. protected $_roleCollectionFactory;
  38. /**
  39. * @var RulesFactory
  40. */
  41. protected $_rulesFactory;
  42. /**
  43. * @var RulesCollectionFactory
  44. */
  45. protected $_rulesCollectionFactory;
  46. /**
  47. * @var Logger
  48. */
  49. protected $_logger;
  50. /**
  51. * @var RootAclResource
  52. */
  53. protected $_rootAclResource;
  54. /**
  55. * Initialize dependencies.
  56. *
  57. * @param AclBuilder $aclBuilder
  58. * @param RoleFactory $roleFactory
  59. * @param RoleCollectionFactory $roleCollectionFactory
  60. * @param RulesFactory $rulesFactory
  61. * @param RulesCollectionFactory $rulesCollectionFactory
  62. * @param Logger $logger
  63. * @param RootAclResource $rootAclResource
  64. */
  65. public function __construct(
  66. AclBuilder $aclBuilder,
  67. RoleFactory $roleFactory,
  68. RoleCollectionFactory $roleCollectionFactory,
  69. RulesFactory $rulesFactory,
  70. RulesCollectionFactory $rulesCollectionFactory,
  71. Logger $logger,
  72. RootAclResource $rootAclResource
  73. ) {
  74. $this->_aclBuilder = $aclBuilder;
  75. $this->_roleFactory = $roleFactory;
  76. $this->_rulesFactory = $rulesFactory;
  77. $this->_rulesCollectionFactory = $rulesCollectionFactory;
  78. $this->_roleCollectionFactory = $roleCollectionFactory;
  79. $this->_logger = $logger;
  80. $this->_rootAclResource = $rootAclResource;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function grantPermissions($integrationId, $resources)
  86. {
  87. try {
  88. $role = $this->_getUserRole($integrationId);
  89. if (!$role) {
  90. $role = $this->_createRole($integrationId);
  91. }
  92. $this->_associateResourcesWithRole($role, $resources);
  93. } catch (\Exception $e) {
  94. $this->_logger->critical($e);
  95. throw new LocalizedException(
  96. __('An error occurred during the attempt to grant permissions. For details, see the exceptions log.')
  97. );
  98. }
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function grantAllPermissions($integrationId)
  104. {
  105. $this->grantPermissions($integrationId, [$this->_rootAclResource->getId()]);
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function removePermissions($integrationId)
  111. {
  112. try {
  113. $this->_deleteRole($integrationId);
  114. } catch (\Exception $e) {
  115. $this->_logger->critical($e);
  116. throw new LocalizedException(
  117. __(
  118. 'Something went wrong while deleting roles and permissions.'
  119. . ' You can find out more in the exceptions log.'
  120. )
  121. );
  122. }
  123. }
  124. /**
  125. * Create new ACL role.
  126. *
  127. * @param int $integrationId
  128. * @return \Magento\Authorization\Model\Role
  129. */
  130. protected function _createRole($integrationId)
  131. {
  132. $roleName = UserContextInterface::USER_TYPE_INTEGRATION . $integrationId;
  133. $role = $this->_roleFactory->create();
  134. $role->setRoleName($roleName)
  135. ->setUserType(UserContextInterface::USER_TYPE_INTEGRATION)
  136. ->setUserId($integrationId)
  137. ->setRoleType(\Magento\Authorization\Model\Acl\Role\User::ROLE_TYPE)
  138. ->setParentId(0)
  139. ->save();
  140. return $role;
  141. }
  142. /**
  143. * Remove integration role. This deletes the cascading permissions
  144. *
  145. * @param int $integrationId
  146. * @return \Magento\Authorization\Model\Role
  147. */
  148. protected function _deleteRole($integrationId)
  149. {
  150. $roleName = UserContextInterface::USER_TYPE_INTEGRATION . $integrationId;
  151. $role = $this->_roleFactory->create()->load($roleName, 'role_name');
  152. return $role->delete();
  153. }
  154. /**
  155. * Identify authorization role associated with provided integration.
  156. *
  157. * @param int $integrationId
  158. * @return \Magento\Authorization\Model\Role|false Return false in case when no role associated with user was found.
  159. */
  160. protected function _getUserRole($integrationId)
  161. {
  162. $roleCollection = $this->_roleCollectionFactory->create();
  163. /** @var Role $role */
  164. $role = $roleCollection
  165. ->setUserFilter($integrationId, UserContextInterface::USER_TYPE_INTEGRATION)
  166. ->getFirstItem();
  167. return $role->getId() ? $role : false;
  168. }
  169. /**
  170. * Associate resources with the specified role. All resources previously assigned to the role will be unassigned.
  171. *
  172. * @param \Magento\Authorization\Model\Role $role
  173. * @param string[] $resources
  174. * @return void
  175. * @throws \LogicException
  176. */
  177. protected function _associateResourcesWithRole($role, $resources)
  178. {
  179. /** @var \Magento\Authorization\Model\Rules $rules */
  180. $rules = $this->_rulesFactory->create();
  181. $rules->setRoleId($role->getId())->setResources($resources)->saveRel();
  182. }
  183. }