infinito/application/src/Entity/Right.php

117 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-09-13 15:55:48 +02:00
namespace App\Entity;
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\NodeAttribut;
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-03 15:50:46 +02:00
use App\Entity\LawInterface;
use App\Entity\RecieverGroupInterface;
use App\Entity\NodeInterface;
use App\Entity\RightInterface;
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")
*/
class Right extends AbstractEntity implements RightInterface
{
2018-09-21 19:43:48 +02:00
use TypeAttribut,LawAttribut, NodeAttribut, 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;
/**
* @ORM\ManyToOne(targetEntity="Node")
* @ORM\JoinColumn(name="node_id", referencedColumnName="id")
*
* @var NodeInterface
*/
protected $node;
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-09-21 16:44:58 +02:00
* @ORM\OneToOne(targetEntity="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-09-21 15:48:23 +02:00
public function isGranted(NodeInterface $node, string $layer, string $right): bool
2018-09-13 16:51:58 +02:00
{
2018-09-21 15:48:23 +02:00
if ($this->layer == $layer && $this->type == $right && $this->checkIfNodeIsReciever($node) && $this->getConditionBoolOrTrue()) {
return $this->grant;
}
return !($this->grant);
}
private function getConditionBoolOrTrue(): bool
{
if ($this->hasCondition()) {
return $this->condition->getResult()->getBool();
}
return true;
}
private function checkIfNodeIsReciever(NodeInterface $node): bool
{
return $this->recieverGroup->getAllRecievers()->contains($node);
2018-09-13 16:51:58 +02:00
}
2018-09-13 15:55:48 +02:00
}