Skip to content
This repository has been archived by the owner on May 3, 2022. It is now read-only.

EZP-24943: Handle publishing of users #393

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions Resources/config/services.yml
Expand Up @@ -191,8 +191,17 @@ services:
class: %ezsystems.platformui.controller.exception.class%
parent: twig.controller.exception

ezsystems.platformui.rest.content_user_gateway:
class: EzSystems\PlatformUIBundle\Rest\ContentUserGateway
ezsystems.platformui.rest.content_publish_user_gateway:
class: EzSystems\PlatformUIBundle\Rest\ContentPublishUserGateway
arguments:
- @ezpublish_rest.input.dispatcher
- @ezpublish_rest.output.visitor.dispatcher
- @ezpublish.api.repository
tags:
- {name: kernel.event_subscriber}

ezsystems.platformui.rest.content_create_user_gateway:
class: EzSystems\PlatformUIBundle\Rest\ContentCreateUserGateway
arguments:
- @ezpublish_rest.input.dispatcher
- @ezpublish_rest.output.visitor.dispatcher
Expand Down
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\HttpKernel\KernelEvents;
use eZ\Publish\Core\FieldType\User\Value as UserFieldValue;

class ContentUserGateway implements EventSubscriberInterface
class ContentCreateUserGateway implements EventSubscriberInterface
{
/** @var \eZ\Publish\Core\REST\Common\Input\Dispatcher */
private $restInputDispatcher;
Expand Down
87 changes: 87 additions & 0 deletions Rest/ContentPublishUserGateway.php
@@ -0,0 +1,87 @@
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformUIBundle\Rest;

use eZ\Publish\API\Repository\Repository;
use eZ\Publish\Core\REST\Common\Input\Dispatcher;
use eZ\Publish\Core\REST\Server\Values\NoContent;
use eZ\Publish\Core\REST\Server\View\AcceptHeaderVisitorDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ContentPublishUserGateway implements EventSubscriberInterface
{
/** @var \eZ\Publish\Core\REST\Common\Input\Dispatcher */
private $restInputDispatcher;

/** @var \eZ\Publish\API\Repository\Repository */
private $repository;

/** @var \eZ\Publish\Core\REST\Server\View\AcceptHeaderVisitorDispatcher */
private $viewDispatcher;

public function __construct(
Dispatcher $restInputDispatcher,
AcceptHeaderVisitorDispatcher $viewDispatcher,
Repository $repository,
array $settings = []
) {
$this->restInputDispatcher = $restInputDispatcher;
$this->repository = $repository;
$this->viewDispatcher = $viewDispatcher;
$this->settings = $settings + ['user-content-type-id' => 4];
}

public static function getSubscribedEvents()
{
return [KernelEvents::REQUEST => ['onKernelRequest', -1000]];
}

public function onKernelRequest(GetResponseEvent $event)
{
if (!$this->shouldRun($request = $event->getRequest())) {
return;
}

$event->setResponse($this->viewDispatcher->dispatch($event->getRequest(), new NoContent()));
}

private function shouldRun(Request $request)
{
if (!$request->attributes->get('is_rest_request')) {
return false;
}

if ($request->attributes->get('_route') !== 'ezpublish_rest_publishVersion') {
return false;
}

$method = $request->getMethod();
$methodOverride = strtolower($request->headers->get('x-http-method-override'));
if (strtolower($method) !== 'publish' || ($method === 'post' && $methodOverride !== 'publish')) {
return false;
}

return ($this->loadContent($request)->contentInfo->contentTypeId === $this->settings['user-content-type-id']);
}

public function loadContent(Request $request)
{
static $content = null;

if ($content === null) {
$routeParams = $request->attributes->get('_route_params');
$content = $this->repository->getContentService()->loadContent(
$routeParams['contentId'],
null,
$routeParams['versionNumber']
);
}

return $content;
}
}