Skip to content

Commit

Permalink
Merge pull request #892 from mikadamczyk/EZEE-2533
Browse files Browse the repository at this point in the history
EZEE-2533: Error HTTP 400 trying to preview Form during creation
  • Loading branch information
Łukasz Serwatka committed Mar 19, 2019
2 parents c538e51 + 7fa7560 commit 71ead62
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 35 deletions.
6 changes: 5 additions & 1 deletion src/bundle/Resources/config/services/siteaccess.yml
Expand Up @@ -5,5 +5,9 @@ services:
public: false

EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolverInterface: '@EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolver'
EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolver: ~
EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessResolver:
arguments:
$siteaccessPreviewVoters: !tagged ezplatform.admin_ui.siteaccess_preview_voter
EzSystems\EzPlatformAdminUi\Siteaccess\NonAdminSiteaccessResolver: ~
EzSystems\EzPlatformAdminUi\Siteaccess\AdminSiteaccessPreviewVoter:
tags: ['ezplatform.admin_ui.siteaccess_preview_voter']
67 changes: 67 additions & 0 deletions src/lib/Siteaccess/AbstractSiteaccessPreviewVoter.php
@@ -0,0 +1,67 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Siteaccess;

use eZ\Publish\Core\MVC\ConfigResolverInterface;

abstract class AbstractSiteaccessPreviewVoter implements SiteaccessPreviewVoterInterface
{
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
protected $configResolver;

/**
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
*/
public function __construct(
ConfigResolverInterface $configResolver
) {
$this->configResolver = $configResolver;
}

/**
* {@inheritdoc}
*/
public function vote(SiteaccessPreviewVoterContext $context): bool
{
$siteaccess = $context->getSiteaccess();
$location = $context->getLocation();
$languageCode = $context->getLanguageCode();
$contentLanguages = $context->getVersionInfo()->languageCodes;

if (empty(array_intersect($this->getRootLocationIds($siteaccess), $location->path))) {
return false;
}

$siteaccessLanguages = $this->configResolver->getParameter(
'languages',
null,
$siteaccess
);
if (!in_array($languageCode, $siteaccessLanguages)) {
return false;
}

$primarySiteaccessLanguage = reset($siteaccessLanguages);
if (
$languageCode !== $primarySiteaccessLanguage
&& in_array($primarySiteaccessLanguage, $contentLanguages)
) {
return false;
}

return true;
}

/**
* @param string $siteaccess
*
* @return int[]
*/
abstract protected function getRootLocationIds(string $siteaccess): array;
}
37 changes: 37 additions & 0 deletions src/lib/Siteaccess/AdminSiteaccessPreviewVoter.php
@@ -0,0 +1,37 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Siteaccess;

class AdminSiteaccessPreviewVoter extends AbstractSiteaccessPreviewVoter
{
/**
* {@inheritdoc}
*/
public function getRootLocationIds(string $siteaccess): array
{
$locationIds = [];
$locationIds[] = $this->configResolver->getParameter(
'location_ids.content_structure',
null,
$siteaccess
);
$locationIds[] = $this->configResolver->getParameter(
'location_ids.media',
null,
$siteaccess
);
$locationIds[] = $this->configResolver->getParameter(
'location_ids.users',
null,
$siteaccess
);

return $locationIds;
}
}
71 changes: 71 additions & 0 deletions src/lib/Siteaccess/SiteaccessPreviewVoterContext.php
@@ -0,0 +1,71 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Siteaccess;

use eZ\Publish\API\Repository\Values\Content\Location;
use eZ\Publish\API\Repository\Values\Content\VersionInfo;

final class SiteaccessPreviewVoterContext
{
/** @var \eZ\Publish\API\Repository\Values\Content\Location */
private $location;

/** @var \eZ\Publish\API\Repository\Values\Content\VersionInfo */
private $versionInfo;

/** @var string */
private $siteaccess;

/** @var string */
private $languageCode;

public function __construct(
Location $location,
VersionInfo $versionInfo,
string $siteaccess,
string $languageCode
) {
$this->location = $location;
$this->versionInfo = $versionInfo;
$this->siteaccess = $siteaccess;
$this->languageCode = $languageCode;
}

/**
* @return \eZ\Publish\API\Repository\Values\Content\Location
*/
public function getLocation(): Location
{
return $this->location;
}

/**
* @return string
*/
public function getSiteaccess(): string
{
return $this->siteaccess;
}

/**
* @return string
*/
public function getLanguageCode(): string
{
return $this->languageCode;
}

/**
* @return \eZ\Publish\API\Repository\Values\Content\VersionInfo
*/
public function getVersionInfo(): VersionInfo
{
return $this->versionInfo;
}
}
21 changes: 21 additions & 0 deletions src/lib/Siteaccess/SiteaccessPreviewVoterInterface.php
@@ -0,0 +1,21 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace EzSystems\EzPlatformAdminUi\Siteaccess;

interface SiteaccessPreviewVoterInterface
{
/**
* Votes whether the Content item can be previewed in given siteaccess.
*
* @param \EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessPreviewVoterContext $context
*
* @return bool
*/
public function vote(SiteaccessPreviewVoterContext $context): bool;
}
65 changes: 31 additions & 34 deletions src/lib/Siteaccess/SiteaccessResolver.php
Expand Up @@ -14,61 +14,58 @@

class SiteaccessResolver implements SiteaccessResolverInterface
{
/** @var ConfigResolverInterface */
/** @var \eZ\Publish\Core\MVC\ConfigResolverInterface */
private $configResolver;

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

/** @var \EzSystems\EzPlatformAdminUi\Siteaccess\SiteaccessPreviewVoterInterface[] */
private $siteaccessPreviewVoters;

/**
* @param ConfigResolverInterface $configResolver
* @param ContentService $contentService
* @param \eZ\Publish\Core\MVC\ConfigResolverInterface $configResolver
* @param \eZ\Publish\API\Repository\ContentService $contentService
* @param iterable $siteaccessPreviewVoters
*/
public function __construct(ConfigResolverInterface $configResolver, ContentService $contentService)
{
public function __construct(
ConfigResolverInterface $configResolver,
ContentService $contentService,
iterable $siteaccessPreviewVoters
) {
$this->configResolver = $configResolver;
$this->contentService = $contentService;
$this->siteaccessPreviewVoters = $siteaccessPreviewVoters;
}

/**
* @param \eZ\Publish\API\Repository\Values\Content\Location $location
* @param int|null $versionNo
* @param string|null $languageCode
*
* @return array
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
*/
public function getSiteaccessesForLocation(
Location $location,
int $versionNo = null,
string $languageCode = null
): array {
$contentInfo = $location->getContentInfo();
$languageCode = $languageCode ?? $contentInfo->mainLanguageCode;
$versionInfo = $this->contentService->loadVersionInfo($contentInfo, $versionNo);
$contentLanguages = $versionInfo->languageCodes;
$languageCode = $languageCode ?? $contentInfo->mainLanguageCode;

$eligibleSiteaccesses = [];
foreach ($this->getSiteaccesses() as $siteaccess) {
$rootLocationId = $this->configResolver->getParameter(
'content.tree_root.location_id',
null,
$siteaccess
);
if (!in_array($rootLocationId, $location->path)) {
continue;
$context = new SiteaccessPreviewVoterContext($location, $versionInfo, $siteaccess, $languageCode);
foreach ($this->siteaccessPreviewVoters as $siteaccessPreviewVoter) {
if ($siteaccessPreviewVoter->vote($context)) {
$eligibleSiteaccesses[] = $siteaccess;
break;
}
}

$siteaccessLanguages = $this->configResolver->getParameter(
'languages',
null,
$siteaccess
);
if (!in_array($languageCode, $siteaccessLanguages)) {
continue;
}

$primarySiteaccessLanguage = reset($siteaccessLanguages);
if (
$languageCode !== $primarySiteaccessLanguage
&& in_array($primarySiteaccessLanguage, $contentLanguages)
) {
continue;
}

$eligibleSiteaccesses[] = $siteaccess;
}

return $eligibleSiteaccesses;
Expand Down

0 comments on commit 71ead62

Please sign in to comment.