diff --git a/application/src/Domain/RightManagement/RightChecker.php b/application/src/Domain/RightManagement/RightChecker.php index 651fbd4..4c7a3c6 100644 --- a/application/src/Domain/RightManagement/RightChecker.php +++ b/application/src/Domain/RightManagement/RightChecker.php @@ -8,6 +8,8 @@ use App\Entity\Source\SourceInterface; use App\Domain\SourceManagement\SourceMemberInformation; /** + * @todo Implement the check of conditions! + * * @author kevinfrantz */ final class RightChecker implements RightCheckerInterface @@ -18,46 +20,46 @@ final class RightChecker implements RightCheckerInterface private $right; /** - * @var Collection|SourceInterface[] + * @todo Implement a performant solution + * + * @return Collection */ - private $allSourcesToWhichRightApplies; - - /** - * Calling this function in the constructor can lead to side effects when the source changes! - * @todo Implement a clean solution! - */ - private function setAllSourcesToWhichRightApplies(): void + private function getAllSourcesToWhichRightApplies(): Collection { $rightSourceMemberInformation = new SourceMemberInformation($this->right->getSource()); - $this->allSourcesToWhichRightApplies = clone ($rightSourceMemberInformation->getAllMembers()); - $this->allSourcesToWhichRightApplies->add($this->right->getSource()); + $allSourcesToWhichRightApplies = clone $rightSourceMemberInformation->getAllMembers(); + $allSourcesToWhichRightApplies->add($this->right->getSource()); + + return $allSourcesToWhichRightApplies; } private function hasSource(SourceInterface $source): bool { - return $this->allSourcesToWhichRightApplies->contains($source); + return $this->getAllSourcesToWhichRightApplies()->contains($source); } - - private function isLayerEqual(string $layer):bool{ - return ($this->right->getLayer() === $layer); + + private function isLayerEqual(string $layer): bool + { + return $this->right->getLayer() === $layer; } - - private function isTypeEqual(string $type):bool{ - return ($this->right->getType() === $type); + + private function isTypeEqual(string $type): bool + { + return $this->right->getType() === $type; } - - private function checkPermission():bool{ + + private function checkPermission(): bool + { return $this->right->getGrant(); } public function __construct(RightInterface $right) { $this->right = $right; - $this->setAllSourcesToWhichRightApplies(); } public function isGranted(string $layer, string $type, SourceInterface $source): bool { - return ($this->isLayerEqual($layer) && $this->isTypeEqual($type) && $this->hasSource($source) && $this->checkPermission()); + return $this->isLayerEqual($layer) && $this->isTypeEqual($type) && $this->hasSource($source) && $this->checkPermission(); } } diff --git a/application/tests/Unit/Domain/RightManagement/RightCheckerTest.php b/application/tests/Unit/Domain/RightManagement/RightCheckerTest.php index 9ca280d..a4554f7 100644 --- a/application/tests/Unit/Domain/RightManagement/RightCheckerTest.php +++ b/application/tests/Unit/Domain/RightManagement/RightCheckerTest.php @@ -1,4 +1,5 @@ right = new Right(); + + public function setUp(): void + { + $this->layer = LayerType::RELATION; + $this->type = RightType::READ; $this->source = $this->getSourceMock(); + $this->right = new Right(); $this->right->setSource($this->source); + $this->right->setType($this->type); + $this->right->setLayer($this->layer); $this->rightManager = new RightChecker($this->right); } - - public function testFirstDimension():void{ - $layer = LayerType::RELATION; - $type = RightType::READ; - $this->right->setType($type); - $this->right->setLayer($layer); - $granted = $this->rightManager->isGranted($layer, $type, $this->source); + + public function testFirstDimension(): void + { + $granted = $this->rightManager->isGranted($this->layer, $this->type, $this->source); $this->assertTrue($granted); - $notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $type, $this->source); + $notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $this->source); $this->assertFalse($notGranted); - $notGranted2 = $this->rightManager->isGranted($layer, RightType::WRITE, $this->source); + $notGranted2 = $this->rightManager->isGranted($this->layer, RightType::WRITE, $this->source); $this->assertFalse($notGranted2); $this->right->setGrant(false); - $notGranted3 = $this->rightManager->isGranted($layer, $type, $this->source); + $notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $this->source); $this->assertFalse($notGranted3); } - -// public function testSecondDimension():void{ - -// } -} \ No newline at end of file + + public function testSecondDimension(): void + { + $secondSource = $this->getSourceMock(); + $this->source->getMemberRelation()->getMembers()->add($secondSource); + $granted = $this->rightManager->isGranted($this->layer, $this->type, $secondSource); + $this->assertTrue($granted); + $notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $secondSource); + $this->assertFalse($notGranted); + $notGranted2 = $this->rightManager->isGranted($this->layer, RightType::WRITE, $secondSource); + $this->assertFalse($notGranted2); + $this->right->setGrant(false); + $notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $secondSource); + $this->assertFalse($notGranted3); + } + + public function testThirdDimension(): void + { + $thirdSource = $this->getSourceMock(); + $secondSource = $this->getSourceMock(); + $secondSource->getMemberRelation()->getMembers()->add($thirdSource); + $this->source->getMemberRelation()->getMembers()->add($secondSource); + $granted = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource); + $this->assertTrue($granted); + $notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $this->type, $thirdSource); + $this->assertFalse($notGranted); + $notGranted2 = $this->rightManager->isGranted($this->layer, RightType::WRITE, $thirdSource); + $this->assertFalse($notGranted2); + $this->right->setGrant(false); + $notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource); + $this->assertFalse($notGranted3); + } +}