Refactored reciever

This commit is contained in:
Kevin Frantz 2018-12-14 10:10:28 +01:00
parent 33b1151a00
commit 0aac336609
11 changed files with 60 additions and 127 deletions

View File

@ -26,9 +26,9 @@ final class RightChecker implements RightCheckerInterface
*/ */
private function getAllSourcesToWhichRightApplies(): Collection private function getAllSourcesToWhichRightApplies(): Collection
{ {
$rightSourceMemberInformation = new SourceMemberInformation($this->right->getSource()); $rightSourceMemberInformation = new SourceMemberInformation($this->right->getReciever());
$allSourcesToWhichRightApplies = clone $rightSourceMemberInformation->getAllMembers(); $allSourcesToWhichRightApplies = clone $rightSourceMemberInformation->getAllMembers();
$allSourcesToWhichRightApplies->add($this->right->getSource()); $allSourcesToWhichRightApplies->add($this->right->getReciever());
return $allSourcesToWhichRightApplies; return $allSourcesToWhichRightApplies;
} }

View File

@ -2,7 +2,7 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use App\Entity\Meta\RecieverInterface; use App\Entity\Source\SourceInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -10,16 +10,16 @@ use App\Entity\Meta\RecieverInterface;
trait RecieverAttribut trait RecieverAttribut
{ {
/** /**
* @var RecieverInterface * @var SourceInterface
*/ */
protected $reciever; protected $reciever;
public function setReciever(RecieverInterface $reciever): void public function setReciever(SourceInterface $reciever): void
{ {
$this->reciever = $reciever; $this->reciever = $reciever;
} }
public function getReciever(): RecieverInterface public function getReciever(): SourceInterface
{ {
return $this->reciever; return $this->reciever;
} }

View File

@ -2,14 +2,14 @@
namespace App\Entity\Attribut; namespace App\Entity\Attribut;
use App\Entity\Meta\RecieverInterface; use App\Entity\Source\SourceInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*/ */
interface RecieverAttributInterface interface RecieverAttributInterface
{ {
public function setReciever(RecieverInterface $reciever): void; public function setReciever(SourceInterface $reciever): void;
public function getReciever(): RecieverInterface; public function getReciever(): SourceInterface;
} }

View File

@ -1,46 +0,0 @@
<?php
namespace App\Entity\Meta;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Source\SourceInterface;
use App\Entity\Attribut\RightAttribut;
use App\Entity\Attribut\CollectionAttribut;
/**
* @author kevinfrantz
* @ORM\Table(name="meta_reciever")
* @ORM\Entity()
*/
class Reciever extends AbstractMeta implements RecieverInterface
{
use RightAttribut, CollectionAttribut;
/**
* The right which the reciever group belongs to.
*
* @ORM\OneToOne(targetEntity="Right",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="right_id", referencedColumnName="id",onDelete="CASCADE")
*
* @var RightInterface
*/
protected $right;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Source\AbstractSource")
* @ORM\JoinTable(name="meta_reciever_members",
* joinColumns={@ORM\JoinColumn(name="reciever_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="source_id", referencedColumnName="id")}
* )
*
* @var ArrayCollection | SourceInterface[]
*/
protected $collection;
public function __construct()
{
parent::__construct();
$this->collection = new ArrayCollection();
}
}

View File

@ -1,15 +0,0 @@
<?php
namespace App\Entity\Meta;
use App\Entity\Attribut\RightAttributInterface;
use App\Entity\Attribut\CollectionAttributInterface;
/**
* It's neccessary to have an own reciever class, because if you would use a GroupSource it would lead to an infinite loop.
*
* @author kevinfrantz
*/
interface RecieverInterface extends MetaInterface, RightAttributInterface, CollectionAttributInterface
{
}

View File

@ -15,6 +15,7 @@ use App\Entity\Attribut\RecieverAttribut;
use App\Entity\Attribut\LayerAttribut; use App\Entity\Attribut\LayerAttribut;
use App\Entity\Attribut\RelationAttribut; use App\Entity\Attribut\RelationAttribut;
use App\Entity\Attribut\PriorityAttribut; use App\Entity\Attribut\PriorityAttribut;
use App\Entity\Source\SourceInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -25,6 +26,14 @@ class Right extends AbstractMeta implements RightInterface
{ {
use TypeAttribut,LawAttribut, RelationAttribut, GrantAttribut,ConditionAttribut,RecieverAttribut,LayerAttribut,PriorityAttribut; use TypeAttribut,LawAttribut, RelationAttribut, GrantAttribut,ConditionAttribut,RecieverAttribut,LayerAttribut,PriorityAttribut;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Source\AbstractSource",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="source_id", referencedColumnName="id",onDelete="CASCADE")
*
* @var SourceInterface The requested source to which the law applies
*/
protected $source;
/** /**
* @ORM\Column(type="integer") * @ORM\Column(type="integer")
* *
@ -49,10 +58,11 @@ class Right extends AbstractMeta implements RightInterface
protected $layer; protected $layer;
/** /**
* @ORM\OneToOne(targetEntity="Reciever",cascade={"persist", "remove"}) * @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")
* *
* @var RecieverInterface * @var SourceInterface
*/ */
protected $reciever; protected $reciever;
@ -84,7 +94,5 @@ class Right extends AbstractMeta implements RightInterface
parent::__construct(); parent::__construct();
$this->grant = true; $this->grant = true;
$this->priority = 0; $this->priority = 0;
$this->reciever = new Reciever();
$this->reciever->setRight($this);
} }
} }

View File

@ -15,6 +15,9 @@ 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;
/**
* @author kevinfrantz
*/
class LawPermissionCheckerTest extends TestCase class LawPermissionCheckerTest extends TestCase
{ {
/** /**
@ -30,7 +33,12 @@ class LawPermissionCheckerTest extends TestCase
/** /**
* @var RightInterface * @var RightInterface
*/ */
protected $client; protected $clientRight;
/**
* @var SourceInterface
*/
private $clientSource;
private function getSourceMock(): SourceInterface private function getSourceMock(): SourceInterface
{ {
@ -40,61 +48,60 @@ class LawPermissionCheckerTest extends TestCase
private function checkClientPermission(): bool private function checkClientPermission(): bool
{ {
return $this->lawPermissionChecker->hasPermission($this->client); return $this->lawPermissionChecker->hasPermission($this->clientRight);
} }
public function setUp(): void public function setUp(): void
{ {
$this->client = new Right(); $this->clientRight = new Right();
$this->law = new Law(); $this->law = new Law();
$this->lawPermissionChecker = new LawPermissionCheckerService($this->law); $this->lawPermissionChecker = new LawPermissionCheckerService($this->law);
$source = $this->getSourceMock(); $this->clientSource = $this->getSourceMock();
$this->client->setSource($source); $this->clientRight->setSource($this->clientSource);
$this->client->setLayer(LayerType::SOURCE); $this->clientRight->setLayer(LayerType::SOURCE);
$this->client->setType(RightType::READ); $this->clientRight->setType(RightType::READ);
} }
public function testGeneralPermission(): void public function testGeneralPermission(): void
{ {
$this->assertFalse($this->checkClientPermission()); $this->assertFalse($this->checkClientPermission());
$lawRight = clone $this->client; $lawRight = clone $this->clientRight;
$lawRight->setReciever($this->clientSource);
$this->law->getRights()->add($lawRight); $this->law->getRights()->add($lawRight);
$this->assertTrue($this->checkClientPermission()); $this->assertTrue($this->checkClientPermission());
$this->client->setType(RightType::WRITE); $this->clientRight->setType(RightType::WRITE);
$this->assertFalse($this->checkClientPermission()); $this->assertFalse($this->checkClientPermission());
} }
public function testMemberPermission(): void public function testMemberPermission(): void
{ {
$parentSource = $this->getSourceMock(); $parentSource = $this->getSourceMock();
$this->client->getSource()->getMemberRelation()->getMemberships()->add($parentSource); $this->clientRight->getSource()->getMemberRelation()->getMemberships()->add($parentSource);
$parentSource->getMemberRelation()->getMembers()->add($this->client->getSource()); $parentSource->getMemberRelation()->getMembers()->add($this->clientRight->getSource());
$lawRight = clone $this->client; $lawRight = clone $this->clientRight;
$lawRight->setSource($parentSource); $lawRight->setReciever($parentSource);
$this->law->getRights()->add($lawRight); $this->law->getRights()->add($lawRight);
$permission = $this->lawPermissionChecker->hasPermission($this->client); $permission = $this->lawPermissionChecker->hasPermission($this->clientRight);
$this->assertTrue($permission); $this->assertTrue($permission);
$this->law->setRights(new ArrayCollection()); $this->law->setRights(new ArrayCollection());
$permission = $this->lawPermissionChecker->hasPermission($this->client); $permission = $this->lawPermissionChecker->hasPermission($this->clientRight);
$this->assertFalse($permission); $this->assertFalse($permission);
} }
public function testSort(): void public function testSort(): void
{ {
$right1 = clone $this->client; $right1 = clone $this->clientRight;
$right1->setPriority(123); $right1->setPriority(123);
$right1->setGrant(false); $right1->setGrant(false);
$right2 = clone $this->client; $right1->setReciever($this->clientSource);
$right2 = clone $this->clientRight;
$right2->setPriority(456); $right2->setPriority(456);
$right2->setGrant(true); $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()); $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 test2Rights():void{
// }
} }

View File

@ -51,7 +51,7 @@ class RightCheckerTest extends TestCase
$this->type = RightType::READ; $this->type = RightType::READ;
$this->source = $this->getSourceMock(); $this->source = $this->getSourceMock();
$this->right = new Right(); $this->right = new Right();
$this->right->setSource($this->source); $this->right->setReciever($this->source);
$this->right->setType($this->type); $this->right->setType($this->type);
$this->right->setLayer($this->layer); $this->right->setLayer($this->layer);
$this->rightManager = new RightChecker($this->right); $this->rightManager = new RightChecker($this->right);
@ -68,6 +68,8 @@ class RightCheckerTest extends TestCase
$this->right->setGrant(false); $this->right->setGrant(false);
$notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $this->source); $notGranted3 = $this->rightManager->isGranted($this->layer, $this->type, $this->source);
$this->assertFalse($notGranted3); $this->assertFalse($notGranted3);
$notGranted4 = $this->rightManager->isGranted($this->layer, $this->type, $this->getSourceMock());
$this->assertFalse($notGranted4);
} }
public function testSecondDimension(): void public function testSecondDimension(): void

View File

@ -5,7 +5,7 @@ namespace Tests\Unit\Entity\Attribut;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use App\Entity\Attribut\RecieverAttributInterface; use App\Entity\Attribut\RecieverAttributInterface;
use App\Entity\Attribut\RecieverAttribut; use App\Entity\Attribut\RecieverAttribut;
use App\Entity\Meta\RecieverInterface; use App\Entity\Source\AbstractSource;
class RecieverAttributTest extends TestCase class RecieverAttributTest extends TestCase
{ {
@ -29,7 +29,7 @@ class RecieverAttributTest extends TestCase
public function testAccessors(): void public function testAccessors(): void
{ {
$reciever = $this->createMock(RecieverInterface::class); $reciever = $this->createMock(AbstractSource::class);
$this->assertNull($this->reciever->setReciever($reciever)); $this->assertNull($this->reciever->setReciever($reciever));
$this->assertEquals($reciever, $this->reciever->getReciever()); $this->assertEquals($reciever, $this->reciever->getReciever());
} }

View File

@ -1,28 +0,0 @@
<?php
namespace App\Tests\Unit\Entity\Meta;
use PHPUnit\Framework\TestCase;
use App\Entity\Meta\Reciever;
use App\Entity\Meta\RecieverInterface;
use Doctrine\Common\Collections\Collection;
class RecieverTest extends TestCase
{
/**
* @var RecieverInterface
*/
public $reciever;
public function setUp(): void
{
$this->reciever = new Reciever();
}
public function testConstructor(): void
{
$this->assertInstanceOf(Collection::class, $this->reciever->getCollection());
$this->expectException(\TypeError::class);
$this->reciever->getRight();
}
}

View File

@ -27,11 +27,16 @@ class RightTest extends TestCase
public function testConstructorGeneral(): void public function testConstructorGeneral(): void
{ {
$this->assertEquals($this->right, $this->right->getReciever()->getRight());
$this->assertTrue($this->right->getGrant()); $this->assertTrue($this->right->getGrant());
$this->assertEquals(0, $this->right->getPriority()); $this->assertEquals(0, $this->right->getPriority());
} }
public function testConstructorReciever(): void
{
$this->expectException(\TypeError::class);
$this->right->getReciever();
}
public function testConstructorLayer(): void public function testConstructorLayer(): void
{ {
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);