Registry.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Acl\Role;
  7. /**
  8. * Acl role registry. Contains list of roles and their relations.
  9. */
  10. class Registry extends \Zend_Acl_Role_Registry
  11. {
  12. /**
  13. * Add parent to the $role node
  14. *
  15. * @param \Zend_Acl_Role_Interface|string $role
  16. * @param array|\Zend_Acl_Role_Interface|string $parents
  17. * @return $this
  18. * @throws \Zend_Acl_Role_Registry_Exception
  19. */
  20. public function addParent($role, $parents)
  21. {
  22. try {
  23. if ($role instanceof \Zend_Acl_Role_Interface) {
  24. $roleId = $role->getRoleId();
  25. } else {
  26. $roleId = $role;
  27. $role = $this->get($role);
  28. }
  29. } catch (\Zend_Acl_Role_Registry_Exception $e) {
  30. throw new \Zend_Acl_Role_Registry_Exception("Child Role id '{$roleId}' does not exist");
  31. }
  32. if (!is_array($parents)) {
  33. $parents = [$parents];
  34. }
  35. foreach ($parents as $parent) {
  36. try {
  37. if ($parent instanceof \Zend_Acl_Role_Interface) {
  38. $roleParentId = $parent->getRoleId();
  39. } else {
  40. $roleParentId = $parent;
  41. }
  42. $roleParent = $this->get($roleParentId);
  43. } catch (\Zend_Acl_Role_Registry_Exception $e) {
  44. throw new \Zend_Acl_Role_Registry_Exception("Parent Role id '{$roleParentId}' does not exist");
  45. }
  46. $this->_roles[$roleId]['parents'][$roleParentId] = $roleParent;
  47. $this->_roles[$roleParentId]['children'][$roleId] = $role;
  48. }
  49. return $this;
  50. }
  51. }