src/Note/EventSubscriber/GrantPermissionSubscriber.php line 70

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2023 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2023 TECLA Consulting Group oü
  16.  */
  17. namespace App\Note\EventSubscriber;
  18. use App\Contact\Dto\Factory\ContactDtoFactoryInterface;
  19. use App\Contact\Model\ContactInterface;
  20. use App\Contact\Model\Identity\ContactId;
  21. use App\Contact\Repository\ContactRepositoryInterface;
  22. use App\Contact\Security\ContactContextInterface;
  23. use App\Note\Dto\Factory\NoteDtoFactoryInterface;
  24. use App\Note\Dto\Factory\NotePermissionDtoFactoryInterface;
  25. use App\Note\Event\Note\CreatedEvent;
  26. use App\Note\Model\NoteInterface;
  27. use App\Note\Service\NotePermissionServiceInterface;
  28. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  29. final class GrantPermissionSubscriber implements EventSubscriberInterface
  30. {
  31.     private ContactContextInterface $contactContext;
  32.     private NotePermissionDtoFactoryInterface $notePermissionDtoFactory;
  33.     private NoteDtoFactoryInterface $noteDtoFactory;
  34.     private ContactDtoFactoryInterface $contactDtoFactory;
  35.     private NotePermissionServiceInterface $notePermissionService;
  36.     private ContactRepositoryInterface $contactRepository;
  37.     public function __construct(
  38.         ContactContextInterface $contactContext,
  39.         NotePermissionDtoFactoryInterface $notePermissionDtoFactory,
  40.         NoteDtoFactoryInterface $noteDtoFactory,
  41.         ContactDtoFactoryInterface $contactDtoFactory,
  42.         NotePermissionServiceInterface $notePermissionService,
  43.         ContactRepositoryInterface $contactRepository
  44.     ) {
  45.         $this->contactContext $contactContext;
  46.         $this->notePermissionDtoFactory $notePermissionDtoFactory;
  47.         $this->noteDtoFactory $noteDtoFactory;
  48.         $this->contactDtoFactory $contactDtoFactory;
  49.         $this->notePermissionService $notePermissionService;
  50.         $this->contactRepository $contactRepository;
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             CreatedEvent::class => 'onCreatedEvent',
  56.         ];
  57.     }
  58.     public function onCreatedEvent(CreatedEvent $event): void
  59.     {
  60.         if (!$this->contactContext->getCurrent()) {
  61.             return;
  62.         }
  63.         if ($this->isTeclaContext()) {
  64.             $this->grantTeclaPermission($event->getNote());
  65.         } else {
  66.             $this->grantPermission($event->getNote(), $this->contactContext->getCurrent());
  67.         }
  68.     }
  69.     private function isTeclaContext(): bool
  70.     {
  71.         $allowedIds = [
  72.             'a1e7e2ae-e4be-11ea-848c-3448ed126851',
  73.             'a1e7e4fc-e4be-11ea-9d2b-3448ed126851',
  74.             'a1e9240c-e4be-11ea-8071-3448ed126851',
  75.             'a1eaf35e-e4be-11ea-8f03-3448ed126851',
  76.             'a1f0badc-e4be-11ea-9318-3448ed126851',
  77.         ];
  78.         if (false === \in_array($this->contactContext->getCurrent()?->getId()?->toRfc4122(), $allowedIds)) {
  79.             return false;
  80.         }
  81.         return true;
  82.     }
  83.     private function grantPermission(NoteInterface $noteContactInterface $contact): void
  84.     {
  85.         $dto $this->notePermissionDtoFactory->create()
  86.             ->setCreatedAt($note->getCreatedAt())
  87.             ->setNote($this->noteDtoFactory->createReferenceFromModel($note))
  88.             ->setContact($this->contactDtoFactory->createReferenceFromModel($contact))
  89.         ;
  90.         $this->notePermissionService->createFromDto($dto);
  91.     }
  92.     private function grantTeclaPermission(NoteInterface $note): void
  93.     {
  94.         $allowedIds = [
  95.             'a1e7e2ae-e4be-11ea-848c-3448ed126851',
  96.             'a1e7e4fc-e4be-11ea-9d2b-3448ed126851',
  97.             'a1e9240c-e4be-11ea-8071-3448ed126851',
  98.             'a1eaf35e-e4be-11ea-8f03-3448ed126851',
  99.             'a1f0badc-e4be-11ea-9318-3448ed126851',
  100.         ];
  101.         foreach ($allowedIds as $allowedId) {
  102.             $contact $this->contactRepository->find(ContactId::createFromString($allowedId));
  103.             $this->grantPermission($note$contact);
  104.         }
  105.     }
  106. }