|
9 | 9 |
|
10 | 10 | namespace Piwik\Plugins\ApiReference; |
11 | 11 |
|
| 12 | +use Piwik\Access; |
12 | 13 | use Piwik\Piwik; |
13 | 14 | use Piwik\Plugins\ApiReference\Generation\PluginListProvider; |
14 | 15 | use Piwik\Plugin\Manager; |
15 | | -use Piwik\Plugins\ApiReference\Specs\SpecGenerator; |
16 | 16 | use Piwik\Plugins\ApiReference\Specs\PathResolver; |
17 | 17 |
|
18 | 18 | /** |
@@ -87,9 +87,66 @@ public function getOpenApiSpec(string $pluginName, string $format = 'json'): arr |
87 | 87 | throw new \Exception('OpenAPI spec file contains invalid JSON.'); |
88 | 88 | } |
89 | 89 |
|
| 90 | + $canViewExampleSite = in_array(1, Access::getInstance()->getSitesIdWithAtLeastViewAccess(), true); |
| 91 | + if (!$canViewExampleSite) { |
| 92 | + $decodedSpec = $this->removeSuccessfulResponseExamples($decodedSpec); |
| 93 | + } |
| 94 | + |
90 | 95 | return $decodedSpec; |
91 | 96 | } |
92 | 97 |
|
| 98 | + /** |
| 99 | + * Remove embedded example payloads from successful 200 responses. |
| 100 | + * |
| 101 | + * @param array<string, mixed> $spec |
| 102 | + * @return array<string, mixed> |
| 103 | + */ |
| 104 | + protected function removeSuccessfulResponseExamples(array $spec): array |
| 105 | + { |
| 106 | + if (empty($spec['paths']) || !is_array($spec['paths'])) { |
| 107 | + return $spec; |
| 108 | + } |
| 109 | + |
| 110 | + foreach ($spec['paths'] as &$pathItem) { |
| 111 | + if (!is_array($pathItem)) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + foreach ($pathItem as &$operation) { |
| 116 | + if ( |
| 117 | + !is_array($operation) |
| 118 | + || empty($operation['responses']) |
| 119 | + || !is_array($operation['responses']) |
| 120 | + ) { |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + foreach (['200', 200] as $responseCode) { |
| 125 | + if ( |
| 126 | + empty($operation['responses'][$responseCode]) |
| 127 | + || !is_array($operation['responses'][$responseCode]) |
| 128 | + || empty($operation['responses'][$responseCode]['content']) |
| 129 | + || !is_array($operation['responses'][$responseCode]['content']) |
| 130 | + ) { |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + foreach ($operation['responses'][$responseCode]['content'] as &$content) { |
| 135 | + if (!is_array($content)) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + unset($content['example'], $content['examples']); |
| 140 | + } |
| 141 | + unset($content); |
| 142 | + } |
| 143 | + } |
| 144 | + unset($operation); |
| 145 | + } |
| 146 | + unset($pathItem); |
| 147 | + |
| 148 | + return $spec; |
| 149 | + } |
93 | 150 | protected function getSpecFilePath(string $pluginName): string |
94 | 151 | { |
95 | 152 | return $this->getSpecPathResolver()->getSpecFilePath($pluginName); |
@@ -131,30 +188,4 @@ protected function getPluginListProvider(): PluginListProvider |
131 | 188 | return new PluginListProvider(); |
132 | 189 | } |
133 | 190 |
|
134 | | - /** |
135 | | - * Generates an OpenAPI specification for one or more plugins and returns it immediately. |
136 | | - * |
137 | | - * @param string $plugin The plugin name to generate, or a comma-separated list of plugin names. |
138 | | - * @param string $format The response format to generate. Supported values are `json` and `yaml`. |
139 | | - * @return array<string, mixed>|string The generated OpenAPI specification as decoded JSON data for |
140 | | - * `json`, or as a YAML string for `yaml`. |
141 | | - */ |
142 | | - public function getGeneratedOpenApiSpec(string $plugin, string $format) |
143 | | - { |
144 | | - Piwik::checkUserHasSomeViewAccess(); |
145 | | - |
146 | | - // Return an error if format is something other than JSON or YAML |
147 | | - $allowedFormats = ['json', 'yaml']; |
148 | | - if (!in_array(strtolower($format), $allowedFormats)) { |
149 | | - throw new \Exception( |
150 | | - Piwik::translate( |
151 | | - 'General_ExceptionInvalidReportRendererFormat', |
152 | | - [$format, implode(', ', $allowedFormats)] |
153 | | - ) |
154 | | - ); |
155 | | - } |
156 | | - |
157 | | - $docString = (new SpecGenerator())->generatePluginDoc($plugin, $format); |
158 | | - return strtolower($format) === 'json' ? json_decode($docString, true) : $docString; |
159 | | - } |
160 | 191 | } |
0 commit comments