|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace App\Controller; |
| 4 | + |
| 5 | +use App\Security\Limitation\CustomLimitationValue; |
| 6 | +use Ibexa\Contracts\AdminUi\Controller\Controller; |
| 7 | +use Ibexa\Contracts\AdminUi\Permission\PermissionCheckerInterface; |
| 8 | +use Ibexa\Contracts\Core\Repository\PermissionResolver; |
| 9 | +use Ibexa\Contracts\User\Controller\AuthenticatedRememberedCheckTrait; |
| 10 | +use Ibexa\Core\MVC\Symfony\Security\Authorization\Attribute; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | + |
| 14 | +class CustomLimitationController extends Controller |
| 15 | +{ |
| 16 | + use AuthenticatedRememberedCheckTrait { |
| 17 | + AuthenticatedRememberedCheckTrait::performAccessCheck as public traitPerformAccessCheck; |
| 18 | + } |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + // ..., |
| 22 | + private readonly PermissionResolver $permissionResolver, |
| 23 | + private readonly PermissionCheckerInterface $permissionChecker |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + // Controller actions... |
| 28 | + public function customAction(Request $request): Response |
| 29 | + { |
| 30 | + // ... |
| 31 | + if ($this->getCustomLimitationValue()) { |
| 32 | + // Action only for user having the custom limitation checked |
| 33 | + } |
| 34 | + |
| 35 | + return new Response('<html><body>...</body></html>'); |
| 36 | + } |
| 37 | + |
| 38 | + private function getCustomLimitationValue(): bool |
| 39 | + { |
| 40 | + $hasAccess = $this->permissionResolver->hasAccess('custom_module', 'custom_function_2'); |
| 41 | + |
| 42 | + if (is_bool($hasAccess)) { |
| 43 | + return $hasAccess; |
| 44 | + } |
| 45 | + |
| 46 | + $customLimitationValues = $this->permissionChecker->getRestrictions( |
| 47 | + $hasAccess, |
| 48 | + CustomLimitationValue::class |
| 49 | + ); |
| 50 | + |
| 51 | + return $customLimitationValues['value'] ?? false; |
| 52 | + } |
| 53 | + |
| 54 | + public function performAccessCheck(): void |
| 55 | + { |
| 56 | + $this->traitPerformAccessCheck(); |
| 57 | + $this->denyAccessUnlessGranted(new Attribute('custom_module', 'custom_function_2')); |
| 58 | + } |
| 59 | +} |
0 commit comments