Optimized RightCheck for thirdt and second dimension source

This commit is contained in:
Kevin Frantz 2018-12-08 13:22:50 +01:00
parent b85e971861
commit 31b6b4afc7
2 changed files with 89 additions and 44 deletions

View File

@ -8,6 +8,8 @@ use App\Entity\Source\SourceInterface;
use App\Domain\SourceManagement\SourceMemberInformation; use App\Domain\SourceManagement\SourceMemberInformation;
/** /**
* @todo Implement the check of conditions!
*
* @author kevinfrantz * @author kevinfrantz
*/ */
final class RightChecker implements RightCheckerInterface final class RightChecker implements RightCheckerInterface
@ -18,46 +20,46 @@ final class RightChecker implements RightCheckerInterface
private $right; private $right;
/** /**
* @var Collection|SourceInterface[] * @todo Implement a performant solution
*
* @return Collection
*/ */
private $allSourcesToWhichRightApplies; private function getAllSourcesToWhichRightApplies(): Collection
/**
* Calling this function in the constructor can lead to side effects when the source changes!
* @todo Implement a clean solution!
*/
private function setAllSourcesToWhichRightApplies(): void
{ {
$rightSourceMemberInformation = new SourceMemberInformation($this->right->getSource()); $rightSourceMemberInformation = new SourceMemberInformation($this->right->getSource());
$this->allSourcesToWhichRightApplies = clone ($rightSourceMemberInformation->getAllMembers()); $allSourcesToWhichRightApplies = clone $rightSourceMemberInformation->getAllMembers();
$this->allSourcesToWhichRightApplies->add($this->right->getSource()); $allSourcesToWhichRightApplies->add($this->right->getSource());
return $allSourcesToWhichRightApplies;
} }
private function hasSource(SourceInterface $source): bool private function hasSource(SourceInterface $source): bool
{ {
return $this->allSourcesToWhichRightApplies->contains($source); return $this->getAllSourcesToWhichRightApplies()->contains($source);
} }
private function isLayerEqual(string $layer):bool{ private function isLayerEqual(string $layer): bool
return ($this->right->getLayer() === $layer); {
return $this->right->getLayer() === $layer;
} }
private function isTypeEqual(string $type):bool{ private function isTypeEqual(string $type): bool
return ($this->right->getType() === $type); {
return $this->right->getType() === $type;
} }
private function checkPermission():bool{ private function checkPermission(): bool
{
return $this->right->getGrant(); return $this->right->getGrant();
} }
public function __construct(RightInterface $right) public function __construct(RightInterface $right)
{ {
$this->right = $right; $this->right = $right;
$this->setAllSourcesToWhichRightApplies();
} }
public function isGranted(string $layer, string $type, SourceInterface $source): bool 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();
} }
} }

View File

@ -1,4 +1,5 @@
<?php <?php
namespace Tests\Unit\Domain\RightManagement; namespace Tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
@ -13,49 +14,91 @@ use App\DBAL\Types\RightType;
class RightCheckerTest extends TestCase class RightCheckerTest extends TestCase
{ {
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $layer;
/** /**
* @var SourceInterface * @var SourceInterface
*/ */
private $source; private $source;
/** /**
* @var RightInterface * @var RightInterface
*/ */
private $right; private $right;
/** /**
* @var RightCheckerInterface * @var RightCheckerInterface
*/ */
private $rightManager; private $rightManager;
private function getSourceMock():SourceInterface{ private function getSourceMock(): SourceInterface
return new class extends AbstractSource{}; {
return new class() extends AbstractSource {
};
} }
public function setUp():void{ public function setUp(): void
$this->right = new Right(); {
$this->layer = LayerType::RELATION;
$this->type = RightType::READ;
$this->source = $this->getSourceMock(); $this->source = $this->getSourceMock();
$this->right = new Right();
$this->right->setSource($this->source); $this->right->setSource($this->source);
$this->right->setType($this->type);
$this->right->setLayer($this->layer);
$this->rightManager = new RightChecker($this->right); $this->rightManager = new RightChecker($this->right);
} }
public function testFirstDimension():void{ public function testFirstDimension(): void
$layer = LayerType::RELATION; {
$type = RightType::READ; $granted = $this->rightManager->isGranted($this->layer, $this->type, $this->source);
$this->right->setType($type);
$this->right->setLayer($layer);
$granted = $this->rightManager->isGranted($layer, $type, $this->source);
$this->assertTrue($granted); $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); $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->assertFalse($notGranted2);
$this->right->setGrant(false); $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); $this->assertFalse($notGranted3);
} }
// public function testSecondDimension():void{ 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);
}
}