Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Role.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
20  protected $_ruleTable;
21 
27  protected $_cache;
28 
34  public function __construct(
35  \Magento\Framework\Model\ResourceModel\Db\Context $context,
36  \Magento\Framework\App\CacheInterface $cache,
37  $connectionName = null
38  ) {
39  parent::__construct($context, $connectionName);
40  $this->_cache = $cache->getFrontend();
41  }
42 
48  protected function _construct()
49  {
50  $this->_init('authorization_role', 'role_id');
51  $this->_ruleTable = $this->getTable('authorization_rule');
52  }
53 
60  protected function _beforeSave(\Magento\Framework\Model\AbstractModel $role)
61  {
62  if ($role->getId() == '') {
63  if ($role->getIdFieldName()) {
64  $role->unsetData($role->getIdFieldName());
65  } else {
66  $role->unsetData('id');
67  }
68  }
69 
70  if (!$role->getTreeLevel()) {
71  $treeLevel = 0;
72  if ($role->getPid() > 0) {
73  $select = $this->getConnection()->select()->from(
74  $this->getMainTable(),
75  ['tree_level']
76  )->where(
77  "{$this->getIdFieldName()} = :pid"
78  );
79 
80  $binds = ['pid' => (int)$role->getPid()];
81 
82  $treeLevel = $this->getConnection()->fetchOne($select, $binds);
83  }
84 
85  $role->setTreeLevel($treeLevel + 1);
86  }
87 
88  if ($role->getName()) {
89  $role->setRoleName($role->getName());
90  }
91 
92  return $this;
93  }
94 
102  protected function _afterSave(\Magento\Framework\Model\AbstractModel $role)
103  {
104  $this->_cache->clean(\Zend_Cache::CLEANING_MODE_MATCHING_TAG, [\Magento\Backend\Block\Menu::CACHE_TAGS]);
105  return $this;
106  }
107 
114  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $role)
115  {
116  $connection = $this->getConnection();
117 
118  $connection->delete($this->getMainTable(), ['parent_id = ?' => (int)$role->getId()]);
119 
120  $connection->delete($this->_ruleTable, ['role_id = ?' => (int)$role->getId()]);
121 
122  return $this;
123  }
124 
131  public function getRoleUsers(\Magento\Authorization\Model\Role $role)
132  {
133  $connection = $this->getConnection();
134 
135  $binds = ['role_id' => $role->getId(), 'role_type' => RoleUser::ROLE_TYPE];
136 
137  $select = $connection->select()
138  ->from($this->getMainTable(), ['user_id'])
139  ->where('parent_id = :role_id')
140  ->where('role_type = :role_type')
141  ->where('user_id > 0');
142 
143  return $connection->fetchCol($select, $binds);
144  }
145 }
getRoleUsers(\Magento\Authorization\Model\Role $role)
Definition: Role.php:131
_afterSave(\Magento\Framework\Model\AbstractModel $role)
Definition: Role.php:102
_beforeSave(\Magento\Framework\Model\AbstractModel $role)
Definition: Role.php:60
const CLEANING_MODE_MATCHING_TAG
Definition: Cache.php:74
$connection
Definition: bulk.php:13
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Framework\App\CacheInterface $cache, $connectionName=null)
Definition: Role.php:34
_afterDelete(\Magento\Framework\Model\AbstractModel $role)
Definition: Role.php:114