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;
/**
* @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();
}
}

View File

@ -1,4 +1,5 @@
<?php
namespace Tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase;
@ -13,49 +14,91 @@ use App\DBAL\Types\RightType;
class RightCheckerTest extends TestCase
{
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $layer;
/**
* @var SourceInterface
*/
private $source;
/**
* @var RightInterface
*/
private $right;
/**
* @var RightCheckerInterface
*/
private $rightManager;
private function getSourceMock():SourceInterface{
return new class extends AbstractSource{};
private function getSourceMock(): SourceInterface
{
return new class() extends AbstractSource {
};
}
public function setUp():void{
$this->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{
// }
}
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);
}
}