1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Api
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27:
28: /**
29: * ACL roles resource
30: *
31: * @category Mage
32: * @package Mage_Api
33: * @author Magento Core Team <core@magentocommerce.com>
34: */
35: class Mage_Api_Model_Resource_Roles extends Mage_Core_Model_Resource_Db_Abstract
36: {
37: /**
38: * User table name
39: *
40: * @var unknown
41: */
42: protected $_usersTable;
43:
44: /**
45: * Rule table name
46: *
47: * @var unknown
48: */
49: protected $_ruleTable;
50:
51: /**
52: * Resource initialization
53: *
54: */
55: protected function _construct()
56: {
57: $this->_init('api/role', 'role_id');
58:
59: $this->_usersTable = $this->getTable('api/user');
60: $this->_ruleTable = $this->getTable('api/rule');
61: }
62:
63: /**
64: * Action before save
65: *
66: * @param Mage_Core_Model_Abstract $role
67: * @return Mage_Api_Model_Resource_Roles
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: $row = $this->load($role->getPid());
81: } else {
82: $row = array('tree_level' => 0);
83: }
84: $role->setTreeLevel($row['tree_level'] + 1);
85: $role->setRoleName($role->getName());
86: return $this;
87: }
88:
89: /**
90: * Action after save
91: *
92: * @param Mage_Core_Model_Abstract $role
93: * @return Mage_Api_Model_Resource_Roles
94: */
95: protected function _afterSave(Mage_Core_Model_Abstract $role)
96: {
97: $this->_updateRoleUsersAcl($role);
98: Mage::app()->getCache()->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG);
99: return $this;
100: }
101:
102: /**
103: * Action after delete
104: *
105: * @param Mage_Core_Model_Abstract $role
106: * @return Mage_Api_Model_Resource_Roles
107: */
108: protected function _afterDelete(Mage_Core_Model_Abstract $role)
109: {
110: $adapter = $this->_getWriteAdapter();
111: $adapter->delete($this->getMainTable(), array('parent_id=?'=>$role->getId()));
112: $adapter->delete($this->_ruleTable, array('role_id=?'=>$role->getId()));
113: return $this;
114: }
115:
116: /**
117: * Get role users
118: *
119: * @param Mage_Api_Model_Roles $role
120: * @return unknown
121: */
122: public function getRoleUsers(Mage_Api_Model_Roles $role)
123: {
124: $adapter = $this->_getReadAdapter();
125: $select = $adapter->select()
126: ->from($this->getMainTable(), array('user_id'))
127: ->where('parent_id = ?', $role->getId())
128: ->where('role_type = ?', Mage_Api_Model_Acl::ROLE_TYPE_USER)
129: ->where('user_id > 0');
130: return $adapter->fetchCol($select);
131: }
132:
133: /**
134: * Update role users
135: *
136: * @param Mage_Api_Model_Roles $role
137: * @return boolean
138: */
139: private function _updateRoleUsersAcl(Mage_Api_Model_Roles $role)
140: {
141: $users = $this->getRoleUsers($role);
142: $rowsCount = 0;
143: if (sizeof($users) > 0) {
144: $rowsCount = $this->_getWriteAdapter()->update(
145: $this->_usersTable,
146: array('reload_acl_flag' => 1),
147: array('user_id IN(?)' => $users));
148: }
149: return ($rowsCount > 0) ? true : false;
150: }
151: }
152: