Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add site restriction for api #17

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ApiRestrictionContainer extends AbstractRestrictionContainer
protected $defaultRestrictionTypes = [
DeletedRestriction::class,
LanguageRestriction::class,
SiteRestriction::class,
];

/**
Expand Down
75 changes: 75 additions & 0 deletions Classes/Database/Query/Restriction/SiteRestriction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace DFAU\ToujouApi\Database\Query\Restriction;

use Psr\Http\Message\RequestInterface;
use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
use TYPO3\CMS\Core\Database\Query\Restriction\QueryRestrictionInterface;
use TYPO3\CMS\Core\Database\QueryGenerator;
use TYPO3\CMS\Core\Site\Entity\Site;

class SiteRestriction implements QueryRestrictionInterface
{
/** @var QueryGenerator */
private $queryGenerator;

/** @var null|int[] */
private $cachedSitePids;

public function __construct(QueryGenerator $queryGenerator)
{
$this->queryGenerator = $queryGenerator;
}

public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression
{
$constraints = [];

$sitePids = $this->getSitePids();

if ([] === $sitePids) {
return $expressionBuilder->andX();
}

foreach ($queriedTables as $tableAlias => $tableName) {
if ('pages' === $tableName) {
$constraints[] = $expressionBuilder->orX(
$expressionBuilder->in($tableAlias . '.uid', $sitePids),
$expressionBuilder->in($tableAlias . '.pid', $sitePids)
);
} else {
$constraints[] = $expressionBuilder->in($tableAlias . '.pid', $sitePids);
}
}

return $expressionBuilder->andX(...$constraints);
}

private function getSitePids(): array
{
if (\is_array($this->cachedSitePids)) {
return $this->cachedSitePids;
}

/** @var RequestInterface $request */
$request = $GLOBALS['TYPO3_REQUEST'] ?? null;

if (null === $request) {
return [];
}

/** @var Site $site */
$site = $request->getAttribute('site');

if (null === $site) {
return [];
}

$this->cachedSitePids = \explode(',', $this->queryGenerator->getTreeList($site->getRootPageId(), 999));

return $this->cachedSitePids;
}
}
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ services:

DFAU\ToujouApi\Database\Query\Restriction\LanguageRestriction:
public: true

DFAU\ToujouApi\Database\Query\Restriction\SiteRestriction:
public: true
7 changes: 7 additions & 0 deletions Tests/Acceptance/Fixtures/pages.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@
<l10n_source>2</l10n_source>
<l10n_parent>2</l10n_parent>
</pages>
<pages>
<uid>4</uid>
<pid>0</pid>
<title>Second Page Root</title>
<doktype>1</doktype>
<is_siteroot>1</is_siteroot>
</pages>
</dataset>