diff --git a/application/src/Domain/LawManagement/LawPermissionCheckerService.php b/application/src/Domain/LawManagement/LawPermissionCheckerService.php index 517edf0..eddc576 100644 --- a/application/src/Domain/LawManagement/LawPermissionCheckerService.php +++ b/application/src/Domain/LawManagement/LawPermissionCheckerService.php @@ -8,6 +8,8 @@ use App\Entity\Meta\RightInterface; use Doctrine\Common\Collections\Collection; use App\Entity\Meta\LawInterface; use App\Domain\RightManagement\RightChecker; +use App\Entity\Source\SourceInterface; +use App\Domain\SourceManagement\SourceMemberInformation; /** * @todo Implement checking by operation sources @@ -45,18 +47,61 @@ final class LawPermissionCheckerService implements LawPermissionCheckerServiceIn * @param Collection|RightInterface[] $rights * @param string $type * - * @return Collection + * @return Collection|RightInterface[] */ private function getRightsByType(Collection $rights, string $type): Collection { 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 string $layer * - * @return Collection + * @return Collection|RightInterface[] */ private function getRightsByLayer(Collection $rights, string $layer): Collection { @@ -98,7 +143,7 @@ final class LawPermissionCheckerService implements LawPermissionCheckerServiceIn $right = $rights[0]; $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) @@ -111,6 +156,7 @@ final class LawPermissionCheckerService implements LawPermissionCheckerServiceIn $rights = clone $this->law->getRights(); $rights = $this->getRightsByType($rights, $client->getType()); $rights = $this->getRightsByLayer($rights, $client->getLayer()); + $rights = $this->getRightsByReciever($rights, $client->getReciever()); $rights = $this->sortByPriority($rights); return $this->isGranted($rights, $client); diff --git a/application/tests/Unit/Domain/LawManagement/LawPermissionCheckerTest.php b/application/tests/Unit/Domain/LawManagement/LawPermissionCheckerTest.php index 4cbf003..35e71dc 100644 --- a/application/tests/Unit/Domain/LawManagement/LawPermissionCheckerTest.php +++ b/application/tests/Unit/Domain/LawManagement/LawPermissionCheckerTest.php @@ -14,6 +14,7 @@ use App\Entity\Meta\Law; use App\Entity\Meta\LawInterface; use App\Entity\Meta\RightInterface; use Doctrine\Common\Collections\ArrayCollection; +use App\Domain\SourceManagement\SourceMemberManager; /** * @author kevinfrantz @@ -21,25 +22,30 @@ use Doctrine\Common\Collections\ArrayCollection; 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 */ - protected $clientRight; + private $clientRight; /** - * @var SourceInterface + * @var SourceInterface The client which requests a law */ private $clientSource; + /** + * @var SourceInterface The source to which the law applies + */ + private $source; + private function getSourceMock(): SourceInterface { return new class() extends AbstractSource { @@ -53,55 +59,136 @@ class LawPermissionCheckerTest extends TestCase public function setUp(): void { - $this->clientRight = new Right(); - $this->law = new Law(); + $this->setSourceDummy(); + $this->setLawDummy(); + $this->setLawPermissionChecker(); + $this->setClientSourceDummy(); + $this->setClientRightDummy(); + } + + private function setLawPermissionChecker(): void + { $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->clientRight->setSource($this->clientSource); + $this->clientSource->setSlug('Client Source'); + } + + private function setClientRightDummy(): void + { + $this->clientRight = new Right(); $this->clientRight->setLayer(LayerType::SOURCE); $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 { - $this->assertFalse($this->checkClientPermission()); - $lawRight = clone $this->clientRight; - $lawRight->setReciever($this->clientSource); - $this->law->getRights()->add($lawRight); + $this->law->getRights()->add($this->getClonedClientRight()); $this->assertTrue($this->checkClientPermission()); $this->clientRight->setType(RightType::WRITE); $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(); - $this->clientRight->getSource()->getMemberRelation()->getMemberships()->add($parentSource); - $parentSource->getMemberRelation()->getMembers()->add($this->clientRight->getSource()); - $lawRight = clone $this->clientRight; - $lawRight->setReciever($parentSource); - $this->law->getRights()->add($lawRight); - $permission = $this->lawPermissionChecker->hasPermission($this->clientRight); - $this->assertTrue($permission); - $this->law->setRights(new ArrayCollection()); - $permission = $this->lawPermissionChecker->hasPermission($this->clientRight); - $this->assertFalse($permission); + $right = $this->getClonedClientRight(); + $right->setType(RightType::WRITE); + $this->law->getRights()->add($right); + $this->assertFalse($this->checkClientPermission()); + $right->setType(RightType::READ); + $this->assertTrue($this->checkClientPermission()); } - 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->setGrant(false); $right1->setReciever($this->clientSource); - $right2 = clone $this->clientRight; + $right2 = $this->getClonedClientRight(); $right2->setPriority(456); $right2->setGrant(true); $right2->setReciever($this->clientSource); - $this->law->setRights(new ArrayCollection([$right1, $right2])); + $this->law->setRights(new ArrayCollection([ + $right1, + $right2, + ])); $this->assertFalse($this->checkClientPermission()); $right2->setPriority(789); $right1->setPriority(101112); $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()); + } }