Skip to content

Commit bd3be01

Browse files
author
Bryant Luk
committed
MAGETWO-36063: Update REST and SOAP controllers to filter out attributes based on ACL
- Add CustomAttributesProcessor
1 parent e53f09d commit bd3be01

File tree

3 files changed

+99
-73
lines changed

3 files changed

+99
-73
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Reflection;
8+
9+
use Magento\Framework\Phrase;
10+
use Magento\Framework\Api\SimpleDataObjectConverter;
11+
use Zend\Code\Reflection\MethodReflection;
12+
use Magento\Framework\Api\CustomAttributesDataInterface;
13+
use Magento\Framework\Api\AttributeTypeResolverInterface;
14+
15+
/**
16+
* Processes custom attributes and produces an array for the data.
17+
*/
18+
class CustomAttributesProcessor
19+
{
20+
/**
21+
* @var DataObjectProcessor
22+
*/
23+
private $dataObjectProcessor;
24+
25+
/**
26+
* @var AttributeTypeResolverInterface
27+
*/
28+
private $attributeTypeResolver;
29+
30+
/**
31+
* @param DataObjectProcessor $dataObjectProcessor
32+
* @param AttributeTypeResolverInterface $typeResolver
33+
*/
34+
public function __construct(
35+
DataObjectProcessor $dataObjectProcessor,
36+
AttributeTypeResolverInterface $typeResolver
37+
) {
38+
$this->dataObjectProcessor = $dataObjectProcessor;
39+
$this->attributeTypeResolver = $typeResolver;
40+
}
41+
42+
/**
43+
* Writes out the custom attributes for a given object into an array.
44+
*
45+
* @param CustomAttributesDataInterface $objectWithCustomAttributes
46+
* @param string $dataObjectType
47+
* @return array
48+
*/
49+
public function buildOutputDataArray(CustomAttributesDataInterface $objectWithCustomAttributes, $dataObjectType)
50+
{
51+
$customAttributes = $objectWithCustomAttributes->getCustomAttributes();
52+
$result = [];
53+
foreach ($customAttributes as $customAttribute) {
54+
$result[] = $this->convertCustomAttribute($customAttribute, $dataObjectType);
55+
}
56+
return $result;
57+
}
58+
59+
/**
60+
* Convert custom_attribute object to use flat array structure
61+
*
62+
* @param \Magento\Framework\Api\AttributeInterface $customAttribute
63+
* @param string $dataObjectType
64+
* @return array
65+
*/
66+
private function convertCustomAttribute($customAttribute, $dataObjectType)
67+
{
68+
$data = [];
69+
$data[AttributeValue::ATTRIBUTE_CODE] = $customAttribute->getAttributeCode();
70+
$value = $customAttribute->getValue();
71+
if (is_object($value)) {
72+
$type = $this->attributeTypeResolver->resolveObjectType(
73+
$customAttribute->getAttributeCode(),
74+
$value,
75+
$dataObjectType
76+
);
77+
$value = $this->dataObjectProcessor->buildOutputDataArray($value, $type);
78+
} elseif (is_array($value)) {
79+
$valueResult = [];
80+
foreach ($value as $singleValue) {
81+
if (is_object($singleValue)) {
82+
$type = $this->attributeTypeResolver->resolveObjectType(
83+
$customAttribute->getAttributeCode(),
84+
$singleValue,
85+
$dataObjectType
86+
);
87+
$singleValue = $this->dataObjectProcessor->buildOutputDataArray($singleValue, $type);
88+
}
89+
// Cannot cast to a type because the type is unknown
90+
$valueResult[] = $singleValue;
91+
}
92+
$value = $valueResult;
93+
}
94+
$data[AttributeValue::VALUE] = $value;
95+
return $data;
96+
}
97+
}

lib/internal/Magento/Framework/Reflection/DataObjectProcessor.php

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@
1818
*/
1919
class DataObjectProcessor
2020
{
21-
const IS_METHOD_PREFIX = 'is';
22-
const HAS_METHOD_PREFIX = 'has';
23-
const GETTER_PREFIX = 'get';
24-
25-
/**
26-
* @var \Magento\Framework\Api\AttributeTypeResolverInterface
27-
*/
28-
protected $attributeTypeResolver;
29-
3021
/**
3122
* @var MethodsMap
3223
*/
@@ -48,20 +39,17 @@ class DataObjectProcessor
4839
private $fieldNamer;
4940

5041
/**
51-
* @param \Magento\Framework\Api\AttributeTypeResolverInterface $typeResolver
5242
* @param MethodsMap $methodsMapProcessor
5343
* @param ExtensionAttributesProcessor $extensionAttributesProcessor
5444
* @param TypeCaster $typeCaster
5545
* @param FieldNamer $fieldNamer
5646
*/
5747
public function __construct(
58-
\Magento\Framework\Api\AttributeTypeResolverInterface $typeResolver,
5948
MethodsMap $methodsMapProcessor,
6049
ExtensionAttributesProcessor $extensionAttributesProcessor,
6150
TypeCaster $typeCaster,
6251
FieldNamer $fieldNamer
6352
) {
64-
$this->attributeTypeResolver = $typeResolver;
6553
$this->methodsMapProcessor = $methodsMapProcessor;
6654
$this->extensionAttributesProcessor = $extensionAttributesProcessor;
6755
$this->typeCaster = $typeCaster;
@@ -100,7 +88,7 @@ public function buildOutputDataArray($dataObject, $dataObjectType)
10088
}
10189

10290
if ($key === CustomAttributesDataInterface::CUSTOM_ATTRIBUTES) {
103-
$value = $this->convertCustomAttributes($value, $dataObjectType);
91+
$value = $this->customAttributesProcessor->buildOutputDataArray($dataObject, $dataObjectType);
10492
} elseif ($key === "extension_attributes") {
10593
$value = $this->extensionAttributesProcessor->buildOutputDataArray($value, $returnType);
10694
} else {
@@ -125,59 +113,4 @@ public function buildOutputDataArray($dataObject, $dataObjectType)
125113
}
126114
return $outputData;
127115
}
128-
129-
/**
130-
* Convert array of custom_attributes to use flat array structure
131-
*
132-
* @param \Magento\Framework\Api\AttributeInterface[] $customAttributes
133-
* @param string $dataObjectType
134-
* @return array
135-
*/
136-
protected function convertCustomAttributes($customAttributes, $dataObjectType)
137-
{
138-
$result = [];
139-
foreach ((array)$customAttributes as $customAttribute) {
140-
$result[] = $this->convertCustomAttribute($customAttribute, $dataObjectType);
141-
}
142-
return $result;
143-
}
144-
145-
/**
146-
* Convert custom_attribute object to use flat array structure
147-
*
148-
* @param \Magento\Framework\Api\AttributeInterface $customAttribute
149-
* @param string $dataObjectType
150-
* @return array
151-
*/
152-
protected function convertCustomAttribute($customAttribute, $dataObjectType)
153-
{
154-
$data = [];
155-
$data[AttributeValue::ATTRIBUTE_CODE] = $customAttribute->getAttributeCode();
156-
$value = $customAttribute->getValue();
157-
if (is_object($value)) {
158-
$type = $this->attributeTypeResolver->resolveObjectType(
159-
$customAttribute->getAttributeCode(),
160-
$value,
161-
$dataObjectType
162-
);
163-
$value = $this->buildOutputDataArray($value, $type);
164-
} elseif (is_array($value)) {
165-
$valueResult = [];
166-
foreach ($value as $singleValue) {
167-
if (is_object($singleValue)) {
168-
$type = $this->attributeTypeResolver->resolveObjectType(
169-
$customAttribute->getAttributeCode(),
170-
$singleValue,
171-
$dataObjectType
172-
);
173-
$singleValue = $this->buildOutputDataArray($singleValue, $type);
174-
}
175-
// Cannot cast to a type because the type is unknown
176-
$valueResult[] = $singleValue;
177-
}
178-
$value = $valueResult;
179-
}
180-
$data[AttributeValue::VALUE] = $value;
181-
return $data;
182-
}
183116
}

lib/internal/Magento/Framework/Reflection/ExtensionAttributesProcessor.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,10 @@
1515
use Zend\Code\Reflection\MethodReflection;
1616

1717
/**
18-
* Processes extension attributes and produces a PHP array for the data.
18+
* Processes extension attributes and produces an array for the data.
1919
*/
2020
class ExtensionAttributesProcessor
2121
{
22-
const IS_METHOD_PREFIX = 'is';
23-
const HAS_METHOD_PREFIX = 'has';
24-
const GETTER_PREFIX = 'get';
25-
2622
/**
2723
* @var DataObjectProcessor
2824
*/

0 commit comments

Comments
 (0)