Refactored code to SecureSourceChecker

This commit is contained in:
Kevin Frantz 2019-01-01 19:07:35 +01:00
parent fa87ed8606
commit 878eae62db
4 changed files with 64 additions and 10 deletions

View File

@ -4,9 +4,9 @@ namespace App\Domain\SecureLoadManagement;
use App\Entity\Source\SourceInterface; use App\Entity\Source\SourceInterface;
use App\Entity\Meta\RightInterface; use App\Entity\Meta\RightInterface;
use App\Domain\LawManagement\LawPermissionCheckerService;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Doctrine\Common\Persistence\ObjectRepository; use Doctrine\Common\Persistence\ObjectRepository;
use App\Domain\SecureManagement\SecureSourceChecker;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -52,14 +52,6 @@ final class SecureSourceLoader implements SecureSourceLoaderInterface
} }
} }
private function hasPermission(SourceInterface $source): bool
{
$requestedRight = $this->getClonedRightWithModifiedSource($source);
$law = new LawPermissionCheckerService($source->getLaw());
return $law->hasPermission($requestedRight);
}
public function __construct(ObjectRepository $sourceRepository, RightInterface $requestedRight) public function __construct(ObjectRepository $sourceRepository, RightInterface $requestedRight)
{ {
$this->sourceRepository = $sourceRepository; $this->sourceRepository = $sourceRepository;
@ -74,7 +66,9 @@ final class SecureSourceLoader implements SecureSourceLoaderInterface
public function getSource(): SourceInterface public function getSource(): SourceInterface
{ {
$source = $this->loadSource(); $source = $this->loadSource();
if ($this->hasPermission($source)) { $requestedRight = $this->getClonedRightWithModifiedSource($source);
$secureSourceChecker = new SecureSourceChecker($source);
if ($secureSourceChecker->hasPermission($requestedRight)) {
return $source; return $source;
} }
throw new AccessDeniedHttpException(); throw new AccessDeniedHttpException();

View File

@ -0,0 +1,33 @@
<?php
namespace App\Domain\SecureManagement;
use App\Entity\Meta\RightInterface;
use App\Entity\Source\SourceInterface;
use App\Domain\LawManagement\LawPermissionCheckerService;
/**
* @author kevinfrantz
*/
final class SecureSourceChecker implements SecureSourceCheckerInterface
{
/**
* @var SourceInterface
*/
private $source;
/**
* @param SourceInterface $source
*/
public function __construct(SourceInterface $source)
{
$this->source = $source;
}
public function hasPermission(RightInterface $requestedRight): bool
{
$law = new LawPermissionCheckerService($this->source->getLaw());
return $law->hasPermission($requestedRight);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Domain\SecureManagement;
use App\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
*/
interface SecureSourceCheckerInterface
{
/**
* @param RightInterface $right
*
* @return bool
*/
public function hasPermission(RightInterface $requestedRight): bool;
}

View File

@ -0,0 +1,9 @@
<?php
namespace App\Exception;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
class SourceAccessDenied extends AccessDeniedHttpException
{
}