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

110 lines
2.6 KiB
PHP
Raw Normal View History

2018-12-08 12:51:29 +01:00
<?php
namespace Infinito\Domain\RightManagement;
2018-12-08 12:51:29 +01:00
use Infinito\Entity\Meta\RightInterface;
2018-12-08 12:51:29 +01:00
use Doctrine\Common\Collections\Collection;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Domain\SourceManagement\SourceMemberInformation;
2018-12-08 12:51:29 +01:00
/**
* @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
}
2019-01-18 18:12:31 +01:00
/**
* @param SourceInterface $clientSource
*
* @return bool
*/
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
}
2019-01-18 18:12:31 +01:00
/**
* @param string $layer
*
* @return bool
*/
private function isLayerEqual(string $layer): bool
{
return $this->right->getLayer() === $layer;
2018-12-08 12:51:29 +01:00
}
2019-01-18 18:12:31 +01:00
/**
* @param string $type
*
* @return bool
*/
private function isTypeEqual(string $type): bool
{
return $this->right->getActionType() === $type;
2018-12-08 12:51:29 +01:00
}
2019-01-18 18:12:31 +01:00
/**
* @return bool
*/
private function checkPermission(): bool
{
2018-12-08 12:51:29 +01:00
return $this->right->getGrant();
}
/**
* @return bool
*/
private function doesRightApplyToAllSources(): bool
{
return !$this->right->hasReciever();
}
/**
* @param SourceInterface $source
*
* @return bool
*/
private function doesRightApply(SourceInterface $source): bool
{
return $this->doesRightApplyToAllSources() || $this->hasClientSource($source);
}
2019-01-18 18:12:31 +01:00
/**
* @param RightInterface $right
*/
2018-12-08 12:51:29 +01:00
public function __construct(RightInterface $right)
{
$this->right = $right;
}
/**
* {@inheritdoc}
*
* @see \Infinito\Domain\RightManagement\RightCheckerInterface::isGranted()
*/
2018-12-08 12:51:29 +01:00
public function isGranted(string $layer, string $type, SourceInterface $source): bool
{
return $this->isLayerEqual($layer) && $this->isTypeEqual($type) && $this->doesRightApply($source) && $this->checkPermission();
2018-12-08 12:51:29 +01:00
}
}