infinito/application/src/Entity/Meta/Right.php

113 lines
3.1 KiB
PHP
Raw Normal View History

2018-09-13 15:55:48 +02:00
<?php
2018-09-13 16:51:58 +02:00
2018-10-28 15:25:32 +01:00
namespace App\Entity\Meta;
2018-09-13 15:55:48 +02:00
use App\Entity\Attribut\TypeAttribut;
use Doctrine\ORM\Mapping as ORM;
use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
2018-09-13 16:37:33 +02:00
use App\Entity\Attribut\LawAttribut;
2018-09-21 15:48:23 +02:00
use App\DBAL\Types\LayerType;
use App\DBAL\Types\RightType;
use App\Entity\Attribut\GrantAttribut;
use App\Logic\Operation\OperationInterface;
use App\Entity\Attribut\ConditionAttribut;
use App\Entity\Attribut\RecieverGroupAttribut;
use App\Entity\Attribut\LayerAttribut;
2018-10-27 14:56:26 +02:00
use App\Entity\Attribut\RelationAttribut;
2018-09-13 15:55:48 +02:00
/**
* @author kevinfrantz
2018-09-13 16:37:33 +02:00
* @ORM\Table(name="`right`")
2018-09-13 15:55:48 +02:00
* @ORM\Entity(repositoryClass="App\Repository\RightRepository")
*/
2018-10-28 15:25:32 +01:00
class Right extends AbstractMeta implements RightInterface
2018-09-13 15:55:48 +02:00
{
2018-10-27 14:56:26 +02:00
use TypeAttribut,LawAttribut, RelationAttribut, GrantAttribut,ConditionAttribut,RecieverGroupAttribut,LayerAttribut;
2018-09-13 16:51:58 +02:00
2018-09-13 16:37:33 +02:00
/**
* @ORM\ManyToOne(targetEntity="Law", inversedBy="rights")
2018-09-13 16:37:33 +02:00
* @ORM\JoinColumn(name="law_id", referencedColumnName="id")
2018-09-13 16:51:58 +02:00
*
2018-09-13 16:37:33 +02:00
* @var LawInterface
*/
protected $law;
2018-09-13 16:51:58 +02:00
2018-09-21 15:48:23 +02:00
/**
2018-09-21 16:44:58 +02:00
* @ORM\Column(name="layer", type="LayerType", nullable=false)
2018-09-21 15:48:23 +02:00
* @DoctrineAssert\Enum(entity="App\DBAL\Types\LayerType")
*
* @var string
*/
protected $layer;
/**
* @ORM\OneToOne(targetEntity="RecieverGroup",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="reciever_id", referencedColumnName="id")
*
* @var RecieverGroupInterface
*/
protected $recieverGroup;
/**
* @ORM\Column(type="boolean",name="`grant`")
*
* @var bool
*/
protected $grant;
/**
2018-10-27 14:56:26 +02:00
* @ORM\ManyToOne(targetEntity="Relation")
* @ORM\JoinColumn(name="relation_id", referencedColumnName="id")
2018-09-21 15:48:23 +02:00
*
2018-10-27 14:56:26 +02:00
* @var RelationInterface
2018-09-21 15:48:23 +02:00
*/
2018-10-27 14:56:26 +02:00
protected $relation;
2018-09-21 15:48:23 +02:00
2018-09-13 15:55:48 +02:00
/**
* @ORM\Column(name="type", type="RightType", nullable=false)
* @DoctrineAssert\Enum(entity="App\DBAL\Types\RightType")
2018-09-13 16:51:58 +02:00
*
2018-09-13 15:55:48 +02:00
* @var string
*/
protected $type;
2018-09-13 16:51:58 +02:00
/**
2018-10-03 16:48:16 +02:00
* @ORM\OneToOne(targetEntity="App\Entity\Source\Operation\AbstractOperation",cascade={"persist"})
2018-09-21 19:43:48 +02:00
* @ORM\JoinColumn(name="operation_id", referencedColumnName="id",nullable=true)
*
2018-09-21 15:48:23 +02:00
* @var OperationInterface
2018-09-13 16:51:58 +02:00
*/
2018-09-21 15:48:23 +02:00
protected $condition;
2018-09-13 16:51:58 +02:00
2018-09-21 15:48:23 +02:00
public function __construct()
2018-09-13 16:51:58 +02:00
{
2018-09-21 15:48:23 +02:00
parent::__construct();
$this->grant = true;
2018-09-21 19:43:48 +02:00
//$this->node = new Node();
//$this->recieverGroup = new RecieverGroup();
2018-09-13 16:51:58 +02:00
}
2018-09-13 15:55:48 +02:00
2018-10-27 14:56:26 +02:00
public function isGranted(RelationInterface $relation, string $layer, string $right): bool
2018-09-13 16:51:58 +02:00
{
2018-10-27 14:56:26 +02:00
if ($this->layer == $layer && $this->type == $right && $this->checkIfNodeIsReciever($relation) && $this->getConditionBoolOrTrue()) {
2018-09-21 15:48:23 +02:00
return $this->grant;
}
return !($this->grant);
}
private function getConditionBoolOrTrue(): bool
{
if ($this->hasCondition()) {
return $this->condition->getResult()->getBool();
}
return true;
}
2018-10-27 14:56:26 +02:00
private function checkIfNodeIsReciever(RelationInterface $relation): bool
2018-09-21 15:48:23 +02:00
{
2018-10-27 14:56:26 +02:00
return $this->recieverGroup->getAllRecievers()->contains($relation);
2018-09-13 16:51:58 +02:00
}
2018-09-13 15:55:48 +02:00
}