infinito/application/symfony/src/Domain/RightManagement/RightChecker.php

66 lines
1.7 KiB
PHP
Raw Normal View History

2018-12-08 12:51:29 +01:00
<?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;
/**
* @todo Implement the check of conditions!
*
2018-12-08 12:51:29 +01:00
* @author kevinfrantz
*/
final class RightChecker implements RightCheckerInterface
{
/**
* @var RightInterface
*/
private $right;
/**
* @todo Implement a performant solution
*
* @return Collection
2018-12-08 12:51:29 +01:00
*/
private function getAllSourcesToWhichRightApplies(): Collection
2018-12-08 12:51:29 +01:00
{
2018-12-14 10:10:28 +01:00
$rightSourceMemberInformation = new SourceMemberInformation($this->right->getReciever());
$allSourcesToWhichRightApplies = clone $rightSourceMemberInformation->getAllMembers();
2018-12-14 10:10:28 +01:00
$allSourcesToWhichRightApplies->add($this->right->getReciever());
return $allSourcesToWhichRightApplies;
2018-12-08 12:51:29 +01:00
}
2018-12-29 23:04:06 +01:00
private function hasClientSource(SourceInterface $clientSource): bool
2018-12-08 12:51:29 +01:00
{
2018-12-29 23:04:06 +01:00
return $this->getAllSourcesToWhichRightApplies()->contains($clientSource);
2018-12-08 12:51:29 +01:00
}
private function isLayerEqual(string $layer): bool
{
return $this->right->getLayer() === $layer;
2018-12-08 12:51:29 +01:00
}
private function isTypeEqual(string $type): bool
{
return $this->right->getType() === $type;
2018-12-08 12:51:29 +01:00
}
private function checkPermission(): bool
{
2018-12-08 12:51:29 +01:00
return $this->right->getGrant();
}
public function __construct(RightInterface $right)
{
$this->right = $right;
}
public function isGranted(string $layer, string $type, SourceInterface $source): bool
{
2018-12-29 23:04:06 +01:00
return $this->isLayerEqual($layer) && $this->isTypeEqual($type) && $this->hasClientSource($source) && $this->checkPermission();
2018-12-08 12:51:29 +01:00
}
}