vendor/nellapp/sdk-bundle/src/Permission/Controller/Async/ChannelResourceAccessCacheController.php line 16

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Permission\Controller\Async;
  3. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  4. use Nellapp\Bundle\SDKBundle\Channel\Entity\ChannelInterface;
  5. use Nellapp\Bundle\SDKBundle\GlobalGetters\GlobalEntitiesService;
  6. use Nellapp\Bundle\SDKBundle\Permission\ChannelResourceManager;
  7. use Nellapp\Bundle\SDKBundle\Permission\Security\Authorization\AuthorizationHelper;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  13. class ChannelResourceAccessCacheController extends AbstractController
  14. {
  15.     public function __construct(
  16.         private GlobalEntitiesService  $globalEntitiesService,
  17.         private ChannelResourceManager $channelResourceManager,
  18.         private AuthorizationHelper    $authorizationHelper,
  19.     )
  20.     {
  21.     }
  22.     public function __invoke(Request $request, string $channelId, string $userId): JsonResponse
  23.     {
  24.         $channel = $this->getChannel($channelId);
  25.         if (!$channel) {
  26.             throw new NotFoundHttpException('');
  27.         }
  28.         $user = $this->getUserCheck($userId);
  29.         if (!$user) {
  30.             throw new NotFoundHttpException('');
  31.         }
  32.         $resourceClass = $this->getResourceClass($request);
  33.         if (!$resourceClass) {
  34.             throw new NotFoundHttpException('');
  35.         }
  36.         $permCheck = $this->getPermCheck($request);
  37.         if (!$permCheck) {
  38.             throw new NotFoundHttpException('');
  39.         }
  40.         $data = json_decode($request->getContent(), true);
  41.         if (!isset($data['ids']) || !is_array($data['ids'])) {
  42.             throw new BadRequestHttpException();
  43.         }
  44.         $channelResourceRepository = $this->channelResourceManager->getChannelResourceRepository($resourceClass);
  45.         if (!$channelResourceRepository) {
  46.             throw new BadRequestHttpException();
  47.         }
  48.         $qb = $channelResourceRepository->queryByChannel($channel);
  49.         $iterable = $qb->andWhere($qb->getRootAliases()[0] . '.id IN (:ids)')
  50.             ->setParameter('ids', $data['ids'])
  51.             ->getQuery()
  52.             ->toIterable();
  53.         $accessibleIds = [];
  54.         foreach ($iterable as $resource) {
  55.             if ($this->authorizationHelper->isUserGranted($user, $permCheck, $resource)) {
  56.                 $accessibleIds[] = $resource->getId();
  57.             }
  58.         }
  59.         return new JsonResponse(['accessibleIds' => $accessibleIds]);
  60.     }
  61.     private function getChannel(string $channelId): ?ChannelInterface
  62.     {
  63.         return $this->globalEntitiesService->getChannelRepository()->find($channelId);
  64.     }
  65.     private function getUserCheck(string $userId): ?UserInterface
  66.     {
  67.         return $this->globalEntitiesService->getUserRepository()->find($userId);
  68.     }
  69.     public function getResourceClass(Request $request): string
  70.     {
  71.         return $request->attributes->get('_resource_class');
  72.     }
  73.     public function getPermCheck(Request $request): string
  74.     {
  75.         return $request->attributes->get('_perm_check');
  76.     }
  77. }