1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Admin_Model_Resource_Roles extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: 38: 39: 40: 41:
42: protected $_usersTable;
43:
44: 45: 46: 47: 48:
49: protected $_ruleTable;
50:
51: 52: 53: 54:
55: protected function _construct()
56: {
57: $this->_init('admin/role', 'role_id');
58:
59: $this->_usersTable = $this->getTable('admin/user');
60: $this->_ruleTable = $this->getTable('admin/rule');
61: }
62:
63: 64: 65: 66: 67: 68:
69: protected function _beforeSave(Mage_Core_Model_Abstract $role)
70: {
71: if ($role->getId() == '') {
72: if ($role->getIdFieldName()) {
73: $role->unsetData($role->getIdFieldName());
74: } else {
75: $role->unsetData('id');
76: }
77: }
78:
79: if ($role->getPid() > 0) {
80: $select = $this->_getReadAdapter()->select()
81: ->from($this->getMainTable(), array('tree_level'))
82: ->where("{$this->getIdFieldName()} = :pid");
83:
84: $binds = array(
85: 'pid' => (int) $role->getPid(),
86: );
87:
88: $treeLevel = $this->_getReadAdapter()->fetchOne($select, $binds);
89: } else {
90: $treeLevel = 0;
91: }
92: $role->setTreeLevel($treeLevel + 1);
93: $role->setRoleName($role->getName());
94: return $this;
95: }
96:
97: 98: 99: 100: 101: 102:
103: protected function _afterSave(Mage_Core_Model_Abstract $role)
104: {
105: $this->_updateRoleUsersAcl($role);
106: Mage::app()->getCache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,
107: array(Mage_Adminhtml_Block_Page_Menu::CACHE_TAGS));
108: return $this;
109: }
110:
111: 112: 113: 114: 115: 116:
117: protected function _afterDelete(Mage_Core_Model_Abstract $role)
118: {
119: $adapter = $this->_getWriteAdapter();
120:
121: $adapter->delete(
122: $this->getMainTable(),
123: array('parent_id = ?' => (int) $role->getId())
124: );
125:
126: $adapter->delete(
127: $this->_ruleTable,
128: array('role_id = ?' => (int) $role->getId())
129: );
130:
131: return $this;
132: }
133:
134: 135: 136: 137: 138: 139:
140: public function getRoleUsers(Mage_Admin_Model_Roles $role)
141: {
142: $read = $this->_getReadAdapter();
143:
144: $binds = array(
145: 'role_id' => $role->getId(),
146: 'role_type' => 'U'
147: );
148:
149: $select = $read->select()
150: ->from($this->getMainTable(), array('user_id'))
151: ->where('parent_id = :role_id')
152: ->where('role_type = :role_type')
153: ->where('user_id > 0');
154:
155: return $read->fetchCol($select, $binds);
156: }
157:
158: 159: 160: 161: 162: 163:
164: private function _updateRoleUsersAcl(Mage_Admin_Model_Roles $role)
165: {
166: $write = $this->_getWriteAdapter();
167: $users = $this->getRoleUsers($role);
168: $rowsCount = 0;
169:
170: if (sizeof($users) > 0) {
171: $bind = array('reload_acl_flag' => 1);
172: $where = array('user_id IN(?)' => $users);
173: $rowsCount = $write->update($this->_usersTable, $bind, $where);
174: }
175:
176: return $rowsCount > 0;
177: }
178: }
179: