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_Api2
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: * API2 Global ACL Role model
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: * @method Mage_Api2_Model_Resource_Acl_Global_Role_Collection getCollection()
34: * @method Mage_Api2_Model_Resource_Acl_Global_Role_Collection getResourceCollection()
35: * @method Mage_Api2_Model_Resource_Acl_Global_Role getResource()
36: * @method Mage_Api2_Model_Resource_Acl_Global_Role _getResource()
37: * @method string getCreatedAt()
38: * @method Mage_Api2_Model_Acl_Global_Role setCreatedAt() setCreatedAt(string $createdAt)
39: * @method string getUpdatedAt()
40: * @method Mage_Api2_Model_Acl_Global_Role setUpdatedAt() setUpdatedAt(string $updatedAt)
41: * @method string getRoleName()
42: * @method Mage_Api2_Model_Acl_Global_Role setRoleName() setRoleName(string $roleName)
43: */
44: class Mage_Api2_Model_Acl_Global_Role extends Mage_Core_Model_Abstract
45: {
46: /**#@+
47: * System roles identifiers
48: */
49: const ROLE_GUEST_ID = 1;
50: const ROLE_CUSTOMER_ID = 2;
51: /**#@-*/
52:
53: /**#@+
54: * Config node identifiers
55: */
56: const ROLE_CONFIG_NODE_NAME_GUEST = 'guest';
57: const ROLE_CONFIG_NODE_NAME_CUSTOMER = 'customer';
58: const ROLE_CONFIG_NODE_NAME_ADMIN = 'admin';
59: /**#@-*/
60:
61: /**
62: * Permissions model
63: *
64: * @var Mage_Api2_Model_Acl_Global_Rule_ResourcePermission
65: */
66: protected $_permissionModel;
67:
68: /**
69: * Initialize resource model
70: *
71: * @return void
72: */
73: protected function _construct()
74: {
75: $this->_init('api2/acl_global_role');
76: }
77:
78: /**
79: * Before save actions
80: *
81: * @return Mage_Api2_Model_Acl_Global_Role
82: */
83: protected function _beforeSave()
84: {
85: if ($this->isObjectNew() && null === $this->getCreatedAt()) {
86: $this->setCreatedAt(Varien_Date::now());
87: } else {
88: $this->setUpdatedAt(Varien_Date::now());
89: }
90:
91: //check and protect guest role
92: if (Mage_Api2_Model_Acl_Global_Role::isSystemRole($this)
93: && $this->getRoleName() != $this->getOrigData('role_name')) {
94:
95: /** @var $helper Mage_Core_Helper_Data */
96: $helper = Mage::helper('core');
97:
98: Mage::throwException(
99: Mage::helper('api2')->__('%s role is a special one and can\'t be changed.',
100: $helper->escapeHtml($this->getRoleName()))
101: );
102: }
103:
104: parent::_beforeSave();
105: return $this;
106: }
107:
108: /**
109: * Perform checks before role delete
110: *
111: * @return Mage_Api2_Model_Acl_Global_Role
112: */
113: protected function _beforeDelete()
114: {
115: if (Mage_Api2_Model_Acl_Global_Role::isSystemRole($this)) {
116: /** @var $helper Mage_Core_Helper_Data */
117: $helper = Mage::helper('core');
118:
119: Mage::throwException(
120: Mage::helper('api2')->__('%s role is a special one and can\'t be deleted.',
121: $helper->escapeHtml($this->getRoleName()))
122: );
123: }
124:
125: parent::_beforeDelete();
126: return $this;
127: }
128:
129: /**
130: * Get pairs resources-permissions for current role
131: *
132: * @return Mage_Api2_Model_Acl_Global_Rule_ResourcePermission
133: */
134: public function getPermissionModel()
135: {
136: if (null == $this->_permissionModel) {
137: $this->_permissionModel = Mage::getModel('api2/acl_global_rule_resourcePermission');
138: }
139: return $this->_permissionModel;
140: }
141:
142: /**
143: * Retrieve system roles
144: *
145: * @return array
146: */
147: static public function getSystemRoles()
148: {
149: return array(
150: self::ROLE_GUEST_ID,
151: self::ROLE_CUSTOMER_ID
152: );
153: }
154:
155: /**
156: * Get role system belonging
157: *
158: * @param Mage_Api2_Model_Acl_Global_Role $role
159: * @return bool
160: */
161: public static function isSystemRole($role)
162: {
163: return in_array($role->getId(), self::getSystemRoles());
164: }
165:
166: /**
167: * Get config node identifiers
168: *
169: * @return string
170: */
171: public function getConfigNodeName()
172: {
173: switch ($this->getId()) {
174: case self::ROLE_GUEST_ID:
175: $roleNodeName = self::ROLE_CONFIG_NODE_NAME_GUEST;
176: break;
177: case self::ROLE_CUSTOMER_ID:
178: $roleNodeName = self::ROLE_CONFIG_NODE_NAME_CUSTOMER;
179: break;
180: default:
181: $roleNodeName = self::ROLE_CONFIG_NODE_NAME_ADMIN;
182: }
183: return $roleNodeName;
184: }
185: }
186: