mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 09:19:08 +00:00 
			
		
		
		
	Added grant to all function to RightChecker and LawPermissionChecker
This commit is contained in:
		| @@ -6,6 +6,8 @@ use App\Entity\Source\SourceInterface; | ||||
|  | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
|  * | ||||
|  * @see RecieverAttributInterface | ||||
|  */ | ||||
| trait RecieverAttribut | ||||
| { | ||||
| @@ -14,13 +16,27 @@ trait RecieverAttribut | ||||
|      */ | ||||
|     protected $reciever; | ||||
|  | ||||
|     public function setReciever(SourceInterface $reciever): void | ||||
|     /** | ||||
|      * @param SourceInterface $reciever | ||||
|      */ | ||||
|     public function setReciever(?SourceInterface $reciever): void | ||||
|     { | ||||
|         $this->reciever = $reciever; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return SourceInterface | ||||
|      */ | ||||
|     public function getReciever(): SourceInterface | ||||
|     { | ||||
|         return $this->reciever; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return bool | ||||
|      */ | ||||
|     public function hasReciever(): bool | ||||
|     { | ||||
|         return isset($this->reciever); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -9,7 +9,18 @@ use App\Entity\Source\SourceInterface; | ||||
|  */ | ||||
| interface RecieverAttributInterface | ||||
| { | ||||
|     public function setReciever(SourceInterface $reciever): void; | ||||
|     /** | ||||
|      * @param SourceInterface|null $reciever If null, then all recievers MUST be addressed. Otherwise just a special reciever | ||||
|      */ | ||||
|     public function setReciever(?SourceInterface $reciever): void; | ||||
|  | ||||
|     /** | ||||
|      * @return SourceInterface | ||||
|      */ | ||||
|     public function getReciever(): SourceInterface; | ||||
|  | ||||
|     /** | ||||
|      * @return bool True if it has a special reciever | ||||
|      */ | ||||
|     public function hasReciever(): bool; | ||||
| } | ||||
|   | ||||
| @@ -4,6 +4,9 @@ namespace App\Domain\FixtureManagement\FixtureSource; | ||||
|  | ||||
| use App\Entity\Source\SourceInterface; | ||||
| use App\Entity\Source\Primitive\Text\TextSource; | ||||
| use App\Entity\Meta\Right; | ||||
| use App\DBAL\Types\Meta\Right\LayerType; | ||||
| use App\DBAL\Types\Meta\Right\CRUDType; | ||||
|  | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
| @@ -22,6 +25,11 @@ final class ImpressumFixtureSource extends AbstractFixtureSource | ||||
|         $impressumSource = new TextSource(); | ||||
|         $impressumSource->setText('Example Impressum'); | ||||
|         $impressumSource->setSlug(self::SLUG); | ||||
|         $right = new Right(); | ||||
|         $right->setSource($impressumSource); | ||||
|         $right->setLayer(LayerType::SOURCE); | ||||
|         $right->setCrud(CRUDType::READ); | ||||
|         $impressumSource->getLaw()->getRights()->add($right); | ||||
|  | ||||
|         return $impressumSource; | ||||
|     } | ||||
|   | ||||
| @@ -54,17 +54,27 @@ final class LawPermissionChecker implements LawPermissionCheckerInterface | ||||
|         return $this->getFilteredRights($rights, $type, 'Crud'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param RightInterface $right | ||||
|      * | ||||
|      * @return bool True if right applies to all | ||||
|      */ | ||||
|     private function doesRightApplyToAll(RightInterface $right): bool | ||||
|     { | ||||
|         return !$right->hasReciever(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param Collection|RightInterface[] $rights | ||||
|      * @param SourceInterface             $reciever | ||||
|      * @param RightInterface              $requestedRight | ||||
|      * | ||||
|      * @return Collection|RightInterface[] | ||||
|      */ | ||||
|     private function getRightsByReciever(Collection $rights, SourceInterface $reciever): Collection | ||||
|     private function getRightsByReciever(Collection $rights, RightInterface $requestedRight): Collection | ||||
|     { | ||||
|         $result = new ArrayCollection(); | ||||
|         foreach ($rights as $right) { | ||||
|             if ($right->getReciever() === $reciever || $this->memberExist($right, $reciever)) { | ||||
|             if ($this->doesRightApplyToAll($right) || $right->getReciever() === $requestedRight->getReciever() || $this->memberExist($right, $requestedRight->getReciever())) { | ||||
|                 $result->add($right); | ||||
|             } | ||||
|         } | ||||
| @@ -160,7 +170,7 @@ final class LawPermissionChecker implements LawPermissionCheckerInterface | ||||
|         $rights = clone $this->law->getRights(); | ||||
|         $rights = $this->getRightsByCrud($rights, $clientRight->getCrud()); | ||||
|         $rights = $this->getRightsByLayer($rights, $clientRight->getLayer()); | ||||
|         $rights = $this->getRightsByReciever($rights, $clientRight->getReciever()); | ||||
|         $rights = $this->getRightsByReciever($rights, $clientRight); | ||||
|         $rights = $this->sortByPriority($rights); | ||||
|  | ||||
|         return $this->isGranted($rights, $clientRight); | ||||
|   | ||||
| @@ -110,7 +110,7 @@ abstract class AbstractRequestedRightFacade implements RequestedRightInterface | ||||
|      * | ||||
|      * @see \App\Attribut\RecieverAttributInterface::setReciever() | ||||
|      */ | ||||
|     public function setReciever(SourceInterface $reciever): void | ||||
|     public function setReciever(?SourceInterface $reciever): void | ||||
|     { | ||||
|         $this->requestedRight->setReciever($reciever); | ||||
|     } | ||||
| @@ -124,4 +124,14 @@ abstract class AbstractRequestedRightFacade implements RequestedRightInterface | ||||
|     { | ||||
|         return $this->requestedRight->hasRequestedEntity(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * {@inheritdoc} | ||||
|      * | ||||
|      * @see \App\Attribut\RecieverAttributInterface::hasReciever() | ||||
|      */ | ||||
|     public function hasReciever(): bool | ||||
|     { | ||||
|         return $this->requestedRight->hasReciever(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -35,7 +35,7 @@ class RequestedUser extends AbstractRequestedRightFacade implements RequestedUse | ||||
|      * {@inheritdoc} | ||||
|      * @see \App\Attribut\RecieverAttributInterface::setReciever() | ||||
|      */ | ||||
|     public function setReciever(SourceInterface $reciever): void | ||||
|     public function setReciever(?SourceInterface $reciever): void | ||||
|     { | ||||
|         throw new SetNotPossibleException('It\'s not possible to set the reciever! Set it via '.UserSourceDirectorInterface::class.'!'); | ||||
|     } | ||||
|   | ||||
| @@ -71,6 +71,24 @@ final class RightChecker implements RightCheckerInterface | ||||
|         return $this->right->getGrant(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return bool | ||||
|      */ | ||||
|     private function doesRightApplyToAllSources(): bool | ||||
|     { | ||||
|         return !$this->right->hasReciever(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param SourceInterface $source | ||||
|      * | ||||
|      * @return bool | ||||
|      */ | ||||
|     private function doesRightApply(SourceInterface $source): bool | ||||
|     { | ||||
|         return $this->doesRightApplyToAllSources() || $this->hasClientSource($source); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param RightInterface $right | ||||
|      */ | ||||
| @@ -79,8 +97,13 @@ final class RightChecker implements RightCheckerInterface | ||||
|         $this->right = $right; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * {@inheritdoc} | ||||
|      * | ||||
|      * @see \App\Domain\RightManagement\RightCheckerInterface::isGranted() | ||||
|      */ | ||||
|     public function isGranted(string $layer, string $type, SourceInterface $source): bool | ||||
|     { | ||||
|         return $this->isLayerEqual($layer) && $this->isTypeEqual($type) && $this->hasClientSource($source) && $this->checkPermission(); | ||||
|         return $this->isLayerEqual($layer) && $this->isTypeEqual($type) && $this->doesRightApply($source) && $this->checkPermission(); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -10,6 +10,9 @@ use App\DBAL\Types\Meta\Right\CRUDType; | ||||
|  */ | ||||
| final class RightLayerCombinationService implements RightLayerCombinationServiceInterface | ||||
| { | ||||
|     /** | ||||
|      * @var array | ||||
|      */ | ||||
|     const EXLUDED_RIGHTS_BY_LAYER = [ | ||||
|         LayerType::HEREDITY => [ | ||||
|             CRUDType::CREATE, | ||||
|   | ||||
| @@ -60,9 +60,9 @@ class Right extends AbstractMeta implements RightInterface | ||||
|     /** | ||||
|      * @todo Test and implement it on an correct way! | ||||
|      * @ORM\OneToOne(targetEntity="App\Entity\Source\AbstractSource",cascade={"persist"}) | ||||
|      * @ORM\JoinColumn(name="reciever_id", referencedColumnName="id",onDelete="CASCADE") | ||||
|      * @ORM\JoinColumn(name="reciever_id", referencedColumnName="id",onDelete="CASCADE",nullable=true) | ||||
|      * | ||||
|      * @var SourceInterface | ||||
|      * @var SourceInterface|null if null then the right should apply to all sources | ||||
|      */ | ||||
|     protected $reciever; | ||||
|  | ||||
|   | ||||
| @@ -23,6 +23,7 @@ class RecieverAttributTest extends TestCase | ||||
|  | ||||
|     public function testConstructor(): void | ||||
|     { | ||||
|         $this->assertFalse($this->reciever->hasReciever()); | ||||
|         $this->expectException(\TypeError::class); | ||||
|         $this->reciever->getReciever(); | ||||
|     } | ||||
| @@ -30,7 +31,9 @@ class RecieverAttributTest extends TestCase | ||||
|     public function testAccessors(): void | ||||
|     { | ||||
|         $reciever = $this->createMock(AbstractSource::class); | ||||
|         $this->assertFalse($this->reciever->hasReciever()); | ||||
|         $this->assertNull($this->reciever->setReciever($reciever)); | ||||
|         $this->assertEquals($reciever, $this->reciever->getReciever()); | ||||
|         $this->assertTrue($this->reciever->hasReciever()); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -203,4 +203,24 @@ class LawPermissionCheckerTest extends TestCase | ||||
|         $this->law->setGrant(true); | ||||
|         $this->assertTrue($this->checkClientPermission()); | ||||
|     } | ||||
|  | ||||
|     public function testAppliesToAll(): void | ||||
|     { | ||||
|         $clientSource = new PureSource(); | ||||
|         $clientRight = new Right(); | ||||
|         $clientRight->setLayer(LayerType::SOURCE); | ||||
|         $clientRight->setCrud(CRUDType::READ); | ||||
|         $clientRight->setSource($this->source); | ||||
|         $clientRight->setReciever($clientSource); | ||||
|         $this->assertFalse($this->lawPermissionChecker->hasPermission($clientRight)); | ||||
|         $right = new Right(); | ||||
|         $right->setLayer(LayerType::SOURCE); | ||||
|         $right->setCrud(CRUDType::READ); | ||||
|         $right->setSource($this->source); | ||||
|         $this->law->getRights()->add($right); | ||||
|         $this->assertTrue($this->lawPermissionChecker->hasPermission($clientRight)); | ||||
|         $otherReciever = new PureSource(); | ||||
|         $right->setReciever($otherReciever); | ||||
|         $this->assertFalse($this->lawPermissionChecker->hasPermission($clientRight)); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -12,6 +12,9 @@ use App\Domain\RightManagement\RightChecker; | ||||
| use App\DBAL\Types\Meta\Right\CRUDType; | ||||
| use App\Entity\Source\PureSource; | ||||
|  | ||||
| /** | ||||
|  * @author kevinfrantz | ||||
|  */ | ||||
| class RightCheckerTest extends TestCase | ||||
| { | ||||
|     /** | ||||
| @@ -97,4 +100,16 @@ class RightCheckerTest extends TestCase | ||||
|         $notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $thirdSource); | ||||
|         $this->assertFalse($notGranted3); | ||||
|     } | ||||
|  | ||||
|     public function testAppliesToAll(): void | ||||
|     { | ||||
|         $this->assertNull($this->right->setReciever(null)); | ||||
|         $this->assertTrue($this->rightManager->isGranted($this->layer, $this->type, $this->source)); | ||||
|         $source2 = new PureSource(); | ||||
|         $this->assertTrue($this->rightManager->isGranted($this->layer, $this->type, $source2)); | ||||
|         $source3 = new PureSource(); | ||||
|         $this->assertNull($this->right->setReciever($source3)); | ||||
|         $this->assertTrue($this->rightManager->isGranted($this->layer, $this->type, $source3)); | ||||
|         $this->assertFalse($this->rightManager->isGranted($this->layer, $this->type, $source2)); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user