Role.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Authorization\Model\ResourceModel;
  7. use Magento\Authorization\Model\Acl\Role\User as RoleUser;
  8. /**
  9. * Admin role resource model
  10. */
  11. class Role extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  12. {
  13. /**
  14. * Rule table
  15. *
  16. * @var string
  17. */
  18. protected $_ruleTable;
  19. /**
  20. * Cache
  21. *
  22. * @var \Magento\Framework\Cache\FrontendInterface
  23. */
  24. protected $_cache;
  25. /**
  26. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  27. * @param \Magento\Framework\App\CacheInterface $cache
  28. * @param string $connectionName
  29. */
  30. public function __construct(
  31. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  32. \Magento\Framework\App\CacheInterface $cache,
  33. $connectionName = null
  34. ) {
  35. parent::__construct($context, $connectionName);
  36. $this->_cache = $cache->getFrontend();
  37. }
  38. /**
  39. * Define main table
  40. *
  41. * @return void
  42. */
  43. protected function _construct()
  44. {
  45. $this->_init('authorization_role', 'role_id');
  46. $this->_ruleTable = $this->getTable('authorization_rule');
  47. }
  48. /**
  49. * Process role before saving
  50. *
  51. * @param \Magento\Framework\Model\AbstractModel $role
  52. * @return $this
  53. */
  54. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $role)
  55. {
  56. if ($role->getId() == '') {
  57. if ($role->getIdFieldName()) {
  58. $role->unsetData($role->getIdFieldName());
  59. } else {
  60. $role->unsetData('id');
  61. }
  62. }
  63. if (!$role->getTreeLevel()) {
  64. $treeLevel = 0;
  65. if ($role->getPid() > 0) {
  66. $select = $this->getConnection()->select()->from(
  67. $this->getMainTable(),
  68. ['tree_level']
  69. )->where(
  70. "{$this->getIdFieldName()} = :pid"
  71. );
  72. $binds = ['pid' => (int)$role->getPid()];
  73. $treeLevel = $this->getConnection()->fetchOne($select, $binds);
  74. }
  75. $role->setTreeLevel($treeLevel + 1);
  76. }
  77. if ($role->getName()) {
  78. $role->setRoleName($role->getName());
  79. }
  80. return $this;
  81. }
  82. /**
  83. * Process role after saving
  84. *
  85. * @param \Magento\Framework\Model\AbstractModel $role
  86. * @return $this
  87. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  88. */
  89. protected function _afterSave(\Magento\Framework\Model\AbstractModel $role)
  90. {
  91. $this->_cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [\Magento\Backend\Block\Menu::CACHE_TAGS]);
  92. return $this;
  93. }
  94. /**
  95. * Process role after deleting
  96. *
  97. * @param \Magento\Framework\Model\AbstractModel $role
  98. * @return $this
  99. */
  100. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $role)
  101. {
  102. $connection = $this->getConnection();
  103. $connection->delete($this->getMainTable(), ['parent_id = ?' => (int)$role->getId()]);
  104. $connection->delete($this->_ruleTable, ['role_id = ?' => (int)$role->getId()]);
  105. return $this;
  106. }
  107. /**
  108. * Get role users
  109. *
  110. * @param \Magento\Authorization\Model\Role $role
  111. * @return array
  112. */
  113. public function getRoleUsers(\Magento\Authorization\Model\Role $role)
  114. {
  115. $connection = $this->getConnection();
  116. $binds = ['role_id' => $role->getId(), 'role_type' => RoleUser::ROLE_TYPE];
  117. $select = $connection->select()
  118. ->from($this->getMainTable(), ['user_id'])
  119. ->where('parent_id = :role_id')
  120. ->where('role_type = :role_type')
  121. ->where('user_id > 0');
  122. return $connection->fetchCol($select, $binds);
  123. }
  124. }