mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 22:17:26 +01:00
In between commit optimizing law permission checker
This commit is contained in:
parent
e221e835d1
commit
a968052309
@ -8,6 +8,8 @@ use App\Entity\Meta\RightInterface;
|
|||||||
use Doctrine\Common\Collections\Collection;
|
use Doctrine\Common\Collections\Collection;
|
||||||
use App\Entity\Meta\LawInterface;
|
use App\Entity\Meta\LawInterface;
|
||||||
use App\Domain\RightManagement\RightChecker;
|
use App\Domain\RightManagement\RightChecker;
|
||||||
|
use App\Entity\Source\SourceInterface;
|
||||||
|
use App\Domain\SourceManagement\SourceMemberInformation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @todo Implement checking by operation sources
|
* @todo Implement checking by operation sources
|
||||||
@ -45,18 +47,61 @@ final class LawPermissionCheckerService implements LawPermissionCheckerServiceIn
|
|||||||
* @param Collection|RightInterface[] $rights
|
* @param Collection|RightInterface[] $rights
|
||||||
* @param string $type
|
* @param string $type
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection|RightInterface[]
|
||||||
*/
|
*/
|
||||||
private function getRightsByType(Collection $rights, string $type): Collection
|
private function getRightsByType(Collection $rights, string $type): Collection
|
||||||
{
|
{
|
||||||
return $this->getFilteredRights($rights, $type, 'Type');
|
return $this->getFilteredRights($rights, $type, 'Type');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Collection|RightInterface[] $rights
|
||||||
|
* @param SourceInterface $reciever
|
||||||
|
*
|
||||||
|
* @return Collection|RightInterface[]
|
||||||
|
*/
|
||||||
|
private function getRightsByReciever(Collection $rights, SourceInterface $reciever): Collection
|
||||||
|
{
|
||||||
|
$result = new ArrayCollection();
|
||||||
|
foreach ($rights as $right) {
|
||||||
|
if ($right->getReciever() === $reciever || $this->memberExist($right, $reciever)) {
|
||||||
|
$result->add($right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo Implement!
|
||||||
|
*
|
||||||
|
* @param RightInterface $right
|
||||||
|
* @param SourceInterface $recieverSource
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function memberExist(RightInterface $right, SourceInterface $recieverSource): bool
|
||||||
|
{
|
||||||
|
$rightMemberInformation = new SourceMemberInformation($right->getReciever());
|
||||||
|
// $rightMemberSources = $rightMemberInformation->getAllMembers();
|
||||||
|
$rightMemberSources = new ArrayCollection();
|
||||||
|
foreach ($rightMemberSources as $memberSource) {
|
||||||
|
// var_dump($memberSource);
|
||||||
|
// echo "______________________________";
|
||||||
|
// var_dump($recieverSource);
|
||||||
|
if ($memberSource === $recieverSource) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Collection|RightInterface[] $rights
|
* @param Collection|RightInterface[] $rights
|
||||||
* @param string $layer
|
* @param string $layer
|
||||||
*
|
*
|
||||||
* @return Collection
|
* @return Collection|RightInterface[]
|
||||||
*/
|
*/
|
||||||
private function getRightsByLayer(Collection $rights, string $layer): Collection
|
private function getRightsByLayer(Collection $rights, string $layer): Collection
|
||||||
{
|
{
|
||||||
@ -98,7 +143,7 @@ final class LawPermissionCheckerService implements LawPermissionCheckerServiceIn
|
|||||||
$right = $rights[0];
|
$right = $rights[0];
|
||||||
$rightChecker = new RightChecker($right);
|
$rightChecker = new RightChecker($right);
|
||||||
|
|
||||||
return $rightChecker->isGranted($client->getLayer(), $client->getType(), $client->getSource());
|
return $rightChecker->isGranted($client->getLayer(), $client->getType(), $client->getReciever());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __construct(LawInterface $law)
|
public function __construct(LawInterface $law)
|
||||||
@ -111,6 +156,7 @@ final class LawPermissionCheckerService implements LawPermissionCheckerServiceIn
|
|||||||
$rights = clone $this->law->getRights();
|
$rights = clone $this->law->getRights();
|
||||||
$rights = $this->getRightsByType($rights, $client->getType());
|
$rights = $this->getRightsByType($rights, $client->getType());
|
||||||
$rights = $this->getRightsByLayer($rights, $client->getLayer());
|
$rights = $this->getRightsByLayer($rights, $client->getLayer());
|
||||||
|
$rights = $this->getRightsByReciever($rights, $client->getReciever());
|
||||||
$rights = $this->sortByPriority($rights);
|
$rights = $this->sortByPriority($rights);
|
||||||
|
|
||||||
return $this->isGranted($rights, $client);
|
return $this->isGranted($rights, $client);
|
||||||
|
@ -14,6 +14,7 @@ use App\Entity\Meta\Law;
|
|||||||
use App\Entity\Meta\LawInterface;
|
use App\Entity\Meta\LawInterface;
|
||||||
use App\Entity\Meta\RightInterface;
|
use App\Entity\Meta\RightInterface;
|
||||||
use Doctrine\Common\Collections\ArrayCollection;
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
use App\Domain\SourceManagement\SourceMemberManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
@ -21,25 +22,30 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||||||
class LawPermissionCheckerTest extends TestCase
|
class LawPermissionCheckerTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var LawPermissionCheckerServiceInterface
|
* @var LawPermissionCheckerServiceInterface The service which checks the law
|
||||||
*/
|
*/
|
||||||
protected $lawPermissionChecker;
|
private $lawPermissionChecker;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var LawInterface
|
* @var LawInterface The law which applies to the source
|
||||||
*/
|
*/
|
||||||
protected $law;
|
private $law;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var RightInterface
|
* @var RightInterface
|
||||||
*/
|
*/
|
||||||
protected $clientRight;
|
private $clientRight;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var SourceInterface
|
* @var SourceInterface The client which requests a law
|
||||||
*/
|
*/
|
||||||
private $clientSource;
|
private $clientSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var SourceInterface The source to which the law applies
|
||||||
|
*/
|
||||||
|
private $source;
|
||||||
|
|
||||||
private function getSourceMock(): SourceInterface
|
private function getSourceMock(): SourceInterface
|
||||||
{
|
{
|
||||||
return new class() extends AbstractSource {
|
return new class() extends AbstractSource {
|
||||||
@ -53,55 +59,136 @@ class LawPermissionCheckerTest extends TestCase
|
|||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->clientRight = new Right();
|
$this->setSourceDummy();
|
||||||
$this->law = new Law();
|
$this->setLawDummy();
|
||||||
|
$this->setLawPermissionChecker();
|
||||||
|
$this->setClientSourceDummy();
|
||||||
|
$this->setClientRightDummy();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setLawPermissionChecker(): void
|
||||||
|
{
|
||||||
$this->lawPermissionChecker = new LawPermissionCheckerService($this->law);
|
$this->lawPermissionChecker = new LawPermissionCheckerService($this->law);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setLawDummy(): void
|
||||||
|
{
|
||||||
|
$this->law = new Law();
|
||||||
|
$this->law->setSource($this->source);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setSourceDummy(): void
|
||||||
|
{
|
||||||
|
$this->source = $this->getSourceMock();
|
||||||
|
$this->source->setSlug('Requested Source');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setClientSourceDummy(): void
|
||||||
|
{
|
||||||
$this->clientSource = $this->getSourceMock();
|
$this->clientSource = $this->getSourceMock();
|
||||||
$this->clientRight->setSource($this->clientSource);
|
$this->clientSource->setSlug('Client Source');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function setClientRightDummy(): void
|
||||||
|
{
|
||||||
|
$this->clientRight = new Right();
|
||||||
$this->clientRight->setLayer(LayerType::SOURCE);
|
$this->clientRight->setLayer(LayerType::SOURCE);
|
||||||
$this->clientRight->setType(RightType::READ);
|
$this->clientRight->setType(RightType::READ);
|
||||||
|
$this->clientRight->setReciever($this->clientSource);
|
||||||
|
$this->clientRight->setSource($this->source);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getClonedClientRight(): RightInterface
|
||||||
|
{
|
||||||
|
return clone $this->clientRight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testInitialValues(): void
|
||||||
|
{
|
||||||
|
$this->assertFalse($this->checkClientPermission());
|
||||||
|
$this->assertTrue($this->clientRight->getGrant());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGeneralPermission(): void
|
public function testGeneralPermission(): void
|
||||||
{
|
{
|
||||||
$this->assertFalse($this->checkClientPermission());
|
$this->law->getRights()->add($this->getClonedClientRight());
|
||||||
$lawRight = clone $this->clientRight;
|
|
||||||
$lawRight->setReciever($this->clientSource);
|
|
||||||
$this->law->getRights()->add($lawRight);
|
|
||||||
$this->assertTrue($this->checkClientPermission());
|
$this->assertTrue($this->checkClientPermission());
|
||||||
$this->clientRight->setType(RightType::WRITE);
|
$this->clientRight->setType(RightType::WRITE);
|
||||||
$this->assertFalse($this->checkClientPermission());
|
$this->assertFalse($this->checkClientPermission());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testMemberPermission(): void
|
// /**
|
||||||
|
// * @todo Implement!
|
||||||
|
// */
|
||||||
|
// public function testChildMemberPermission(): void
|
||||||
|
// {
|
||||||
|
// $parentSource = $this->getSourceMock();
|
||||||
|
// $parentSource->setSlug('Parent Source');
|
||||||
|
// $sourceMemberManager = new SourceMemberManager($parentSource);
|
||||||
|
// $sourceMemberManager->addMember($this->clientSource);
|
||||||
|
// $parentSourceRight = $this->getClonedClientRight();
|
||||||
|
// $parentSourceRight->setReciever($parentSource);
|
||||||
|
// $this->law->getRights()->add($parentSourceRight);
|
||||||
|
// $this->assertTrue($this->checkClientPermission());
|
||||||
|
// $this->law->setRights(new ArrayCollection());
|
||||||
|
// $this->assertFalse($this->checkClientPermission());
|
||||||
|
// }
|
||||||
|
|
||||||
|
public function testGetRightsByType(): void
|
||||||
{
|
{
|
||||||
$parentSource = $this->getSourceMock();
|
$right = $this->getClonedClientRight();
|
||||||
$this->clientRight->getSource()->getMemberRelation()->getMemberships()->add($parentSource);
|
$right->setType(RightType::WRITE);
|
||||||
$parentSource->getMemberRelation()->getMembers()->add($this->clientRight->getSource());
|
$this->law->getRights()->add($right);
|
||||||
$lawRight = clone $this->clientRight;
|
$this->assertFalse($this->checkClientPermission());
|
||||||
$lawRight->setReciever($parentSource);
|
$right->setType(RightType::READ);
|
||||||
$this->law->getRights()->add($lawRight);
|
$this->assertTrue($this->checkClientPermission());
|
||||||
$permission = $this->lawPermissionChecker->hasPermission($this->clientRight);
|
|
||||||
$this->assertTrue($permission);
|
|
||||||
$this->law->setRights(new ArrayCollection());
|
|
||||||
$permission = $this->lawPermissionChecker->hasPermission($this->clientRight);
|
|
||||||
$this->assertFalse($permission);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testSort(): void
|
public function testGetRightsByLayer(): void
|
||||||
{
|
{
|
||||||
$right1 = clone $this->clientRight;
|
$right = $this->getClonedClientRight();
|
||||||
|
$right->setLayer(LayerType::LAW);
|
||||||
|
$this->law->getRights()->add($right);
|
||||||
|
$this->assertFalse($this->checkClientPermission());
|
||||||
|
$right->setLayer(LayerType::SOURCE);
|
||||||
|
$this->assertTrue($this->checkClientPermission());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSortByPriority(): void
|
||||||
|
{
|
||||||
|
$right1 = $this->getClonedClientRight();
|
||||||
$right1->setPriority(123);
|
$right1->setPriority(123);
|
||||||
$right1->setGrant(false);
|
$right1->setGrant(false);
|
||||||
$right1->setReciever($this->clientSource);
|
$right1->setReciever($this->clientSource);
|
||||||
$right2 = clone $this->clientRight;
|
$right2 = $this->getClonedClientRight();
|
||||||
$right2->setPriority(456);
|
$right2->setPriority(456);
|
||||||
$right2->setGrant(true);
|
$right2->setGrant(true);
|
||||||
$right2->setReciever($this->clientSource);
|
$right2->setReciever($this->clientSource);
|
||||||
$this->law->setRights(new ArrayCollection([$right1, $right2]));
|
$this->law->setRights(new ArrayCollection([
|
||||||
|
$right1,
|
||||||
|
$right2,
|
||||||
|
]));
|
||||||
$this->assertFalse($this->checkClientPermission());
|
$this->assertFalse($this->checkClientPermission());
|
||||||
$right2->setPriority(789);
|
$right2->setPriority(789);
|
||||||
$right1->setPriority(101112);
|
$right1->setPriority(101112);
|
||||||
$this->assertTrue($this->checkClientPermission());
|
$this->assertTrue($this->checkClientPermission());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testMemberFilter(): void
|
||||||
|
{
|
||||||
|
$right1 = $this->getClonedClientRight();
|
||||||
|
$right1->setPriority(123);
|
||||||
|
$right1->setGrant(false);
|
||||||
|
$right1->setReciever($this->getSourceMock());
|
||||||
|
$right1->getReciever()->setSlug('Rigth1 Reciever');
|
||||||
|
$right2 = $this->getClonedClientRight();
|
||||||
|
$right2->setPriority(456);
|
||||||
|
$right2->setGrant(true);
|
||||||
|
$right2->setReciever($this->clientSource);
|
||||||
|
$this->law->setRights(new ArrayCollection([
|
||||||
|
$right1,
|
||||||
|
$right2,
|
||||||
|
]));
|
||||||
|
$this->assertTrue($this->checkClientPermission());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user