diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 96f0819df2..d39d471e17 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -8685,16 +8685,6 @@ parameters: count: 1 path: src/lib/Permission/PermissionChecker.php - - - message: "#^Parameter \\#1 \\$array of function array_filter expects array, iterable\\ given\\.$#" - count: 1 - path: src/lib/Permission/PermissionChecker.php - - - - message: "#^Parameter \\#1 \\$contentTypeIds of method Ibexa\\\\Contracts\\\\Core\\\\Limitation\\\\Target\\\\Builder\\\\VersionBuilder\\:\\:createFromAnyContentTypeOf\\(\\) expects array\\, array\\ given\\.$#" - count: 2 - path: src/lib/Permission/PermissionChecker.php - - message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, iterable\\ given\\.$#" count: 1 diff --git a/src/bundle/Controller/Permission/LanguageLimitationController.php b/src/bundle/Controller/Permission/LanguageLimitationController.php index 44f07e7b65..054e5a0df1 100644 --- a/src/bundle/Controller/Permission/LanguageLimitationController.php +++ b/src/bundle/Controller/Permission/LanguageLimitationController.php @@ -10,44 +10,30 @@ use Ibexa\AdminUi\Permission\LimitationResolverInterface; use Ibexa\Contracts\AdminUi\Controller\Controller; -use Ibexa\Contracts\Core\Repository\ContentService; -use Ibexa\Contracts\Core\Repository\LocationService; +use Ibexa\Contracts\Core\Repository\Values\Content\Content; +use Ibexa\Contracts\Core\Repository\Values\Content\Location; +use Ibexa\Contracts\Core\Repository\Values\Content\VersionInfo; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; final class LanguageLimitationController extends Controller { - private ContentService $contentService; - - private LocationService $locationService; - private LimitationResolverInterface $limitationResolver; - public function __construct( - ContentService $contentService, - LocationService $locationService, - LimitationResolverInterface $limitationResolver - ) { - $this->contentService = $contentService; - $this->locationService = $locationService; + public function __construct(LimitationResolverInterface $limitationResolver) + { $this->limitationResolver = $limitationResolver; } public function loadLanguageLimitationsForContentAction( - int $contentId, - ?int $versionNo = null, - ?int $locationId = null + Content $content, + ?VersionInfo $versionInfo = null, + ?Location $location = null ): Response { - $versionInfo = $this->contentService->loadVersionInfoById($contentId, $versionNo); - - if (null === $locationId) { - $locationId = $versionInfo->getContentInfo()->getMainLocationId(); - } - return new JsonResponse( $this->limitationResolver->getLanguageLimitations( - $versionInfo, - $this->locationService->loadLocation($locationId) + $versionInfo ?? $content->getVersionInfo(), + $location ?? $content->getVersionInfo()->getContentInfo()->getMainLocation() ) ); } diff --git a/src/bundle/Resources/config/routing.yaml b/src/bundle/Resources/config/routing.yaml index f8fa7de97c..e67588de01 100644 --- a/src/bundle/Resources/config/routing.yaml +++ b/src/bundle/Resources/config/routing.yaml @@ -962,13 +962,10 @@ ibexa.asset.upload_image: # # Permissions # - ibexa.permission.limitation.language: - path: permission/limitation/language/{contentId}/{versionNo}/{locationId} + path: permission/limitation/language/{contentId} options: expose: true defaults: - versionNo: null - locationId: null _controller: 'Ibexa\Bundle\AdminUi\Controller\Permission\LanguageLimitationController::loadLanguageLimitationsForContentAction' methods: [GET] diff --git a/src/lib/Permission/LimitationResolver.php b/src/lib/Permission/LimitationResolver.php index bb8c88de23..e4e7a6f457 100644 --- a/src/lib/Permission/LimitationResolver.php +++ b/src/lib/Permission/LimitationResolver.php @@ -91,36 +91,51 @@ public function getContentUpdateLimitations(Location $parentLocation): LookupLim public function getLanguageLimitations( VersionInfo $versionInfo, - Location $location + ?Location $location = null ): array { $languages = $versionInfo->getLanguages(); + $targets[] = (new VersionBuilder())->translateToAnyLanguageOf($this->getActiveLanguageCodes($languages))->build(); + if (null !== $location) { + $targets[] = $location; + } + $lookupLimitations = $this->permissionResolver->lookupLimitations( 'content', 'edit', $versionInfo->getContentInfo(), - [ - (new VersionBuilder())->translateToAnyLanguageOf($this->getActiveLanguageCodes($languages))->build(), - $location, - ], + $targets, [Limitation::LANGUAGE] ); $limitationLanguageCodes = $this->lookupLimitationsTransformer->getFlattenedLimitationsValues($lookupLimitations); - return array_map( - static function (Language $language) use ($limitationLanguageCodes): array { - return [ - 'languageCode' => $language->getLanguageCode(), - 'name' => $language->getName(), - 'hasAccess' => empty($limitationLanguageCodes) || in_array($language->getLanguageCode(), $limitationLanguageCodes, true), - ]; - }, - $languages - ); + $languageLimitations = []; + + foreach ($languages as $language) { + $languageLimitations[] = [ + 'languageCode' => $language->getLanguageCode(), + 'name' => $language->getName(), + 'hasAccess' => $this->hasAccessToLanguage($language, $limitationLanguageCodes), + ]; + } + + return $languageLimitations; } /** - * @return array + * @param array $limitationLanguageCodes + */ + private function hasAccessToLanguage(Language $language, array $limitationLanguageCodes): bool + { + return $language->isEnabled() + && ( + empty($limitationLanguageCodes) + || in_array($language->getLanguageCode(), $limitationLanguageCodes, true) + ); + } + + /** + * @return array */ private function getContentTypeIds(): array { @@ -138,17 +153,20 @@ private function getContentTypeIds(): array } /** + * @param iterable<\Ibexa\Contracts\Core\Repository\Values\Content\Language>|null $languages + * * @return array */ - private function getActiveLanguageCodes(?array $languageCodes = null): array + private function getActiveLanguageCodes(?iterable $languages = null): array { - $filter = array_filter( - $languageCodes ?? $this->languageService->loadLanguages(), - static function (Language $language) { - return $language->enabled; + $languageCodes = []; + $languages ??= $this->languageService->loadLanguages(); + foreach ($languages as $language) { + if ($language->isEnabled()) { + $languageCodes[] = $language->getLanguageCode(); } - ); + } - return array_column($filter, 'languageCode'); + return $languageCodes; } } diff --git a/src/lib/Permission/LimitationResolverInterface.php b/src/lib/Permission/LimitationResolverInterface.php index 2b9c8e67d2..0219e4ed1b 100644 --- a/src/lib/Permission/LimitationResolverInterface.php +++ b/src/lib/Permission/LimitationResolverInterface.php @@ -40,6 +40,6 @@ public function getContentUpdateLimitations(Location $parentLocation): LookupLim */ public function getLanguageLimitations( VersionInfo $versionInfo, - Location $location + ?Location $location = null ): array; }