From 04b156b8fcf9f1f72e69b5db77a6acd8d60afb6d Mon Sep 17 00:00:00 2001 From: Kevin Frantz Date: Sat, 12 Jan 2019 15:40:54 +0100 Subject: [PATCH] Implemented RightLayerCombination --- .../src/DBAL/Types/Meta/Right/LayerType.php | 2 + .../RightLayerCombinationService.php | 85 +++++++++++++++++++ .../RightLayerCombinationServiceInterface.php | 34 ++++++++ .../RightLayerCombinationServiceTest.php | 56 ++++++++++++ 4 files changed, 177 insertions(+) create mode 100644 application/symfony/src/Domain/RightManagement/RightLayerCombinationService.php create mode 100644 application/symfony/src/Domain/RightManagement/RightLayerCombinationServiceInterface.php create mode 100644 application/symfony/tests/Unit/Domain/RightManagement/RightLayerCombinationServiceTest.php diff --git a/application/symfony/src/DBAL/Types/Meta/Right/LayerType.php b/application/symfony/src/DBAL/Types/Meta/Right/LayerType.php index c46b424..4d45f4c 100644 --- a/application/symfony/src/DBAL/Types/Meta/Right/LayerType.php +++ b/application/symfony/src/DBAL/Types/Meta/Right/LayerType.php @@ -6,6 +6,8 @@ use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType; /** * @author kevinfrantz + * + * @todo Implement more layers and refactor! */ final class LayerType extends AbstractEnumType { diff --git a/application/symfony/src/Domain/RightManagement/RightLayerCombinationService.php b/application/symfony/src/Domain/RightManagement/RightLayerCombinationService.php new file mode 100644 index 0000000..9bb4e99 --- /dev/null +++ b/application/symfony/src/Domain/RightManagement/RightLayerCombinationService.php @@ -0,0 +1,85 @@ + [ + CRUDType::CREATE, + CRUDType::DELETE, + ], + LayerType::LAW => [ + CRUDType::CREATE, + CRUDType::DELETE, + ], + ]; + + /** + * @var array[] Array of string arrays + */ + private $possibleCombinations = []; + + public function __construct() + { + $this->setCombination(); + } + + /** + * @param string $layer + * @param string $crud + * + * @return bool + */ + private function checkIfExcluded(string $layer, string $crud): bool + { + return array_key_exists($layer, self::EXLUDED_RIGHTS_BY_LAYER) && in_array($crud, self::EXLUDED_RIGHTS_BY_LAYER[$layer]); + } + + private function setCombination(): void + { + foreach (LayerType::getChoices() as $layer) { + foreach (CRUDType::getChoices() as $crud) { + if (!array_key_exists($layer, $this->possibleCombinations)) { + $this->possibleCombinations[$layer] = []; + } + if (!$this->checkIfExcluded($layer, $crud)) { + $this->possibleCombinations[$layer][] = $crud; + } + } + } + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\RightManagement\RightLayerCombinationServiceInterface::getPossibleCruds() + */ + public function getPossibleCruds(string $layer): array + { + return $this->possibleCombinations[$layer]; + } + + /** + * {@inheritdoc} + * + * @see \App\Domain\RightManagement\RightLayerCombinationServiceInterface::getPossibleLayers() + */ + public function getPossibleLayers(string $crudType): array + { + $possibleLayers = []; + foreach ($this->possibleCombinations as $layer => $possibleCombination) { + if (in_array($crudType, $possibleCombination)) { + $possibleLayers[] = $layer; + } + } + + return $possibleLayers; + } +} diff --git a/application/symfony/src/Domain/RightManagement/RightLayerCombinationServiceInterface.php b/application/symfony/src/Domain/RightManagement/RightLayerCombinationServiceInterface.php new file mode 100644 index 0000000..ad78a9a --- /dev/null +++ b/application/symfony/src/Domain/RightManagement/RightLayerCombinationServiceInterface.php @@ -0,0 +1,34 @@ +rightLayerCombinationService = new RightLayerCombinationService(); + } + + public function testBySource(): void + { + foreach (CRUDType::getChoices() as $crudType) { + $layers = $this->rightLayerCombinationService->getPossibleLayers($crudType); + $this->assertContains(LayerType::SOURCE, $layers); + $sourceCruds = $this->rightLayerCombinationService->getPossibleCruds(LayerType::SOURCE); + $this->assertContains($crudType, $sourceCruds); + } + } + + public function testByLaw(): void + { + foreach ([CRUDType::DELETE, CRUDType::CREATE] as $crudType) { + $layers = $this->rightLayerCombinationService->getPossibleLayers($crudType); + $this->assertNotContains(LayerType::LAW, $layers); + $sourceCruds = $this->rightLayerCombinationService->getPossibleCruds(LayerType::LAW); + $this->assertNotContains($crudType, $sourceCruds); + } + foreach ([CRUDType::READ, CRUDType::UPDATE] as $crudType) { + $layers = $this->rightLayerCombinationService->getPossibleLayers($crudType); + $this->assertContains(LayerType::LAW, $layers); + $sourceCruds = $this->rightLayerCombinationService->getPossibleCruds(LayerType::LAW); + $this->assertContains($crudType, $sourceCruds); + } + } +}