Implemented draft for ActionManagement

This commit is contained in:
Kevin Frantz 2019-01-27 15:28:25 +01:00
parent f7242f725e
commit 707df1b951
46 changed files with 686 additions and 831 deletions

View File

@ -6,7 +6,6 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Controller\API\AbstractAPIController;
use App\Entity\Source\PureSource;
/**
* @author kevinfrantz
@ -28,52 +27,6 @@ class SourceApiController extends AbstractAPIController
{
}
// /**
// * @Route("/{_locale}/api/source.{_format}",
// * defaults={"_format"="json"} ,
// * methods={"POST","GET"}
// * )
// * {@inheritdoc}
// *
// * @see \App\Controller\API\AbstractAPIController::create()
// */
// public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response
// {
// $response = new Response();
// if (!$this->getUser()) {
// //throw $this->createAccessDeniedException('The user must be logged in!');
// }
// if (Request::METHOD_POST === $request->getMethod()) {
// $response = new Response();
// $response->setContent('Post Request!');
// return $response;
// }
// /**
// * @var SecureSourceCreatorInterface
// */
// $sourceCreator = $crudFactory->create();
// $response->setContent($sourceCreator->create()->getText());
// return $response;
// // $response = new Response();
// // $response->setContent('GET Request!');
// // return $response;
// // $requestedSource = new PureSource();
// // $requestedSource->setSlug(SystemSlugType::IMPRINT);
// // $requestedRight = new Right();
// // $requestedRight->setSource($requestedSource);
// // $requestedRight->setLayer(LayerType::SOURCE);
// // $requestedRight->setCrud(CRUDType::READ);
// // $sourceResponseManager = new SourceRESTResponseManager($this->getUser(), $entityManager, $requestedRight, $this->getViewHandler());
// // return $sourceResponseManager->getResponse();
// }
/**
* @Route("/{_locale}/api/source/{identifier}.{_format}",
* defaults={"_format"="json"} ,

View File

@ -0,0 +1,47 @@
<?php
namespace App\Domain\ActionManagement;
use App\Entity\EntityInterface;
use App\Exception\NotSecureException;
use App\Exception\NotValidByFormException;
/**
* @author kevinfrantz
*/
abstract class AbstractAction extends AbstractActionConstructor implements ActionInterface
{
/**
* @return bool
*/
abstract protected function isSecure(): bool;
/**
* @return bool
*/
abstract protected function isValidByForm(): bool;
/**
* Process the routine.
*
* @return EntityInterface|EntityInterface[]|null
*/
abstract protected function proccess();
/**
* @throws \Exception
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\ActionInterface::execute()
*/
public function execute()
{
if ($this->isSecure()) {
if ($this->isValidByForm()) {
return $this->proccess();
}
throw new NotValidByFormException('The requested Entity is not valid!');
}
throw new NotSecureException("You don't have the permission to execute this action!");
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Domain\ActionManagement;
/**
* This class just containes the constructor
* It is used by concrete actions and the factory.
*
* @author kevinfrantz
*/
abstract class AbstractActionConstructor
{
/**
* @var ActionServiceInterface
*/
protected $actionService;
/**
* @param ActionServiceInterface $actionService
*/
final public function __construct(ActionServiceInterface $actionService)
{
$this->actionService = $actionService;
}
}

View File

@ -0,0 +1,74 @@
<?php
namespace App\Domain\ActionManagement;
/**
* @author kevinfrantz
*/
final class ActionFactoryService extends AbstractActionConstructor implements ActionFactoryServiceInterface
{
const BASE_NAMESPACE = 'App\\Domain\\ActionManagement\\';
const CLASS_SUFFIX = 'Action';
/**
* @param string $name
*
* @return string
*/
private function ucfirst(string $name): string
{
return ucfirst(strtolower($name));
}
/**
* @param string $action
* @param string $layer
*
* @return string
*/
private function getClassName(string $action, string $layer = ''): string
{
return $this->ucfirst($action).$this->ucfirst($layer).self::CLASS_SUFFIX;
}
/**
* @param string $layer
* @param string $action
*
* @return string
*/
private function getActionNamespace(string $action, string $layer = ''): string
{
return self::BASE_NAMESPACE.$this->ucfirst($action).'\\'.$this->getClassName($action, $layer);
}
/**
* @return string
*/
private function generateFullClassName(): string
{
$requestedAction = $this->actionService->getRequestedAction();
$action = $requestedAction->getActionType();
$layer = $requestedAction->getLayer();
$class = $this->getActionNamespace($action, $layer);
if (class_exists($class)) {
return $class;
}
$defaultClass = $this->getActionNamespace($action);
return $defaultClass;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\ActionFactoryServiceInterface::create()
*/
public function create(): ActionInterface
{
$class = $this->generateFullClassName();
return new $class($this->actionService);
}
}

View File

@ -0,0 +1,16 @@
<?php
namespace App\Domain\ActionManagement;
/**
* Offers a function to create an action object by the RequestedActionService.
*
* @author kevinfrantz
*/
interface ActionFactoryServiceInterface
{
/**
* @return ActionInterface
*/
public function create(): ActionInterface;
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Domain\ActionManagement;
use App\Entity\EntityInterface;
interface ActionInterface
{
/**
* Executes the action.
*
* @return EntityInterface|EntityInterface[]|null
*/
public function execute();
}

View File

@ -4,12 +4,25 @@ namespace App\Domain\ActionManagement;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Domain\FormManagement\EntityFormBuilderServiceInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use App\Entity\EntityInterface;
use Doctrine\ORM\EntityManagerInterface;
/**
* @author kevinfrantz
*/
final class ActionService implements ActionServiceInterface
{
/**
* @var Request
*/
private $requestStack;
/**
* @var RequestedActionInterface
*/
@ -20,13 +33,32 @@ final class ActionService implements ActionServiceInterface
*/
private $secureRequestedRightChecker;
/**
* @var LayerRepositoryFactoryServiceInterface
*/
private $layerRepositoryFactoryService;
/**
* @var
*/
private $entityFormBuilderService;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @param RequestedActionInterface $requestedAction
*/
public function __construct(RequestedActionInterface $requestedAction, SecureRequestedRightCheckerInterface $secureRequestedRightChecker)
public function __construct(RequestedActionInterface $requestedAction, SecureRequestedRightCheckerInterface $secureRequestedRightChecker, RequestStack $requestStack, LayerRepositoryFactoryServiceInterface $layerRepositoryFactoryService, EntityFormBuilderServiceInterface $entityFormBuilderService, EntityManagerInterface $entityManager)
{
$this->requestedAction = $requestedAction;
$this->secureRequestedRightChecker = $secureRequestedRightChecker;
$this->requestStack = $requestStack;
$this->layerRepositoryFactoryService = $layerRepositoryFactoryService;
$this->entityFormBuilderService = $entityFormBuilderService;
$this->entityManager = $entityManager;
}
/**
@ -48,4 +80,44 @@ final class ActionService implements ActionServiceInterface
{
return $this->secureRequestedRightChecker->check($this->requestedAction);
}
/**
* @return FormBuilderInterface
*/
public function getForm(EntityInterface $entity): FormBuilderInterface
{
$this->entityFormBuilderService->create($entity);
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\ActionServiceInterface::getRequest()
*/
public function getRequest(): Request
{
return $this->requestStack->getCurrentRequest();
}
/**
* {@use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;inheritDoc}.
*
* @see \App\Domain\ActionManagement\ActionServiceInterface::getRepository()
*/
public function getRepository(): RepositoryInterface
{
$layer = $this->requestedAction->getLayer();
return $this->layerRepositoryFactoryService->getRepository($layer);
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\ActionServiceInterface::getEntityManager()
*/
public function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}
}

View File

@ -3,6 +3,11 @@
namespace App\Domain\ActionManagement;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use Symfony\Component\HttpFoundation\Request;
use App\Repository\RepositoryInterface;
use Symfony\Component\Form\FormBuilderInterface;
use App\Entity\EntityInterface;
use Doctrine\ORM\EntityManagerInterface;
/**
* This interface offers all classes for managing an Action.
@ -20,4 +25,26 @@ interface ActionServiceInterface
* @return bool true if the action permissions are right
*/
public function isRequestedActionSecure(): bool;
/**
* @return Request
*/
public function getRequest(): Request;
/**
* @return RepositoryInterface
*/
public function getRepository(): RepositoryInterface;
/**
* @param EntityInterface $entity
*
* @return FormBuilderInterface
*/
public function getForm(EntityInterface $entity): FormBuilderInterface;
/**
* @return EntityManagerInterface
*/
public function getEntityManager(): EntityManagerInterface;
}

View File

@ -0,0 +1,12 @@
<?php
namespace App\Domain\ActionManagement\Create;
use App\Domain\ActionManagement\AbstractAction;
/**
* @author kevinfrantz
*/
abstract class AbstractCreateAction extends AbstractAction
{
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Domain\ActionManagement\Create;
/**
* @author kevinfrantz
*/
final class CreateSourceAction extends AbstractCreateAction
{
protected function isSecure(): bool
{
}
protected function isValidByForm(): bool
{
}
protected function proccess()
{
}
}

View File

@ -0,0 +1,45 @@
<?php
namespace App\Domain\ActionManagement\Delete;
use App\Domain\ActionManagement\AbstractAction;
/**
* @author kevinfrantz
* Declare as not final as soon as you need it!
*/
final class DeleteAction extends AbstractAction
{
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
return $this->isSecure();
}
/**
* @todo Implement!
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::isValidByForm()
*/
protected function isValidByForm(): bool
{
return true;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::proccess()
*/
protected function proccess()
{
$entityManager = $this->actionService->getEntityManager();
$entity = $this->actionService->getRequestedAction()->getRequestedEntity()->getEntity();
$entityManager->remove($entity);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Domain\ActionManagement\Read;
use App\Domain\ActionManagement\AbstractAction;
/**
* @author kevinfrantz
*/
final class ReadAction extends AbstractAction implements ReadActionInterface
{
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::isSecure()
*/
protected function isSecure(): bool
{
return $this->actionService->isRequestedActionSecure();
}
/**
* @todo Implement!
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::isValidByForm()
*/
protected function isValidByForm(): bool
{
return true;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ActionManagement\AbstractAction::proccess()
*/
protected function proccess()
{
return $this->actionService->getRequestedAction()->getRequestedEntity()->getEntity();
}
}

View File

@ -0,0 +1,14 @@
<?php
namespace App\Domain\ActionManagement\Read;
use App\Domain\ActionManagement\ActionInterface;
/**
* Needed for mocking with PHPUnit!
*
* @author kevinfrantz
*/
interface ReadActionInterface extends ActionInterface
{
}

View File

@ -0,0 +1,7 @@
<?php
namespace App\Domain\ActionManagement\Update;
class UpdateSourceAction
{
}

View File

@ -1,82 +0,0 @@
<?php
namespace App\Domain\ResponseManagement;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\UserInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Meta\RightInterface;
use Symfony\Component\Security\Core\User\UserInterface as CoreUserInterface;
use FOS\RestBundle\View\ViewHandlerInterface;
use App\Entity\Source\SourceInterface;
use FOS\RestBundle\View\View;
use App\Exception\AllreadyDefinedException;
use App\Domain\SecureCRUDManagement\CRUD\Read\SecureSourceReadService;
/**
* @author kevinfrantz
*
* @todo Implement as a service!
*/
final class SourceRESTResponseManagerService implements SourceRESTResponseManagerServiceInterface
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var RightInterface
*/
private $requestedRight;
/**
* @var SourceInterface
*/
private $loadedSource;
/**
* @var UserInterface
*/
private $user;
public function __construct(CoreUserInterface $user, SecureSourceReadService $secureSourceRead, EntityManagerInterface $entityManager, RightInterface $requestedRight)
{
$this->entityManager = $entityManager;
$this->user = $user;
$this->setRequestedRight($requestedRight);
$this->setLoadedSource();
$this->setView();
}
private function setView(): void
{
$this->view = new View($this->loadedSource, 200);
}
/**
* @param RightInterface $requestedRight
*
* @throws AllreadyDefinedException
*/
private function setRequestedRight(RightInterface $requestedRight): void
{
try {
$requestedRight->getReciever();
throw new AllreadyDefinedException('The reciever is allready defined.');
} catch (\TypeError $error) {
$requestedRight->setReciever($this->user->getSource());
$this->requestedRight = $requestedRight;
}
}
/**
* {@inheritdoc}
*
* @see \App\Domain\ResponseManagement\SourceRESTResponseManagerServiceInterface::getResponse()
*/
public function getResponse(ViewHandlerInterface $viewHandler): Response
{
return $viewHandler->handle($this->view);
}
}

View File

@ -1,14 +0,0 @@
<?php
namespace App\Domain\ResponseManagement;
use Symfony\Component\HttpFoundation\Response;
use FOS\RestBundle\View\ViewHandlerInterface;
interface SourceRESTResponseManagerServiceInterface
{
/**
* @return Response
*/
public function getResponse(ViewHandlerInterface $viewHandler): Response;
}

View File

@ -1,40 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
use Doctrine\ORM\EntityManagerInterface;
/**
* @author kevinfrantz
*/
abstract class AbstractSecureCRUDService
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var Security
*/
protected $security;
/**
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @param RequestStack $requestStack
* @param Security $security
* @param EntityManagerInterface $entityManager
*/
public function __construct(RequestStack $requestStack, Security $security, EntityManagerInterface $entityManager)
{
$this->requestStack = $requestStack;
$this->security = $security;
$this->entityManager = $entityManager;
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD;
use App\Domain\SecureCRUDManagement\AbstractSecureCRUDService as AbstractSecureCRUDServiceParent;
/**
* @author kevinfrantz
*/
abstract class AbstractSecureCRUDService extends AbstractSecureCRUDServiceParent implements SecureCRUDServiceInterface
{
}

View File

@ -1,12 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Domain\SecureCRUDManagement\CRUD\AbstractSecureCRUDService;
/**
* @author kevinfrantz
*/
abstract class AbstractSecureCreateService extends AbstractSecureCRUDService implements SecureCreateServiceInterface
{
}

View File

@ -1,19 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Entity\EntityInterface;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDServiceInterface;
/**
* @todo Implement!
*
* @author kevinfrantz
*/
interface SecureCreateServiceInterface extends SecureCRUDServiceInterface
{
/**
* @return EntityInterface The created entity
*/
public function create(): EntityInterface;
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
final class SecureHeredityCreateService extends AbstractSecureCreateService
{
/**
* @todo Implement
*
* @return EntityInterface
*/
public function create(): EntityInterface
{
}
}

View File

@ -1,16 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
final class SecureMemberCreateService extends AbstractSecureCreateService
{
public function create(): EntityInterface
{
//todo implement!
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Entity\EntityInterface;
/**
* @author kevinfrantz
*/
final class SecureRightCreateService extends AbstractSecureCreateService
{
/**
* @todo Implement!
*
* @return EntityInterface
*/
public function create(): EntityInterface
{
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Entity\EntityInterface;
use App\Entity\Source\Primitive\Text\TextSource;
/**
* @author kevinfrantz
*
* @todo Implement!
*/
final class SecureSourceCreateService extends AbstractSecureCreateService
{
/**
* @return EntityInterface
*/
public function create(): EntityInterface
{
$source = new TextSource();
$source->setText('Hello World!');
return $source;
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
use App\Domain\SecureCRUDManagement\CRUD\AbstractSecureCRUDService;
/**
* @author kevinfrantz
*/
abstract class AbstractSecureReadService extends AbstractSecureCRUDService implements SecureReadServiceInterface
{
}

View File

@ -1,21 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
use App\Entity\EntityInterface;
use App\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
*/
final class SecureLawReadService extends AbstractSecureReadService
{
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\CRUD\Read\SecureReadServiceInterface::read()
*/
public function read(RightInterface $requestedRight): EntityInterface
{
}
}

View File

@ -1,21 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
use App\Entity\EntityInterface;
use App\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
*/
class SecureMemberReadService extends AbstractSecureReadService
{
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\CRUD\Read\SecureReadServiceInterface::read()
*/
public function read(RightInterface $requestedRight): EntityInterface
{
}
}

View File

@ -1,20 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDServiceInterface;
use App\Entity\EntityInterface;
use App\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
*/
interface SecureReadServiceInterface extends SecureCRUDServiceInterface
{
/**
* @param RightInterface $requestedRight
*
* @return EntityInterface
*/
public function read(RightInterface $requestedRight): EntityInterface;
}

View File

@ -1,21 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
use App\Entity\EntityInterface;
use App\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
*/
final class SecureRightReadService extends AbstractSecureReadService
{
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\CRUD\Read\SecureReadServiceInterface::read()
*/
public function read(RightInterface $requestedRight): EntityInterface
{
}
}

View File

@ -1,70 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
use App\Entity\Source\SourceInterface;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureManagement\SecureSourceChecker;
use App\Exception\SourceAccessDenied;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Source\AbstractSource;
use App\Domain\SecureCRUDManagement\CRUD\AbstractSecureCRUDService;
use App\Entity\EntityInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use App\Repository\Source\SourceRepository;
/**
* @author kevinfrantz
*/
final class SecureSourceReadService extends AbstractSecureCRUDService //implements SecureSourceReadServiceInterface
{
/**
* @todo It would be better to specify the type
*
* @var SourceRepository
*/
private $sourceRepository;
/**
* @param SourceInterface $source
*
* @return RightInterface
*/
private function getClonedRightWithModifiedSource(SourceInterface $source, RightInterface $requestedRight): RightInterface
{
$requestedRight = clone $requestedRight;
$requestedRight->setSource($source);
return $requestedRight;
}
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\AbstractSecureCRUDService::__construct()
*/
public function __construct(RequestStack $requestStack, Security $security, EntityManagerInterface $entityManager)
{
$this->sourceRepository = $entityManager->getRepository(AbstractSource::class);
parent::__construct($requestStack, $security, $entityManager);
}
/**
* @todo This will not work! Change interface to requested right!
*
* @param RightInterface $requestedRight
*
* @return EntityInterface
*/
public function read(RightInterface $requestedRight): EntityInterface
{
$source = $requestedRight->getSource();
$requestedRight = $this->getClonedRightWithModifiedSource($source, $requestedRight);
$secureSourceChecker = new SecureSourceChecker($source);
if ($secureSourceChecker->hasPermission($requestedRight)) {
return $source;
}
throw new SourceAccessDenied();
}
}

View File

@ -1,10 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD\Read;
/**
* @author kevinfrantz
*/
interface SecureSourceReadServiceInterface extends SecureReadServiceInterface
{
}

View File

@ -1,12 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\CRUD;
/**
* @todo Implement!
*
* @author kevinfrantz
*/
interface SecureCRUDServiceInterface
{
}

View File

@ -1,58 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\AbstractSecureCRUDService;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDServiceInterface;
/**
* @author kevinfrantz
*
* @todo Improve code performance
*/
final class SecureCRUDFactoryService extends AbstractSecureCRUDService implements SecureCRUDFactoryServiceInterface
{
/**
* @param string $crud
*
* @return string
*/
private function getCrud(string $crud): string
{
return ucfirst(strtolower($crud));
}
/**
* @param string $layer
* @param string $crud
*
* @return string
*/
private function getClassName(string $layer, string $crud): string
{
return 'Secure'.ucfirst(strtolower($layer)).$this->getCrud($crud);
}
/**
* @param string $layer
*
* @return string
*/
private function getCRUDNamespace(string $layer, string $crud): string
{
return 'App\\Domain\\SecureCRUDManagement\\CRUD\\'.$this->getCrud($crud).'\\'.$this->getClassName($layer, $crud).'Service';
}
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface::create()
*/
public function create(RightInterface $requestedRight): SecureCRUDServiceInterface
{
$namespace = $this->getCRUDNamespace($requestedRight->getLayer(), $requestedRight->getCrud());
return new $namespace($this->requestStack, $this->security, $this->entityManager);
}
}

View File

@ -1,17 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDServiceInterface;
/**
* @author kevinfrantz
*/
interface SecureCRUDFactoryServiceInterface
{
/**
* @return SecureCRUDServiceInterface
*/
public function create(RightInterface $requestedRight): SecureCRUDServiceInterface;
}

View File

@ -1,43 +0,0 @@
<?php
namespace App\Domain\ViewManagement;
use FOS\RestBundle\View\View;
use App\Domain\RequestManagement\RequestedUser;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryService;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface;
/**
* @author kevinfrantz
*/
class AbstractViewBuilder implements ViewBuilderInterface
{
/**
* @var View
*/
protected $view;
/**
* @var SecureCRUDFactoryServiceInterface
*/
protected $secureCrudFactoryService;
/**
* @param RequestedUser $requestedUserRight
* @param SecureCRUDFactoryService $secureCrudFactoryService
*/
public function __construct(RequestedUser $requestedUserRight, SecureCRUDFactoryService $secureCrudFactoryService)
{
$this->view = new View();
$this->requestedUserRight = $requestedUserRight;
$this->secureCrudFactoryService = $secureCrudFactoryService;
}
/**
* @return View
*/
public function getView(): View
{
$this->secureCrudFactoryService->create($requestedRight);
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace App\Domain\ViewManagement;
use FOS\RestBundle\View\View;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryService;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface;
use App\Domain\RequestManagement\User\RequestedUserInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
/**
* @author kevinfrantz
*/
class ViewBuilder implements ViewBuilderInterface
{
/**
* @var View
*/
protected $view;
/**
* @var SecureCRUDFactoryServiceInterface
*/
protected $secureCrudFactoryService;
/**
* @var RequestedEntityInterface
*/
protected $requestedEntity;
/**
* @var RequestedUserInterface
*/
protected $requestedUser;
/**
* @param RequestedUserInterface $requestedUserRight
* @param SecureCRUDFactoryService $secureCrudFactoryService
*/
public function __construct(RequestedUserInterface $requestedUserRight, SecureCRUDFactoryService $secureCrudFactoryService, RequestedEntityInterface $requestedEntity)
{
$this->view = new View();
$this->requestedUser = $requestedUserRight;
$this->secureCrudFactoryService = $secureCrudFactoryService;
$this->requestedEntity = $requestedEntity;
}
private function process()
{
$secureCrudService = $this->secureCrudFactoryService->create($this->requestedUser);
$entity = $secureCrudService->process($this->requestedEntity);
}
/**
* @return View
*/
public function getView(): View
{
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exception;
/**
* @author kevinfrantz
*/
class NotSecureException extends \Exception
{
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Exception;
/**
* @author kevinfrantz
*/
class NotValidByFormException extends \Exception
{
}

View File

@ -0,0 +1,6 @@
# Exceptions
This folder containes all Exceptions which the software throws.
## todo
- Remove exception duplicates at the end

View File

@ -10,7 +10,7 @@ use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
/**
* @author kevinfrantz
*/
class RequestAttributTest extends TestCase
class LayerRepositoryFactoryServiceAttributTest extends TestCase
{
/**
* @var LayerRepositoryFactoryServiceAttributInterface

View File

@ -0,0 +1,55 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\Factory;
use App\DBAL\Types\Meta\Right\LayerType;
use PHPUnit\Framework\TestCase;
use App\Domain\ActionManagement\ActionFactoryServiceInterface;
use App\Domain\ActionManagement\ActionFactoryService;
use App\Domain\ActionManagement\ActionServiceInterface;
use App\DBAL\Types\ActionType;
use App\Domain\ActionManagement\ActionInterface;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
/**
* @author kevinfrantz
*/
class ActionFactoryServiceTest extends TestCase
{
/**
* @var ActionFactoryServiceInterface
*/
private $actionFactoryService;
/**
* @var ActionServiceInterface
*/
private $actionService;
/**
* @var RequestedActionInterface
*/
private $requestedAction;
public function setUp(): void
{
$this->requestedEntity = $this->createMock(RequestedEntityInterface::class);
$this->requestedAction = $this->createMock(RequestedActionInterface::class);
$this->actionService = $this->createMock(ActionServiceInterface::class);
$this->actionService->method('getRequestedAction')->willReturn($this->requestedAction);
$this->actionFactoryService = new ActionFactoryService($this->actionService);
}
public function testCreate(): void
{
foreach (ActionType::getChoices() as $action) {
foreach (LayerType::getChoices() as $layer) {
$this->requestedAction->method('getLayer')->willReturn($layer);
$this->requestedAction->method('getActionType')->willReturn($action);
$action = $this->actionFactoryService->create();
$this->assertInstanceOf(ActionInterface::class, $action);
}
}
}
}

View File

@ -6,26 +6,71 @@ use PHPUnit\Framework\TestCase;
use App\Domain\ActionManagement\ActionService;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\SecureManagement\SecureRequestedRightCheckerInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Domain\FormManagement\EntityFormBuilderServiceInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use App\Domain\RepositoryManagement\LayerRepositoryFactoryServiceInterface;
use App\Domain\ActionManagement\ActionServiceInterface;
/**
* @author kevinfrantz
*/
class ActionServiceTest extends TestCase
{
/**
* @var RequestedActionInterface
*/
private $requestedAction;
/**
* @var SecureRequestedRightCheckerInterface
*/
private $secureRequestedRightChecker;
/**
* @var EntityFormBuilderServiceInterface
*/
private $entityFormBuilderService;
/**
* @var RequestStack
*/
private $requestStack;
/**
* @var LayerRepositoryFactoryServiceInterface
*/
private $layerRepositoryFactoryService;
/**
* @var ActionServiceInterface
*/
private $actionService;
/**
* @var EntityManagerInterface
*/
private $entityManager;
public function setUp(): void
{
$this->requestedAction = $this->createMock(RequestedActionInterface::class);
$this->secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
$this->entityFormBuilderService = $this->createMock(EntityFormBuilderServiceInterface::class);
$this->requestStack = $this->createMock(RequestStack::class);
$this->layerRepositoryFactoryService = $this->createMock(LayerRepositoryFactoryServiceInterface::class);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->actionService = new ActionService($this->requestedAction, $this->secureRequestedRightChecker, $this->requestStack, $this->layerRepositoryFactoryService, $this->entityFormBuilderService, $this->entityManager);
}
public function testIsRequestedActionSecure()
{
$requestedAction = $this->createMock(RequestedActionInterface::class);
$secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
$secureRequestedRightChecker->method('check')->willReturn(true);
$actionService = new ActionService($requestedAction, $secureRequestedRightChecker);
$this->assertTrue($actionService->isRequestedActionSecure());
$this->secureRequestedRightChecker->method('check')->willReturn(true);
$this->assertTrue($this->actionService->isRequestedActionSecure());
}
public function testRequestedActionGetter()
{
$requestedAction = $this->createMock(RequestedActionInterface::class);
$secureRequestedRightChecker = $this->createMock(SecureRequestedRightCheckerInterface::class);
$actionService = new ActionService($requestedAction, $secureRequestedRightChecker);
$this->assertInstanceOf(RequestedActionInterface::class, $actionService->getRequestedAction());
$this->assertInstanceOf(RequestedActionInterface::class, $this->actionService->getRequestedAction());
}
}

View File

@ -0,0 +1,72 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\CRUD\Read;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
use App\Domain\ActionManagement\Read\ReadAction;
use App\Domain\ActionManagement\ActionServiceInterface;
use App\Domain\ActionManagement\Read\ReadActionInterface;
use App\Exception\NotSecureException;
use App\Domain\RequestManagement\Action\RequestedActionInterface;
use App\Domain\RequestManagement\Entity\RequestedEntityInterface;
use App\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
*/
class ReadSourceActionTest extends TestCase
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var ActionServiceInterface
*/
private $actionService;
/**
* @var ReadActionInterface
*/
private $sourceReadAction;
/**
* @var RequestedEntityInterface
*/
private $requestedEntity;
/**
* @var SourceInterface
*/
private $entity;
public function setUp(): void
{
$this->entity = $this->createMock(SourceInterface::class);
$this->requestedEntity = $this->createMock(RequestedEntityInterface::class);
$this->requestedEntity->method('getEntity')->willReturn($this->entity);
$this->entityManager = $this->createMock(EntityManagerInterface::class);
$this->requestedAction = $this->createMock(RequestedActionInterface::class);
$this->requestedAction->method('getRequestedEntity')->willReturn($this->requestedEntity);
$this->actionService = $this->createMock(ActionServiceInterface::class);
$this->actionService->method('getEntityManager')->willReturn($this->entityManager);
$this->actionService->method('getRequestedAction')->willReturn($this->requestedAction);
$this->sourceReadAction = new ReadAction($this->actionService);
}
public function testNotSecureException(): void
{
$this->actionService->method('isRequestedActionSecure')->willReturn(false);
$this->expectException(NotSecureException::class);
$this->sourceReadAction->execute();
}
public function testGranted(): void
{
$this->actionService->method('isRequestedActionSecure')->willReturn(true);
$result = $this->sourceReadAction->execute();
$this->assertEquals($this->entity, $result);
}
}

View File

@ -1,75 +0,0 @@
<?php
namespace Unit\Domain\ResponseManagement;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\Meta\RightInterface;
use App\Entity\Meta\Right;
use FOS\RestBundle\View\ViewHandlerInterface;
use App\Entity\Source\PureSource;
use App\DBAL\Types\SystemSlugType;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Domain\ResponseManagement\SourceRESTResponseManager;
use App\Exception\AllreadyDefinedException;
/**
* @author kevinfrantz
*
* @todo Implement more tests!
*/
class SourceRESTReponseManagerTest extends KernelTestCase
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var RightInterface
*/
private $requestedRight;
/**
* @var ViewHandlerInterface
*/
private $viewHandler;
private function setRequestedRight(): void
{
$this->requestedRight = new Right();
}
private function setEntityManager(): void
{
$this->entityManager = self::$container->get('doctrine.orm.default_entity_manager');
}
private function setViewHandler(): void
{
$this->viewHandler = $this->createMock(ViewHandlerInterface::class);
}
public function setUp(): void
{
self::bootKernel();
$this->setEntityManager();
$this->setRequestedRight();
$this->setViewHandler();
}
public function testAllreadyDefinedException(): void
{
$requestedSource = new PureSource();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$requestedRight = new Right();
$requestedRight->setSource($requestedSource);
$requestedRight->setReciever(new PureSource());
$requestedRight->setLayer(LayerType::SOURCE);
$requestedRight->setCrud(CRUDType::READ);
$this->expectException(AllreadyDefinedException::class);
$sourceResponseManager = new SourceRESTResponseManager(null, $this->entityManager, $requestedRight, $this->viewHandler);
$sourceResponseManager->getResponse();
}
}

View File

@ -1,75 +0,0 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\CRUD\Read;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Doctrine\Common\Persistence\ObjectRepository;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use App\Entity\Source\Primitive\Text\TextSource;
use App\DBAL\Types\SystemSlugType;
use App\Entity\Meta\Right;
use App\DBAL\Types\Meta\Right\LayerType;
use App\DBAL\Types\Meta\Right\CRUDType;
use App\Entity\Source\Complex\UserSource;
use App\Entity\Source\Primitive\Text\TextSourceInterface;
use Doctrine\ORM\EntityManagerInterface;
use App\Domain\SecureCRUDManagement\CRUD\Read\SecureSourceReadService;
use Symfony\Component\Security\Core\Security;
/**
* @author kevinfrantz
*
* @todo Implement more tests
*/
class SecureSourceReadServiceTest extends KernelTestCase
{
/**
* @var ObjectRepository
*/
private $sourceRepository;
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var SecureSourceReadService
*/
private $secureSourceReadService;
public function setUp(): void
{
self::bootKernel();
$requestStack = self::$container->get('request_stack');
$security = new Security(self::$kernel->getContainer());
$entityManager = self::$container->get('doctrine.orm.default_entity_manager');
$this->secureSourceReadService = new SecureSourceReadService($requestStack, $security, $entityManager);
}
public function testAccessDeniedException(): void
{
$requestedSource = new TextSource();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$requestedRight = new Right();
$requestedRight->setSource($requestedSource);
$requestedRight->setLayer(LayerType::SOURCE);
$requestedRight->setCrud(CRUDType::READ);
$requestedRight->setReciever(new UserSource());
$this->expectException(AccessDeniedHttpException::class);
$this->secureSourceReadService->read($requestedRight);
}
public function testGranted(): void
{
$requestedSource = new TextSource();
$requestedSource->setSlug(SystemSlugType::IMPRINT);
$requestedRight = new Right();
$requestedRight->setSource($requestedSource);
$requestedRight->setLayer(LayerType::SOURCE);
$requestedRight->setCrud(CRUDType::READ);
$requestedRight->setReciever($this->sourceRepository->findOneBySlug(SystemSlugType::GUEST_USER));
$textSourceResponse = $this->secureSourceReadService->read($requestedRight);
$this->assertInstanceOf(TextSourceInterface::class, $textSourceResponse);
}
}

View File

@ -1,58 +0,0 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\Factory;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Security\Core\Security;
use App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryService;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Entity\Meta\Right;
use App\Domain\SecureCRUDManagement\CRUD\SecureCRUDServiceInterface;
use App\DBAL\Types\Meta\Right\CRUDType;
/**
* @author kevinfrantz
*/
class SecureCRUDFactoryServiceTest extends KernelTestCase
{
const EXCLUDED_TYPES = [
CRUDType::CREATE => [
LayerType::LAW,
],
CRUDType::DELETE => [
LayerType::LAW,
],
CRUDType::READ => [],
CRUDType::UPDATE => [],
];
/**
* @var SecureCRUDFactoryServiceInterface
*/
private $secureCRUDFactoryService;
public function setUp(): void
{
self::bootKernel();
$requestStack = self::$container->get('request_stack');
$security = new Security(self::$kernel->getContainer());
$entityManager = self::$container->get('doctrine.orm.default_entity_manager');
$this->secureCRUDFactoryService = new SecureCRUDFactoryService($requestStack, $security, $entityManager);
}
public function testCreate(): void
{
foreach (CRUDType::getChoices() as $crud) {
foreach (LayerType::getChoices() as $layer) {
if (!in_array($layer, self::EXCLUDED_TYPES[$crud])) {
$requestedRight = new Right();
$requestedRight->setLayer($layer);
$requestedRight->setCrud($crud);
$secureCreator = $this->secureCRUDFactoryService->create($requestedRight);
$this->assertInstanceOf(SecureCRUDServiceInterface::class, $secureCreator);
}
}
}
}
}