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_Admin
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: * Admin Roles Model
29: *
30: * @method Mage_Admin_Model_Resource_Roles _getResource()
31: * @method Mage_Admin_Model_Resource_Roles getResource()
32: * @method int getParentId()
33: * @method Mage_Admin_Model_Roles setParentId(int $value)
34: * @method int getTreeLevel()
35: * @method Mage_Admin_Model_Roles setTreeLevel(int $value)
36: * @method int getSortOrder()
37: * @method Mage_Admin_Model_Roles setSortOrder(int $value)
38: * @method string getRoleType()
39: * @method Mage_Admin_Model_Roles setRoleType(string $value)
40: * @method int getUserId()
41: * @method Mage_Admin_Model_Roles setUserId(int $value)
42: * @method string getRoleName()
43: * @method Mage_Admin_Model_Roles setRoleName(string $value)
44: *
45: * @category Mage
46: * @package Mage_Admin
47: * @author Magento Core Team <core@magentocommerce.com>
48: */
49: class Mage_Admin_Model_Roles extends Mage_Core_Model_Abstract
50: {
51: /**
52: * @var string
53: */
54: protected $_eventPrefix = 'admin_roles';
55:
56: /**
57: * Constructor
58: */
59: protected function _construct()
60: {
61: $this->_init('admin/roles');
62: }
63:
64: /**
65: * Update object into database
66: *
67: * @return Mage_Admin_Model_Roles
68: */
69: public function update()
70: {
71: $this->getResource()->update($this);
72: return $this;
73: }
74:
75: /**
76: * Retrieve users collection
77: *
78: * @return Mage_Admin_Model_Resource_Roles_User_Collection
79: */
80: public function getUsersCollection()
81: {
82: return Mage::getResourceModel('admin/roles_user_collection');
83: }
84:
85: /**
86: * Return tree of acl resources
87: *
88: * @return array|null|Varien_Simplexml_Element
89: */
90: public function getResourcesTree()
91: {
92: return $this->_buildResourcesArray(null, null, null, null, true);
93: }
94:
95: /**
96: * Return list of acl resources
97: *
98: * @return array|null|Varien_Simplexml_Element
99: */
100: public function getResourcesList()
101: {
102: return $this->_buildResourcesArray();
103: }
104:
105: /**
106: * Return list of acl resources in 2D format
107: *
108: * @return array|null|Varien_Simplexml_Element
109: */
110: public function getResourcesList2D()
111: {
112: return $this->_buildResourcesArray(null, null, null, true);
113: }
114:
115: /**
116: * Return users for role
117: *
118: * @return array|false
119: */
120: public function getRoleUsers()
121: {
122: return $this->getResource()->getRoleUsers($this);
123: }
124:
125: /**
126: * Build resources array process
127: *
128: * @param null|Varien_Simplexml_Element $resource
129: * @param null $parentName
130: * @param int $level
131: * @param null $represent2Darray
132: * @param bool $rawNodes
133: * @param string $module
134: * @return array|null|Varien_Simplexml_Element
135: */
136: protected function _buildResourcesArray(Varien_Simplexml_Element $resource = null,
137: $parentName = null, $level = 0, $represent2Darray = null, $rawNodes = false, $module = 'adminhtml')
138: {
139: static $result;
140: if (is_null($resource)) {
141: $resource = Mage::getSingleton('admin/config')->getAdminhtmlConfig()->getNode('acl/resources');
142: $resourceName = null;
143: $level = -1;
144: } else {
145: $resourceName = $parentName;
146: if (!in_array($resource->getName(), array('title', 'sort_order', 'children', 'disabled'))) {
147: $resourceName = (is_null($parentName) ? '' : $parentName . '/') . $resource->getName();
148:
149: //assigning module for its' children nodes
150: if ($resource->getAttribute('module')) {
151: $module = (string)$resource->getAttribute('module');
152: }
153:
154: if ($rawNodes) {
155: $resource->addAttribute("aclpath", $resourceName);
156: $resource->addAttribute("module_c", $module);
157: }
158:
159: if ( is_null($represent2Darray) ) {
160: $result[$resourceName]['name'] = Mage::helper($module)->__((string)$resource->title);
161: $result[$resourceName]['level'] = $level;
162: } else {
163: $result[] = $resourceName;
164: }
165: }
166: }
167:
168: //check children and run recursion if they exists
169: $children = $resource->children();
170: foreach ($children as $key => $child) {
171: if (1 == $child->disabled) {
172: $resource->{$key} = null;
173: continue;
174: }
175: $this->_buildResourcesArray($child, $resourceName, $level + 1, $represent2Darray, $rawNodes, $module);
176: }
177:
178: if ($rawNodes) {
179: return $resource;
180: } else {
181: return $result;
182: }
183: }
184: }
185: