ResourceLoader.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * ACL Resource Loader
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Acl\Loader;
  9. use Magento\Framework\Acl;
  10. use Magento\Framework\Acl\AclResource as AclResource;
  11. use Magento\Framework\Acl\AclResource\ProviderInterface;
  12. use Magento\Framework\Acl\AclResourceFactory;
  13. /**
  14. * ACL Loader
  15. */
  16. class ResourceLoader implements \Magento\Framework\Acl\LoaderInterface
  17. {
  18. /**
  19. * Acl resource config
  20. *
  21. * @var ProviderInterface
  22. */
  23. protected $_resourceProvider;
  24. /**
  25. * Resource factory
  26. *
  27. * @var AclResourceFactory
  28. */
  29. protected $_resourceFactory;
  30. /**
  31. * @param ProviderInterface $resourceProvider
  32. * @param AclResourceFactory $resourceFactory
  33. */
  34. public function __construct(ProviderInterface $resourceProvider, AclResourceFactory $resourceFactory)
  35. {
  36. $this->_resourceProvider = $resourceProvider;
  37. $this->_resourceFactory = $resourceFactory;
  38. }
  39. /**
  40. * Populate ACL with resources from external storage
  41. *
  42. * @param Acl $acl
  43. * @return void
  44. * @throws \Zend_Acl_Exception
  45. */
  46. public function populateAcl(Acl $acl)
  47. {
  48. $this->_addResourceTree($acl, $this->_resourceProvider->getAclResources(), null);
  49. }
  50. /**
  51. * Add list of nodes and their children to acl
  52. *
  53. * @param Acl $acl
  54. * @param array $resources
  55. * @param AclResource $parent
  56. * @return void
  57. * @throws \InvalidArgumentException
  58. * @throws \Zend_Acl_Exception
  59. */
  60. protected function _addResourceTree(Acl $acl, array $resources, AclResource $parent = null)
  61. {
  62. foreach ($resources as $resourceConfig) {
  63. if (!isset($resourceConfig['id'])) {
  64. throw new \InvalidArgumentException('Missing ACL resource identifier');
  65. }
  66. /** @var $resource AclResource */
  67. $resource = $this->_resourceFactory->createResource(['resourceId' => $resourceConfig['id']]);
  68. $acl->addResource($resource, $parent);
  69. if (isset($resourceConfig['children'])) {
  70. $this->_addResourceTree($acl, $resourceConfig['children'], $resource);
  71. }
  72. }
  73. }
  74. }