5 use Drupal\Core\Database\Connection;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
28 private $schemasToModify = [
61 public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, RouteMatchInterface $routeMatch) {
62 parent::__construct($configuration, $plugin_id, $plugin_definition);
63 $this->configuration = $configuration;
64 $this->pluginId = $plugin_id;
65 $this->pluginDefinition = $plugin_definition;
66 $this->database = $database;
67 $this->routeMatch = $routeMatch;
73 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
78 $container->get(
'database'),
79 $container->get(
'current_route_match')
95 return in_array($schema, $this->schemasToModify)
96 && !$this->alternateEndpoint()
97 && $this->accessLevel($schema, $data) ===
'non-public';
106 private function alternateEndpoint() {
107 $routeName = $this->routeMatch->getRouteName();
108 return strpos($routeName,
'dkan_alt_api.') === 0;
122 private function accessLevel(
string $schema, $data) : string {
124 if (
'distribution' === $schema) {
125 return $this->parentDatasetAccessLevel($data);
127 return $this->datasetAccessLevel($data);
139 private function datasetAccessLevel($data) {
140 if (is_string($data)) {
141 $data = json_decode($data);
143 return $data->accessLevel;
155 private function parentDatasetAccessLevel($dist) {
156 $identifier = $this->getIdentifier($dist);
157 $parentDataset = $this->getParentDataset($identifier);
158 return $this->datasetAccessLevel($parentDataset);
170 private function getIdentifier($dist) : string {
171 if (is_string($dist)) {
172 if ($decoded = json_decode($dist)) {
179 return $dist->identifier;
191 private function getParentDataset($identifier) {
192 $datasets = $this->database->select(
'node__field_json_metadata',
'm')
193 ->condition(
'm.field_json_metadata_value',
'%accessLevel%',
'LIKE')
194 ->condition(
'm.field_json_metadata_value',
"%{$identifier}%",
'LIKE')
195 ->fields(
'm', [
'field_json_metadata_value'])
199 return reset($datasets);
213 public function modify(
string $schema, $data) {
214 if (
'distribution' === $schema) {
215 return $this->protectDistribution($data);
217 return $this->protectDataset($data);
229 private function protectDataset($dataset) {
230 if (is_string($dataset)) {
231 $datasetObj = json_decode($dataset);
232 $datasetObj = $this->protectDatasetObject($datasetObj);
233 return json_encode($datasetObj);
235 return $this->protectDatasetObject($dataset);
247 private function protectDatasetObject($dataset) {
248 if (isset($dataset->distribution) && is_array($dataset->distribution)) {
249 foreach ($dataset->distribution as $key => &$dist) {
250 $dataset->distribution[$key] = $this->protectDistributionObject($dist);
265 private function protectDistribution($dist) {
266 if (is_string($dist)) {
267 $distObj = json_decode($dist);
268 $distObj = $this->protectDistributionObject($distObj);
269 return json_encode($distObj);
271 return $this->protectDistributionObject($dist);
283 private function protectDistributionObject($dist) {
285 return (
object) [
'title' => $this->
message()];