1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Integration\Model\Config\Consolidated;
/**
* Converter of integration.xml content into array format.
*/
class Converter implements \Magento\Framework\Config\ConverterInterface
{
/**#@+
* Array keys for config internal representation.
*/
const KEY_EMAIL = 'email';
const KEY_AUTHENTICATION_ENDPOINT_URL = 'endpoint_url';
const KEY_IDENTITY_LINKING_URL = 'identity_link_url';
const API_RESOURCES = 'resource';
const API_RESOURCE_NAME = 'name';
/**#@-*/
/**#@-*/
protected $resourceProvider;
/**
* Initialize dependencies.
*
* @param \Magento\Framework\Acl\AclResource\ProviderInterface $resourceProvider
*/
public function __construct(
\Magento\Framework\Acl\AclResource\ProviderInterface $resourceProvider
) {
$this->resourceProvider = $resourceProvider;
}
/**
* @inheritdoc
*/
public function convert($source)
{
$result = [];
$allResources = $this->resourceProvider->getAclResources();
$hashAclResourcesTree = $this->hashResources($allResources[1]['children']);
/** @var \DOMNodeList $integrations */
$integrations = $source->getElementsByTagName('integration');
/** @var \DOMElement $integration */
foreach ($integrations as $integration) {
if ($integration->nodeType != XML_ELEMENT_NODE) {
continue;
}
$integrationName = $integration->attributes->getNamedItem('name')->nodeValue;
$result[$integrationName] = [];
$result[$integrationName][self::API_RESOURCES] = [];
/** @var \DOMElement $email */
$email = $integration->getElementsByTagName('email')->item(0)->nodeValue;
/** @var \DOMNodeList $resources */
$resources = $integration->getElementsByTagName('resource');
$result[$integrationName][self::KEY_EMAIL] = $email;
if ($integration->getElementsByTagName('endpoint_url')->length) {
/** @var \DOMElement $endpointUrl */
$endpointUrl = $integration->getElementsByTagName('endpoint_url')->item(0)->nodeValue;
$result[$integrationName][self::KEY_AUTHENTICATION_ENDPOINT_URL] = $endpointUrl;
}
if ($integration->getElementsByTagName('identity_link_url')->length) {
/** @var \DOMElement $identityLinkUrl */
$identityLinkUrl = $integration->getElementsByTagName('identity_link_url')->item(0)->nodeValue;
$result[$integrationName][self::KEY_IDENTITY_LINKING_URL] = $identityLinkUrl;
}
/** @var \DOMElement $resource */
foreach ($resources as $resource) {
if ($resource->nodeType != XML_ELEMENT_NODE) {
continue;
}
$resource = $resource->attributes->getNamedItem('name')->nodeValue;
$resourceNames = $this->addParentsToResource($hashAclResourcesTree, $resource);
foreach ($resourceNames as $name) {
$result[$integrationName][self::API_RESOURCES][] = $name;
}
}
// Add root resource if any child has been added
if (!empty($result[$integrationName][self::API_RESOURCES])) {
array_unshift($result[$integrationName][self::API_RESOURCES], $allResources[1]['id']);
}
// Remove any duplicates added parents
$result[$integrationName][self::API_RESOURCES] = array_values(
array_unique($result[$integrationName][self::API_RESOURCES])
);
}
return $result;
}
/**
* Make ACL resource array return a hash with parent-resource-name => [children-resources-names] representation
*
* @param array $resources
* @return array
*/
private function hashResources(array $resources)
{
$output = [];
foreach ($resources as $resource) {
if (isset($resource['children'])) {
$item = $this->hashResources($resource['children']);
} else {
$item = [];
}
$output[$resource['id']] = $item;
}
return $output;
}
/**
* Find parents names of a node in an ACL resource hash and add them to returned array
*
* @param array $resourcesHash
* @param string $nodeName
* @return array
*/
private function addParentsToResource(array $resourcesHash, $nodeName)
{
$output = [];
foreach ($resourcesHash as $resource => $children) {
if ($resource == $nodeName) {
$output = [$resource];
break;
}
if (!empty($children)) {
$names = $this->addParentsToResource($children, $nodeName);
if (!empty($names)) {
$output = array_merge([$resource], $names);
break;
} else {
continue;
}
}
}
return $output;
}
}