Skip to content

Commit

Permalink
Added endpoints to load language limitations for content
Browse files Browse the repository at this point in the history
  • Loading branch information
ciastektk committed Sep 14, 2023
1 parent 4018614 commit 620d755
Show file tree
Hide file tree
Showing 11 changed files with 705 additions and 134 deletions.
15 changes: 5 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8630,6 +8630,11 @@ parameters:
count: 1
path: src/lib/Pagination/Pagerfanta/URLWildcardAdapter.php

-
message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\LookupLimitationResult\\:\\:\\$hasAccess\\.$#"
count: 1
path: src/lib/Permission/LimitationResolver.php

-
message: "#^Access to protected property Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\LookupLimitationResult\\:\\:\\$lookupPolicyLimitations\\.$#"
count: 2
Expand Down Expand Up @@ -8685,16 +8690,6 @@ parameters:
count: 1
path: src/lib/Permission/PermissionChecker.php

-
message: "#^Parameter \\#1 \\$array of function array_filter expects array, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\Content\\\\Language\\> 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\\<int\\>, array\\<string\\> given\\.$#"
count: 2
path: src/lib/Permission/PermissionChecker.php

-
message: "#^Parameter \\#1 \\$value of function count expects array\\|Countable, iterable\\<Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Values\\\\User\\\\UserGroup\\> given\\.$#"
count: 1
Expand Down
120 changes: 120 additions & 0 deletions src/bundle/Controller/Permission/LanguageLimitationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

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

namespace Ibexa\Bundle\AdminUi\Controller\Permission;

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\ContentInfo;
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 LimitationResolverInterface $limitationResolver;

private LocationService $locationService;

public function __construct(
ContentService $contentService,
LimitationResolverInterface $limitationResolver,
LocationService $locationService
) {
$this->contentService = $contentService;
$this->limitationResolver = $limitationResolver;
$this->locationService = $locationService;
}

public function loadLanguageLimitationsForContentCreateAction(Location $location): Response
{
$contentInfo = $location->getContentInfo();
$contentType = $contentInfo->getContentType();
$contentCreateStruct = $this->contentService->newContentCreateStruct(
$contentType,
$contentInfo->getMainLanguageCode()
);
$contentCreateStruct->sectionId = $contentInfo->getSection();
$locationCreateStruct = $this->locationService->newLocationCreateStruct($location->id);

return new JsonResponse(
$this->limitationResolver->getLanguageLimitations(
'create',
$contentCreateStruct,
[],
[
$locationCreateStruct,
]
)
);
}

public function loadLanguageLimitationsForContentEditAction(
ContentInfo $contentInfo,
?VersionInfo $versionInfo = null,
?Location $location = null
): Response {
return new JsonResponse(
$this->getLanguageLimitationsByFunction(
'edit',
$contentInfo,
$versionInfo,
$location
)
);
}

public function loadLanguageLimitationsForContentReadAction(
ContentInfo $contentInfo,
?VersionInfo $versionInfo = null,
?Location $location = null
): Response {
return new JsonResponse(
$this->getLanguageLimitationsByFunction(
'read',
$contentInfo,
$versionInfo,
$location
)
);
}

/**
* @return array<array{
* languageCode: string,
* name: string,
* hasAccess: bool,
* }>
*/
private function getLanguageLimitationsByFunction(
string $function,
ContentInfo $contentInfo,
?VersionInfo $versionInfo = null,
?Location $location = null
): array {
$versionInfo ??= $this->contentService->loadVersionInfo($contentInfo);
$location ??= $contentInfo->getMainLocation();
$targets = [];

if (null !== $location) {
$targets[] = $location;
}

return $this->limitationResolver->getLanguageLimitations(
$function,
$contentInfo,
$versionInfo->getLanguages(),
$targets
);
}
}
33 changes: 33 additions & 0 deletions src/bundle/Resources/config/routing.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -958,3 +958,36 @@ ibexa.asset.upload_image:
defaults:
_controller: 'Ibexa\Bundle\AdminUi\Controller\AssetController::uploadImageAction'
methods: [POST]

#
# Permissions
#
ibexa.permission.limitation.language.content_create:
path: /permission/limitation/language/content-create/{locationId}
options:
expose: true
defaults:
_controller: 'Ibexa\Bundle\AdminUi\Controller\Permission\LanguageLimitationController::loadLanguageLimitationsForContentCreateAction'
methods: [GET]
requirements:
locationId: \d+

ibexa.permission.limitation.language.content_edit:
path: /permission/limitation/language/content-edit/{contentInfoId}
options:
expose: true
defaults:
_controller: 'Ibexa\Bundle\AdminUi\Controller\Permission\LanguageLimitationController::loadLanguageLimitationsForContentEditAction'
methods: [GET]
requirements:
contentInfoId: \d+

ibexa.permission.limitation.language.content_read:
path: /permission/limitation/language/content-read/{contentInfoId}
options:
expose: true
defaults:
_controller: 'Ibexa\Bundle\AdminUi\Controller\Permission\LanguageLimitationController::loadLanguageLimitationsForContentReadAction'
methods: [GET]
requirements:
contentInfoId: \d+
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/services/controllers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,9 @@ services:
autowire: true

Ibexa\Bundle\AdminUi\Controller\User\InvitationController: ~

Ibexa\Bundle\AdminUi\Controller\Permission\LanguageLimitationController:
parent: Ibexa\Contracts\AdminUi\Controller\Controller
autowire: true
tags:
- controller.service_arguments
5 changes: 5 additions & 0 deletions src/bundle/Resources/config/services/permissions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ services:
alias: Ibexa\AdminUi\Permission\PermissionChecker

Ibexa\AdminUi\Permission\LookupLimitationsTransformer: ~

Ibexa\AdminUi\Permission\LimitationResolver: ~

Ibexa\AdminUi\Permission\LimitationResolverInterface:
alias: Ibexa\AdminUi\Permission\LimitationResolver
14 changes: 10 additions & 4 deletions src/contracts/Permission/PermissionCheckerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,26 @@ public function getRestrictions(array $hasAccess, string $class): array;
public function canCreateInLocation(Location $location, $hasAccess): bool;

/**
* @internal
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*
* @internal
*
* @deprecated 4.6.0 The "\Ibexa\Contracts\AdminUi\Permission\PermissionCheckerInterface::getContentCreateLimitations()" method is deprecated, will be removed in 5.0.
* Use { @see \Ibexa\AdminUi\Permission\LimitationResolverInterface::getContentCreateLimitations() } instead.
*/
public function getContentCreateLimitations(Location $parentLocation): LookupLimitationResult;

/**
* @internal
*
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\BadStateException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
*
* @internal
*
* @deprecated 4.6.0 The "\Ibexa\Contracts\AdminUi\Permission\PermissionCheckerInterface::getContentUpdateLimitations()" method is deprecated, will be removed in 5.0.
* Use { @see \Ibexa\AdminUi\Permission\LimitationResolverInterface::getContentUpdateLimitations } instead.
*/
public function getContentUpdateLimitations(Location $parentLocation): LookupLimitationResult;
}
Expand Down
Loading

0 comments on commit 620d755

Please sign in to comment.