‪TYPO3CMS  ‪main
MutationRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
29 {
31 
36  public function ‪__construct(
37  private readonly ‪Map $staticMutations,
38  private readonly ‪SiteFinder $siteFinder,
39  private readonly ‪ModelService $modelService,
40  private readonly ‪ScopeRepository $scopeRepository,
41  private readonly ‪ResolutionRepository $resolutionRepository,
42  ) {
43  $this->‪resolvedMutations = null;
44  }
45 
50  public function findAll(bool $resolved = true): ‪Map
51  {
52  if ($resolved) {
55  }
56  return $this->staticMutations;
57  }
58 
63  public function findByScope(‪Scope $scope, bool $resolved = true): ‪Map
64  {
65  if ($resolved) {
67  return $this->‪resolvedMutations[$scope] ?? new ‪Map();
68  }
69  return $this->‪staticMutations[$scope] ?? new ‪Map();
70  }
71 
72  private function ‪resolveMutations(): void
73  {
74  if ($this->‪resolvedMutations !== null) {
75  return;
76  }
77  $this->‪resolvedMutations = clone $this->staticMutations;
78  $allScopes = $this->scopeRepository->findAll();
79  // fetch resolution mutations from the database
80  foreach ($this->resolutionRepository->findAll() as $resolution) {
81  // only for existing scopes (e.g. ignore scopes for sites, that are not existing anymore)
82  if (in_array($resolution->scope, $allScopes, true)) {
83  $mutationOrigin = new ‪MutationOrigin(‪MutationOriginType::resolution, $resolution->summary);
84  $target = $this->provideScopeInResolvedMutations($resolution->scope);
85  $target[$mutationOrigin] = $resolution->mutationCollection;
86  }
87  }
88  // fetch site-specific mutations
89  foreach ($this->scopeRepository->findAllFrontendSites() as $scope) {
90  $site = $this->‪resolveSite($scope);
91  $target = $this->provideScopeInResolvedMutations($scope);
92  $shallInheritDefault = (bool)($site->getConfiguration()['contentSecurityPolicies']['inheritDefault'] ?? true);
93  if ($shallInheritDefault && $scope->isFrontendSite()) {
94  foreach ($this->‪resolvedMutations[‪Scope::frontend()] ?? [] as $existingOrigin => $existingCollection) {
95  $target[$existingOrigin] = clone $existingCollection;
96  }
97  }
98  $mutationCollection = $this->‪resolveFrontendSiteMutationCollection($site);
99  if ($mutationCollection !== null) {
100  $mutationOrigin = new ‪MutationOrigin(MutationOriginType::site, $scope->siteIdentifier);
101  $target[$mutationOrigin] = $mutationCollection;
102  }
103  }
104  }
105 
107  {
108  $mutationConfigurations = $site->‪getConfiguration()['contentSecurityPolicies']['mutations'] ?? [];
109  if (empty($mutationConfigurations) || !is_array($mutationConfigurations)) {
110  return null;
111  }
112  $mutations = array_map(
113  fn(array $array) => $this->modelService->buildMutationFromArray($array),
114  $mutationConfigurations
115  );
116  return new ‪MutationCollection(...$mutations);
117  }
118 
122  private function provideScopeInResolvedMutations(‪Scope $scope): ‪Map
123  {
124  $reducedScope = $this->‪reduceScope($scope);
125  if (!isset($this->‪resolvedMutations[$reducedScope])) {
126  $this->‪resolvedMutations[$reducedScope] = new ‪Map();
127  }
128  return $this->‪resolvedMutations[$reducedScope];
129  }
130 
135  private function ‪reduceScope(‪Scope $scope): ‪Scope
136  {
137  if ($scope->‪isFrontendSite()) {
138  return ‪Scope::frontendSiteIdentifier($scope->siteIdentifier);
139  }
140  return $scope;
141  }
142 
143  private function ‪resolveSite(‪Scope $scope): ‪Site
144  {
145  return $scope->site ?? $this->siteFinder->getSiteByIdentifier($scope->siteIdentifier);
146  }
147 }
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\resolveFrontendSiteMutationCollection
‪resolveFrontendSiteMutationCollection(Site $site)
Definition: MutationRepository.php:106
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\resolvedMutations
‪return $this resolvedMutations
Definition: MutationRepository.php:54
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\resolveSite
‪resolveSite(Scope $scope)
Definition: MutationRepository.php:143
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\resolveMutations
‪resolveMutations()
Definition: MutationRepository.php:72
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationOrigin
Definition: MutationOrigin.php:25
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ModelService
Definition: ModelService.php:28
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\reduceScope
‪reduceScope(Scope $scope)
Definition: MutationRepository.php:135
‪TYPO3\CMS\Core\Site\SiteFinder
Definition: SiteFinder.php:31
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\__construct
‪__construct(private readonly Map $staticMutations, private readonly SiteFinder $siteFinder, private readonly ModelService $modelService, private readonly ScopeRepository $scopeRepository, private readonly ResolutionRepository $resolutionRepository,)
Definition: MutationRepository.php:36
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\frontend
‪static frontend()
Definition: Scope.php:46
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\resolveMutations
‪Map< Scope, findAll(bool $resolved=true):Map { if( $resolved) { $this-> resolveMutations()
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository
Definition: MutationRepository.php:29
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\ScopeRepository
Definition: ScopeRepository.php:27
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\resolution
‪@ resolution
Definition: MutationOriginType.php:24
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope
Definition: Scope.php:30
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\reduceScope
‪Map< MutationOrigin, provideScopeInResolvedMutations(Scope $scope):Map { $reducedScope=$this-> reduceScope($scope)
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\resolveMutations
‪Map< MutationOrigin, findByScope(Scope $scope, bool $resolved=true):Map { if( $resolved) { $this-> resolveMutations()
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy
Definition: ConsumableNonce.php:18
‪TYPO3\CMS\Core\Site\Entity\Site\getConfiguration
‪getConfiguration()
Definition: Site.php:316
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\isFrontendSite
‪isFrontendSite()
Definition: Scope.php:126
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Scope\frontendSiteIdentifier
‪static frontendSiteIdentifier(string $siteIdentifier)
Definition: Scope.php:64
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\Reporting\ResolutionRepository
Definition: ResolutionRepository.php:29
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationCollection
Definition: MutationCollection.php:24
‪TYPO3\CMS\Core\Type\Map
Definition: Map.php:47
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\$resolvedMutations
‪Map $resolvedMutations
Definition: MutationRepository.php:30
‪TYPO3\CMS\Core\Security\ContentSecurityPolicy\MutationRepository\staticMutations
‪return $this staticMutations
Definition: MutationRepository.php:56