From e7d7b6056a3d8b4157ec4389da734deef58c0459 Mon Sep 17 00:00:00 2001 From: Nattfarinn Date: Thu, 8 Mar 2018 14:36:39 +0100 Subject: [PATCH] fix: Move Content on the Fly to dedicated controller --- src/bundle/Controller/ContentController.php | 126 -------------- .../Controller/ContentOnTheFlyController.php | 154 ++++++++++++++++++ src/bundle/Resources/config/routing.yml | 8 +- 3 files changed, 158 insertions(+), 130 deletions(-) create mode 100644 src/bundle/Controller/ContentOnTheFlyController.php diff --git a/src/bundle/Controller/ContentController.php b/src/bundle/Controller/ContentController.php index 52658be0cb..6ae6044654 100644 --- a/src/bundle/Controller/ContentController.php +++ b/src/bundle/Controller/ContentController.php @@ -8,12 +8,7 @@ use eZ\Publish\API\Repository\ContentService; use eZ\Publish\API\Repository\Exceptions as ApiException; -use eZ\Publish\API\Repository\LanguageService; -use eZ\Publish\API\Repository\LocationService; use eZ\Publish\API\Repository\Values\Content\Content; -use eZ\Publish\API\Repository\Values\Content\Language; -use eZ\Publish\API\Repository\Values\Content\Location; -use eZ\Publish\API\Repository\Values\ContentType\ContentType; use eZ\Publish\Core\Base\Exceptions\UnauthorizedException; use EzSystems\EzPlatformAdminUi\Exception\InvalidArgumentException as AdminInvalidArgumentException; use EzSystems\EzPlatformAdminUi\Form\Data\Content\Draft\ContentCreateData; @@ -23,13 +18,6 @@ use EzSystems\EzPlatformAdminUi\Form\Factory\FormFactory; use EzSystems\EzPlatformAdminUi\Form\SubmitHandler; use EzSystems\EzPlatformAdminUi\Notification\NotificationHandlerInterface; -use EzSystems\EzPlatformAdminUi\RepositoryForms\Dispatcher\ContentOnTheFlyDispatcher; -use EzSystems\EzPlatformAdminUi\RepositoryForms\View\ContentCreateOnTheFlyView; -use EzSystems\RepositoryForms\Content\View\ContentCreateView; -use EzSystems\RepositoryForms\Data\Mapper\ContentCreateMapper; -use EzSystems\RepositoryForms\Form\Type\Content\ContentEditType; -use Symfony\Component\Form\Form; -use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; @@ -47,9 +35,6 @@ class ContentController extends Controller /** @var ContentService */ private $contentService; - /** @var LanguageService */ - private $languageService; - /** @var FormFactory */ private $formFactory; @@ -65,12 +50,6 @@ class ContentController extends Controller /** @var string */ private $defaultSiteaccess; - /** @var LocationService */ - private $locationService; - - /** @var ContentOnTheFlyDispatcher */ - private $contentActionDispatcher; - /** * @param NotificationHandlerInterface $notificationHandler * @param ContentService $contentService @@ -83,24 +62,18 @@ class ContentController extends Controller public function __construct( NotificationHandlerInterface $notificationHandler, ContentService $contentService, - LanguageService $languageService, - LocationService $locationService, FormFactory $formFactory, SubmitHandler $submitHandler, TranslatorInterface $translator, ContentMainLocationUpdateMapper $contentMetadataUpdateMapper, - ContentOnTheFlyDispatcher $contentActionDispatcher, string $defaultSiteaccess ) { $this->notificationHandler = $notificationHandler; $this->contentService = $contentService; - $this->locationService = $locationService; - $this->languageService = $languageService; $this->formFactory = $formFactory; $this->submitHandler = $submitHandler; $this->translator = $translator; $this->contentMainLocationUpdateMapper = $contentMetadataUpdateMapper; - $this->contentActionDispatcher = $contentActionDispatcher; $this->defaultSiteaccess = $defaultSiteaccess; } @@ -142,105 +115,6 @@ public function createAction(Request $request): Response return $this->redirect($this->generateUrl('ezplatform.dashboard')); } - /** - * @param Request $request - * @param string $languageCode - * @param ContentType $contentType - * @param Location $parentLocation - * - * @return ContentCreateView|Response - * - * @throws ApiException\NotFoundException - */ - public function createOnTheFlyAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation) - { - $language = $this->languageService->loadLanguage($languageCode); - - $data = (new ContentCreateMapper())->mapToFormData($contentType, [ - 'mainLanguageCode' => $language->languageCode, - 'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocation->id), - ]); - - $form = $this->createForm(ContentEditType::class, $data, [ - 'languageCode' => $language->languageCode, - 'mainLanguageCode' => $language->languageCode, - 'drafts_enabled' => true, - ]); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $this->contentActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName()); - if ($response = $this->contentActionDispatcher->getResponse()) { - return $response; - } - } - - return new ContentCreateOnTheFlyView(null, [ - 'form' => $form->createView(), - 'language' => $language, - 'contentType' => $contentType, - 'parentLocation' => $parentLocation, - ]); - } - - /** - * @param Request $request - * @param string $languageCode - * @param ContentType $contentType - * @param Location $parentLocation - * - * @return JsonResponse - * - * @throws ApiException\InvalidArgumentException - * @throws ApiException\BadStateException - */ - public function hasOnTheFlyCreateAccessAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation) - { - $response = new JsonResponse(); - - try { - $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $languageCode); - $locationCreateStruct = $this->locationService->newLocationCreateStruct($parentLocation->id); - - $permissionResolver = $this->container->get('ezpublish.api.repository')->getPermissionResolver(); - - if (!$permissionResolver->canUser('content', 'create', $contentCreateStruct, [$locationCreateStruct])) { - throw new UnauthorizedException( - 'content', - 'create', - [ - 'contentTypeIdentifier' => $contentType->identifier, - 'parentLocationId' => $locationCreateStruct->parentLocationId, - 'languageCode' => $languageCode, - ] - ); - } - - if (!$permissionResolver->canUser('content', 'publish', $contentCreateStruct, [$locationCreateStruct])) { - throw new UnauthorizedException( - 'content', - 'publish', - [ - 'contentTypeIdentifier' => $contentType->identifier, - 'parentLocationId' => $locationCreateStruct->parentLocationId, - 'languageCode' => $languageCode, - ] - ); - } - - $response->setData([ - 'access' => true, - ]); - } catch (ApiException\UnauthorizedException $exception) { - $response->setData([ - 'access' => false, - 'message' => $exception->getMessage(), - ]); - } - - return $response; - } - /** * @param Request $request * diff --git a/src/bundle/Controller/ContentOnTheFlyController.php b/src/bundle/Controller/ContentOnTheFlyController.php new file mode 100644 index 0000000000..bb1f39d6c4 --- /dev/null +++ b/src/bundle/Controller/ContentOnTheFlyController.php @@ -0,0 +1,154 @@ +contentService = $contentService; + $this->locationService = $locationService; + $this->languageService = $languageService; + $this->contentActionDispatcher = $contentActionDispatcher; + } + + /** + * @param Request $request + * @param string $languageCode + * @param ContentType $contentType + * @param Location $parentLocation + * + * @return ContentCreateOnTheFlyView|Response + * + * @throws ApiException\NotFoundException + * @throws \eZ\Publish\Core\Base\Exceptions\InvalidArgumentType + */ + public function createContentAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation) + { + $language = $this->languageService->loadLanguage($languageCode); + + $data = (new ContentCreateMapper())->mapToFormData($contentType, [ + 'mainLanguageCode' => $language->languageCode, + 'parentLocation' => $this->locationService->newLocationCreateStruct($parentLocation->id), + ]); + + $form = $this->createForm(ContentEditType::class, $data, [ + 'languageCode' => $language->languageCode, + 'mainLanguageCode' => $language->languageCode, + 'drafts_enabled' => true, + ]); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->contentActionDispatcher->dispatchFormAction($form, $data, $form->getClickedButton()->getName()); + if ($response = $this->contentActionDispatcher->getResponse()) { + return $response; + } + } + + return new ContentCreateOnTheFlyView(null, [ + 'form' => $form->createView(), + 'language' => $language, + 'contentType' => $contentType, + 'parentLocation' => $parentLocation, + ]); + } + + /** + * @param Request $request + * @param string $languageCode + * @param ContentType $contentType + * @param Location $parentLocation + * + * @return JsonResponse + */ + public function hasCreateAccessAction(Request $request, string $languageCode, ContentType $contentType, Location $parentLocation) + { + $response = new JsonResponse(); + + try { + $contentCreateStruct = $this->contentService->newContentCreateStruct($contentType, $languageCode); + $locationCreateStruct = $this->locationService->newLocationCreateStruct($parentLocation->id); + + $permissionResolver = $this->container->get('ezpublish.api.repository')->getPermissionResolver(); + + if (!$permissionResolver->canUser('content', 'create', $contentCreateStruct, [$locationCreateStruct])) { + throw new UnauthorizedException( + 'content', + 'create', + [ + 'contentTypeIdentifier' => $contentType->identifier, + 'parentLocationId' => $locationCreateStruct->parentLocationId, + 'languageCode' => $languageCode, + ] + ); + } + + if (!$permissionResolver->canUser('content', 'publish', $contentCreateStruct, [$locationCreateStruct])) { + throw new UnauthorizedException( + 'content', + 'publish', + [ + 'contentTypeIdentifier' => $contentType->identifier, + 'parentLocationId' => $locationCreateStruct->parentLocationId, + 'languageCode' => $languageCode, + ] + ); + } + + $response->setData([ + 'access' => true, + ]); + } catch (ApiException\UnauthorizedException $exception) { + $response->setData([ + 'access' => false, + 'message' => $exception->getMessage(), + ]); + } + + return $response; + } +} + diff --git a/src/bundle/Resources/config/routing.yml b/src/bundle/Resources/config/routing.yml index 8ecc61fd44..44f0fd3c82 100644 --- a/src/bundle/Resources/config/routing.yml +++ b/src/bundle/Resources/config/routing.yml @@ -549,18 +549,18 @@ ezplatform.user_profile.change_password: # Content on the Fly # -ezplatform.content.create.on_the_fly: +ezplatform.content_on_the_fly.create: path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId} methods: ['GET', 'POST'] defaults: - _controller: 'EzPlatformAdminUiBundle:Content:createOnTheFly' + _controller: 'EzPlatformAdminUiBundle:ContentOnTheFly:createContent' options: expose: true -ezplatform.content.create.on_the_fly.has_access: +ezplatform.content_on_the_fly.has_access: path: /content/create/onthefly/{contentTypeIdentifier}/{languageCode}/{locationId}/hasaccess methods: ['GET'] defaults: - _controller: 'EzPlatformAdminUiBundle:Content:hasOnTheFlyCreateAccess' + _controller: 'EzPlatformAdminUiBundle::ContentOnTheFly:hasCreateAccess' options: expose: true