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: * Webservice API2 data helper
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Helper_Data extends Mage_Core_Helper_Abstract
35: {
36: /**
37: * Request interpret adapters
38: */
39: const XML_PATH_API2_REQUEST_INTERPRETERS = 'global/api2/request/interpreters';
40:
41: /**
42: * Response render adapters
43: */
44: const XML_PATH_API2_RESPONSE_RENDERS = 'global/api2/response/renders';
45:
46: /**#@+
47: * Config paths
48: */
49: const XML_PATH_AUTH_ADAPTERS = 'global/api2/auth_adapters';
50: const XML_PATH_USER_TYPES = 'global/api2/user_types';
51: /**#@- */
52:
53: /**
54: * Compare order to be used in adapters list sort
55: *
56: * @param int $a
57: * @param int $b
58: * @return int
59: */
60: protected static function _compareOrder($a, $b)
61: {
62: if ($a['order'] == $b['order']) {
63: return 0;
64: }
65: return ($a['order'] < $b['order']) ? -1 : 1;
66: }
67:
68: /**
69: * Retrieve Auth adapters info from configuration file as array
70: *
71: * @param bool $enabledOnly
72: * @return array
73: */
74: public function getAuthAdapters($enabledOnly = false)
75: {
76: $adapters = Mage::getConfig()->getNode(self::XML_PATH_AUTH_ADAPTERS);
77:
78: if (!$adapters) {
79: return array();
80: }
81: $adapters = $adapters->asArray();
82:
83: if ($enabledOnly) {
84: foreach ($adapters as $adapter) {
85: if (empty($adapter['enabled'])) {
86: unset($adapters);
87: }
88: }
89: $adapters = (array) $adapters;
90: }
91: uasort($adapters, array('Mage_Api2_Helper_Data', '_compareOrder'));
92:
93: return $adapters;
94: }
95:
96: /**
97: * Retrieve enabled user types in form of user type => user model pairs
98: *
99: * @return array
100: */
101: public function getUserTypes()
102: {
103: $userModels = array();
104: $types = Mage::getConfig()->getNode(self::XML_PATH_USER_TYPES);
105:
106: if ($types) {
107: foreach ($types->asArray() as $type => $params) {
108: if (!empty($params['allowed'])) {
109: $userModels[$type] = $params['model'];
110: }
111: }
112: }
113: return $userModels;
114: }
115:
116: /**
117: * Get interpreter type for Request body according to Content-type HTTP header
118: *
119: * @return array
120: */
121: public function getRequestInterpreterAdapters()
122: {
123: return (array) Mage::app()->getConfig()->getNode(self::XML_PATH_API2_REQUEST_INTERPRETERS);
124: }
125:
126: /**
127: * Get interpreter type for Request body according to Content-type HTTP header
128: *
129: * @return array
130: */
131: public function getResponseRenderAdapters()
132: {
133: return (array) Mage::app()->getConfig()->getNode(self::XML_PATH_API2_RESPONSE_RENDERS);
134: }
135:
136: /**
137: * Check API type support
138: *
139: * @param string $type
140: * @return bool
141: */
142: public function isApiTypeSupported($type)
143: {
144: return in_array($type, Mage_Api2_Model_Server::getApiTypes());
145: }
146:
147: /**
148: * Get allowed attributes of a rule
149: *
150: * @param string $userType
151: * @param string $resourceId
152: * @param string $operation One of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
153: * @return array
154: */
155: public function getAllowedAttributes($userType, $resourceId, $operation)
156: {
157: /** @var $resource Mage_Api2_Model_Resource_Acl_Filter_Attribute */
158: $resource = Mage::getResourceModel('api2/acl_filter_attribute');
159:
160: $attributes = $resource->getAllowedAttributes($userType, $resourceId, $operation);
161:
162: return ($attributes === false || $attributes === null ? array() : explode(',', $attributes));
163: }
164:
165: /**
166: * Check if ALL attributes are allowed
167: *
168: * @param string $userType
169: * @return bool
170: */
171: public function isAllAttributesAllowed($userType)
172: {
173: /** @var $resource Mage_Api2_Model_Resource_Acl_Filter_Attribute */
174: $resource = Mage::getResourceModel('api2/acl_filter_attribute');
175:
176: return $resource->isAllAttributesAllowed($userType);
177: }
178:
179: /**
180: * Get operation type for specified operation
181: *
182: * @param string $operation One of Mage_Api2_Model_Resource::OPERATION_... constant
183: * @return string One of Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_... constant
184: * @throws Exception
185: */
186: public function getTypeOfOperation($operation)
187: {
188: if (Mage_Api2_Model_Resource::OPERATION_RETRIEVE === $operation) {
189: return Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ;
190: } elseif (Mage_Api2_Model_Resource::OPERATION_CREATE === $operation) {
191: return Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_WRITE;
192: } elseif (Mage_Api2_Model_Resource::OPERATION_UPDATE === $operation) {
193: return Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_WRITE;
194: } else {
195: throw new Exception('Can not determine operation type');
196: }
197: }
198: }
199: