Implemented draft for right checker

This commit is contained in:
Kevin Frantz 2018-12-08 12:51:29 +01:00
parent e97003b91d
commit b85e971861
3 changed files with 134 additions and 0 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace App\Domain\RightManagement;
use App\Entity\Meta\RightInterface;
use Doctrine\Common\Collections\Collection;
use App\Entity\Source\SourceInterface;
use App\Domain\SourceManagement\SourceMemberInformation;
/**
* @author kevinfrantz
*/
final class RightChecker implements RightCheckerInterface
{
/**
* @var RightInterface
*/
private $right;
/**
* @var Collection|SourceInterface[]
*/
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
{
$rightSourceMemberInformation = new SourceMemberInformation($this->right->getSource());
$this->allSourcesToWhichRightApplies = clone ($rightSourceMemberInformation->getAllMembers());
$this->allSourcesToWhichRightApplies->add($this->right->getSource());
}
private function hasSource(SourceInterface $source): bool
{
return $this->allSourcesToWhichRightApplies->contains($source);
}
private function isLayerEqual(string $layer):bool{
return ($this->right->getLayer() === $layer);
}
private function isTypeEqual(string $type):bool{
return ($this->right->getType() === $type);
}
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());
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Domain\RightManagement;
use App\Entity\Source\SourceInterface;
interface RightCheckerInterface
{
public function isGranted(string $layer, string $type, SourceInterface $source): bool;
}

View File

@ -0,0 +1,61 @@
<?php
namespace Tests\Unit\Domain\RightManagement;
use PHPUnit\Framework\TestCase;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use App\Entity\Source\SourceInterface;
use App\Entity\Source\AbstractSource;
use App\DBAL\Types\LayerType;
use App\Domain\RightManagement\RightCheckerInterface;
use App\Domain\RightManagement\RightChecker;
use App\DBAL\Types\RightType;
class RightCheckerTest extends TestCase
{
/**
* @var SourceInterface
*/
private $source;
/**
* @var RightInterface
*/
private $right;
/**
* @var RightCheckerInterface
*/
private $rightManager;
private function getSourceMock():SourceInterface{
return new class extends AbstractSource{};
}
public function setUp():void{
$this->right = new Right();
$this->source = $this->getSourceMock();
$this->right->setSource($this->source);
$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);
$this->assertTrue($granted);
$notGranted = $this->rightManager->isGranted(LayerType::SOURCE, $type, $this->source);
$this->assertFalse($notGranted);
$notGranted2 = $this->rightManager->isGranted($layer, RightType::WRITE, $this->source);
$this->assertFalse($notGranted2);
$this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($layer, $type, $this->source);
$this->assertFalse($notGranted3);
}
// public function testSecondDimension():void{
// }
}