mirror of
				https://github.com/kevinveenbirkenbach/infinito.git
				synced 2025-10-31 17:29:04 +00:00 
			
		
		
		
	Implemented draft for right checker
This commit is contained in:
		
							
								
								
									
										63
									
								
								application/src/Domain/RightManagement/RightChecker.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										63
									
								
								application/src/Domain/RightManagement/RightChecker.php
									
									
									
									
									
										Normal 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()); | ||||
|     } | ||||
| } | ||||
| @@ -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; | ||||
| } | ||||
| @@ -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{ | ||||
|          | ||||
| //     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user