|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Silverback API Components Bundle Project |
| 5 | + * |
| 6 | + * (c) Daniel West <daniel@silverback.is> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Silverback\ApiComponentsBundle\ApiPlatform\Metadata\Resource; |
| 13 | + |
| 14 | +use ApiPlatform\Metadata\HttpOperation; |
| 15 | +use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; |
| 16 | +use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
| 17 | +use Silverback\ApiComponentsBundle\ApiResource\ComponentGroupOrder; |
| 18 | +use Silverback\ApiComponentsBundle\AttributeReader\PublishableAttributeReader; |
| 19 | +use Silverback\ApiComponentsBundle\Entity\Core\AbstractComponent; |
| 20 | +use Silverback\ApiComponentsBundle\Entity\Core\AbstractPageData; |
| 21 | +use Silverback\ApiComponentsBundle\Entity\Core\ComponentGroup; |
| 22 | +use Silverback\ApiComponentsBundle\Entity\Core\ComponentPosition; |
| 23 | +use Silverback\ApiComponentsBundle\Entity\Core\Layout; |
| 24 | +use Silverback\ApiComponentsBundle\Entity\Core\Page; |
| 25 | +use Silverback\ApiComponentsBundle\Entity\Core\Route; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Daniel West <daniel@silverback.is> |
| 29 | + */ |
| 30 | +final readonly class ContentWriteSecurityResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface |
| 31 | +{ |
| 32 | + public const string OPT_OUT_EXTRA_PROPERTY = 'publishable_permission'; |
| 33 | + |
| 34 | + private const array CONTENT_CLASSES = [ |
| 35 | + Page::class, |
| 36 | + Layout::class, |
| 37 | + Route::class, |
| 38 | + ComponentGroup::class, |
| 39 | + ComponentGroupOrder::class, |
| 40 | + ComponentPosition::class, |
| 41 | + AbstractComponent::class, |
| 42 | + AbstractPageData::class, |
| 43 | + ]; |
| 44 | + |
| 45 | + private const array WRITE_METHODS = [ |
| 46 | + HttpOperation::METHOD_POST, |
| 47 | + HttpOperation::METHOD_PUT, |
| 48 | + HttpOperation::METHOD_PATCH, |
| 49 | + HttpOperation::METHOD_DELETE, |
| 50 | + ]; |
| 51 | + |
| 52 | + public function __construct( |
| 53 | + private ResourceMetadataCollectionFactoryInterface $decorated, |
| 54 | + private PublishableAttributeReader $publishableAttributeReader, |
| 55 | + private string $permission, |
| 56 | + ) { |
| 57 | + } |
| 58 | + |
| 59 | + public function create(string $resourceClass): ResourceMetadataCollection |
| 60 | + { |
| 61 | + $resourceMetadataCollection = $this->decorated->create($resourceClass); |
| 62 | + |
| 63 | + if (!$this->isContent($resourceClass)) { |
| 64 | + return $resourceMetadataCollection; |
| 65 | + } |
| 66 | + |
| 67 | + $permission = $this->permission($resourceClass); |
| 68 | + foreach ($resourceMetadataCollection as $i => $resource) { |
| 69 | + $operations = $resource->getOperations(); |
| 70 | + if (null === $operations) { |
| 71 | + continue; |
| 72 | + } |
| 73 | + foreach ($operations as $key => $operation) { |
| 74 | + if (!\in_array($operation->getMethod(), self::WRITE_METHODS, true) || false === ($operation->getExtraProperties()[self::OPT_OUT_EXTRA_PROPERTY] ?? true)) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + $operations->add($key, $operation->withSecurity(self::security($permission, $operation->getSecurity()))); |
| 78 | + } |
| 79 | + $resourceMetadataCollection[$i] = $resource->withOperations($operations); |
| 80 | + } |
| 81 | + |
| 82 | + return $resourceMetadataCollection; |
| 83 | + } |
| 84 | + |
| 85 | + private function isContent(string $resourceClass): bool |
| 86 | + { |
| 87 | + foreach (self::CONTENT_CLASSES as $contentClass) { |
| 88 | + if (is_a($resourceClass, $contentClass, true)) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + private function permission(string $resourceClass): string |
| 97 | + { |
| 98 | + if (!$this->publishableAttributeReader->isConfigured($resourceClass)) { |
| 99 | + return $this->permission; |
| 100 | + } |
| 101 | + |
| 102 | + return $this->publishableAttributeReader->getConfiguration($resourceClass)->isGranted ?? $this->permission; |
| 103 | + } |
| 104 | + |
| 105 | + private static function security(string $permission, ?string $security): string |
| 106 | + { |
| 107 | + if (null === $security || '' === $security) { |
| 108 | + return $permission; |
| 109 | + } |
| 110 | + |
| 111 | + return \sprintf('(%s) and (%s)', $permission, $security); |
| 112 | + } |
| 113 | +} |
0 commit comments