|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Bundle\AdminUi\Controller\Permission; |
| 10 | + |
| 11 | +use Ibexa\AdminUi\Permission\LimitationResolverInterface; |
| 12 | +use Ibexa\Contracts\AdminUi\Controller\Controller; |
| 13 | +use Ibexa\Contracts\Core\Repository\ContentService; |
| 14 | +use Ibexa\Contracts\Core\Repository\LocationService; |
| 15 | +use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; |
| 16 | +use Ibexa\Contracts\Core\Repository\Values\Content\Location; |
| 17 | +use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; |
| 18 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 19 | +use Symfony\Component\HttpFoundation\Response; |
| 20 | + |
| 21 | +final class LanguageLimitationController extends Controller |
| 22 | +{ |
| 23 | + private ContentService $contentService; |
| 24 | + |
| 25 | + private LimitationResolverInterface $limitationResolver; |
| 26 | + |
| 27 | + private LocationService $locationService; |
| 28 | + |
| 29 | + public function __construct( |
| 30 | + ContentService $contentService, |
| 31 | + LimitationResolverInterface $limitationResolver, |
| 32 | + LocationService $locationService |
| 33 | + ) { |
| 34 | + $this->contentService = $contentService; |
| 35 | + $this->limitationResolver = $limitationResolver; |
| 36 | + $this->locationService = $locationService; |
| 37 | + } |
| 38 | + |
| 39 | + public function loadLanguageLimitationsForContentCreateAction(Location $location): Response |
| 40 | + { |
| 41 | + $contentInfo = $location->getContentInfo(); |
| 42 | + $contentType = $contentInfo->getContentType(); |
| 43 | + $contentCreateStruct = $this->contentService->newContentCreateStruct( |
| 44 | + $contentType, |
| 45 | + $contentInfo->getMainLanguageCode() |
| 46 | + ); |
| 47 | + $contentCreateStruct->sectionId = $contentInfo->getSection(); |
| 48 | + $locationCreateStruct = $this->locationService->newLocationCreateStruct($location->id); |
| 49 | + |
| 50 | + return new JsonResponse( |
| 51 | + $this->limitationResolver->getLanguageLimitations( |
| 52 | + 'create', |
| 53 | + $contentCreateStruct, |
| 54 | + [], |
| 55 | + [ |
| 56 | + $locationCreateStruct, |
| 57 | + ] |
| 58 | + ) |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + public function loadLanguageLimitationsForContentEditAction( |
| 63 | + ContentInfo $contentInfo, |
| 64 | + ?VersionInfo $versionInfo = null, |
| 65 | + ?Location $location = null |
| 66 | + ): Response { |
| 67 | + return new JsonResponse( |
| 68 | + $this->getLanguageLimitationsByFunction( |
| 69 | + 'edit', |
| 70 | + $contentInfo, |
| 71 | + $versionInfo, |
| 72 | + $location |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public function loadLanguageLimitationsForContentReadAction( |
| 78 | + ContentInfo $contentInfo, |
| 79 | + ?VersionInfo $versionInfo = null, |
| 80 | + ?Location $location = null |
| 81 | + ): Response { |
| 82 | + return new JsonResponse( |
| 83 | + $this->getLanguageLimitationsByFunction( |
| 84 | + 'read', |
| 85 | + $contentInfo, |
| 86 | + $versionInfo, |
| 87 | + $location |
| 88 | + ) |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @return array<array{ |
| 94 | + * languageCode: string, |
| 95 | + * name: string, |
| 96 | + * hasAccess: bool, |
| 97 | + * }> |
| 98 | + */ |
| 99 | + private function getLanguageLimitationsByFunction( |
| 100 | + string $function, |
| 101 | + ContentInfo $contentInfo, |
| 102 | + ?VersionInfo $versionInfo = null, |
| 103 | + ?Location $location = null |
| 104 | + ): array { |
| 105 | + $versionInfo ??= $this->contentService->loadVersionInfo($contentInfo); |
| 106 | + $location ??= $contentInfo->getMainLocation(); |
| 107 | + $targets = []; |
| 108 | + |
| 109 | + if (null !== $location) { |
| 110 | + $targets[] = $location; |
| 111 | + } |
| 112 | + |
| 113 | + return $this->limitationResolver->getLanguageLimitations( |
| 114 | + $function, |
| 115 | + $contentInfo, |
| 116 | + $versionInfo->getLanguages(), |
| 117 | + $targets |
| 118 | + ); |
| 119 | + } |
| 120 | +} |
0 commit comments