Optimized SecureCRUDManagement Draft

This commit is contained in:
Kevin Frantz 2019-01-07 21:19:20 +01:00
parent f2828e0d5e
commit 6b29c5a577
21 changed files with 271 additions and 159 deletions

View File

@ -5,7 +5,6 @@ namespace App\Controller\API;
use App\Controller\AbstractController; use App\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use App\Domain\SecureCRUDManagement\SecureCRUDFactoryService;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -21,7 +20,7 @@ abstract class AbstractAPIController extends AbstractController
* *
* @return Response * @return Response
*/ */
abstract public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response; // abstract public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response;
/** /**
* @param Request $request HTTP Method GET * @param Request $request HTTP Method GET

View File

@ -5,7 +5,6 @@ namespace App\Controller\API\Meta;
use App\Controller\API\AbstractAPIController; use App\Controller\API\AbstractAPIController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use App\Domain\SecureCRUDManagement\SecureCRUDFactoryService;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -18,9 +17,9 @@ class HeredityApiController extends AbstractAPIController
{ {
} }
public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response // public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response
{ // {
} // }
public function update(Request $request, $identifier): Response public function update(Request $request, $identifier): Response
{ {

View File

@ -5,7 +5,6 @@ namespace App\Controller\API\Meta;
use App\Controller\API\AbstractAPIController; use App\Controller\API\AbstractAPIController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use App\Domain\SecureCRUDManagement\SecureCRUDFactoryService;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -18,9 +17,9 @@ class LawApiController extends AbstractAPIController
{ {
} }
public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response // public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response
{ // {
} // }
public function update(Request $request, $identifier): Response public function update(Request $request, $identifier): Response
{ {

View File

@ -5,7 +5,6 @@ namespace App\Controller\API\Meta;
use App\Controller\API\AbstractAPIController; use App\Controller\API\AbstractAPIController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use App\Domain\SecureCRUDManagement\SecureCRUDFactoryService;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -18,9 +17,9 @@ class MemberApiController extends AbstractAPIController
{ {
} }
public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response // public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response
{ // {
} // }
public function update(Request $request, $identifier): Response public function update(Request $request, $identifier): Response
{ {

View File

@ -5,7 +5,6 @@ namespace App\Controller\API\Meta;
use App\Controller\API\AbstractAPIController; use App\Controller\API\AbstractAPIController;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use App\Domain\SecureCRUDManagement\SecureCRUDFactoryService;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -18,9 +17,9 @@ class RightApiController extends AbstractAPIController
{ {
} }
public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response // public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response
{ // {
} // }
public function update(Request $request, $identifier): Response public function update(Request $request, $identifier): Response
{ {

View File

@ -7,8 +7,6 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use App\Controller\API\AbstractAPIController; use App\Controller\API\AbstractAPIController;
use App\Entity\Source\PureSource; use App\Entity\Source\PureSource;
use App\Domain\SecureCRUDManagement\SecureCRUDFactoryService;
use App\Domain\SecureCRUDManagement\Create\SecureSourceCreatorInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -30,51 +28,51 @@ class SourceApiController extends AbstractAPIController
{ {
} }
/** // /**
* @Route("/{_locale}/api/source.{_format}", // * @Route("/{_locale}/api/source.{_format}",
* defaults={"_format"="json"} , // * defaults={"_format"="json"} ,
* methods={"POST","GET"} // * methods={"POST","GET"}
* ) // * )
* {@inheritdoc} // * {@inheritdoc}
* // *
* @see \App\Controller\API\AbstractAPIController::create() // * @see \App\Controller\API\AbstractAPIController::create()
*/ // */
public function create(Request $request, SecureCRUDFactoryService $crudFactory): Response // 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 = new Response();
// $response->setContent('GET Request!'); // 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; // return $response;
// $requestedSource = new PureSource(); // // $response = new Response();
// $requestedSource->setSlug(SystemSlugType::IMPRINT); // // $response->setContent('GET Request!');
// $requestedRight = new Right();
// $requestedRight->setSource($requestedSource);
// $requestedRight->setLayer(LayerType::SOURCE);
// $requestedRight->setType(CRUDType::READ);
// $sourceResponseManager = new SourceRESTResponseManager($this->getUser(), $entityManager, $requestedRight, $this->getViewHandler());
// return $sourceResponseManager->getResponse(); // // return $response;
}
// // $requestedSource = new PureSource();
// // $requestedSource->setSlug(SystemSlugType::IMPRINT);
// // $requestedRight = new Right();
// // $requestedRight->setSource($requestedSource);
// // $requestedRight->setLayer(LayerType::SOURCE);
// // $requestedRight->setType(CRUDType::READ);
// // $sourceResponseManager = new SourceRESTResponseManager($this->getUser(), $entityManager, $requestedRight, $this->getViewHandler());
// // return $sourceResponseManager->getResponse();
// }
/** /**
* @Route("/{_locale}/api/source/{identifier}.{_format}", * @Route("/{_locale}/api/source/{identifier}.{_format}",

View File

@ -1,11 +1,9 @@
<?php <?php
namespace App\Domain\SecureCRUDManagement; namespace App\Domain\SecureCRUDManagement\CRUD;
/** /**
* @author kevinfrantz * @author kevinfrantz
*
* @todo Implement!
*/ */
abstract class AbstractSecureCRUD implements SecureCRUDInterface abstract class AbstractSecureCRUD implements SecureCRUDInterface
{ {

View File

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

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Domain\SecureCRUDManagement\Create; namespace App\Domain\SecureCRUDManagement\CRUD\Create;
use App\Domain\SecureCRUDManagement\SecureCRUDInterface; use App\Domain\SecureCRUDManagement\SecureCRUDInterface;
use App\Entity\EntityInterface; use App\Entity\EntityInterface;

View File

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

View File

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

View File

@ -1,6 +1,6 @@
<?php <?php
namespace App\Domain\SecureCRUDManagement; namespace App\Domain\SecureCRUDManagement\CRUD;
/** /**
* @todo Implement! * @todo Implement!

View File

@ -1,12 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\Create;
/**
* @author kevinfrantz
*
* @todo Implement!
*/
abstract class AbstractSecureCreator implements SecureCreatorInterface
{
}

View File

@ -1,28 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement\Create;
use App\Entity\EntityInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use App\Entity\Source\Primitive\Text\TextSource;
/**
* @author kevinfrantz
*
* @todo Implement!
*/
class SecureSourceCreator extends AbstractSecureCreator implements SecureSourceCreatorInterface
{
public function __construct(Request $request, Security $security)
{
}
public function create(): EntityInterface
{
$source = new TextSource();
$source->setText('Hello World!');
return $source;
}
}

View File

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

View File

@ -0,0 +1,63 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @author kevinfrantz
*
* @todo Implement!
* @todo substitute through child classes!
*/
abstract class AbstractSecureCRUDFactoryService implements SecureCRUDFactoryServiceInterface
{
/**
* @var Request
*/
protected $request;
/**
* @var Security
*/
protected $security;
/**
* @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
*/
protected function getCRUDNamespace(string $layer, string $crud): string
{
return 'App\\Domain\\SecureCRUDManagement\\CRUD\\'.$this->getCrud($crud).'\\'.$this->getClassName($layer, $crud);
}
public function __construct(RequestStack $requestStack, Security $security)
{
$this->request = $requestStack->getCurrentRequest();
$this->security = $security;
}
}

View File

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

View File

@ -0,0 +1,30 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
use App\Entity\Meta\RightInterface;
use App\DBAL\Types\Meta\Right\LayerType;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureSourceCreator;
/**
* @author kevinfrantz
*
* @todo Implement!
*/
final class SecureCreatorFactoryService extends AbstractSecureCRUDFactoryService
{
/**
* {@inheritdoc}
*
* @see \App\Domain\SecureCRUDManagement\Factory\SecureCRUDFactoryServiceInterface::create()
*/
public function create(RightInterface $requestedRight): SecureCreatorInterface
{
switch ($requestedRight->getLayer()) {
case LayerType::SOURCE:
return new SecureSourceCreator($this->request, $this->security);
case LayerType::MEMBER:
}
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Domain\SecureCRUDManagement\Factory;
/**
* @author kevinfrantz
*/
interface SecureCreatorFactoryServiceInterface extends SecureCRUDFactoryServiceInterface
{
}

View File

@ -1,42 +0,0 @@
<?php
namespace App\Domain\SecureCRUDManagement;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\Create\SecureSourceCreator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* @author kevinfrantz
*
* @todo Implement!
* @todo substitute through child classes!
*/
class SecureCRUDFactoryService
{
/**
* @var Request
*/
private $request;
/**
* @var Security
*/
private $security;
public function __construct(RequestStack $requestStack, Security $security)
{
$this->request = $requestStack->getCurrentRequest();
$this->security = $security;
}
/**
* @param RightInterface $requestedRight
*/
public function create(?RightInterface $requestedRight = null)
{
return new SecureSourceCreator($this->request, $this->security);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace tests\Unit\Domain\SecureCRUDManagement\Factory;
use PHPUnit\Framework\TestCase;
use App\Domain\SecureCRUDManagement\Factory\AbstractSecureCRUDFactoryService;
use App\Entity\Meta\RightInterface;
use App\Domain\SecureCRUDManagement\CRUD\Create\SecureCreatorInterface;
/**
* @author kevinfrantz
*/
class AbstractSecureCRUDFactoryServiceTest extends TestCase
{
public function testGetCRUDNamespace(): void
{
$abstractSecureCRUDFactoryService = new class() extends AbstractSecureCRUDFactoryService {
public function __construct()
{
}
public function publicGetCRUDNamespace(string $layer, string $crud): string
{
return $this->getCRUDNamespace($layer, $crud);
}
public function create(RightInterface $requestedRight): SecureCreatorInterface
{
}
};
$result = $abstractSecureCRUDFactoryService->publicGetCRUDNamespace('Layer', 'Crud');
$this->assertEquals('App\\Domain\\SecureCRUDManagement\\CRUD\\Crud\\SecureLayerCrud', $result);
}
}