src/EventSubscriber/TeamCreateSubscriber.php line 41

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber;
  4. use ApiPlatform\Symfony\EventListener\EventPriorities;
  5. use App\Entity\Company;
  6. use App\Entity\Team;
  7. use App\Entity\Teamplay;
  8. use App\Entity\User;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Security\Core\Security;
  14. /**
  15.  * POST /api/teams (créer une team) : la team est toujours créée dans l'entreprise de l'utilisateur
  16.  * (le champ « company » du corps est ignoré) et son challenge doit appartenir à cette entreprise.
  17.  * Le compte d'une entreprise (back-office manager, sans fiche client) crée pour la sienne : une autre entreprise
  18.  * dans le corps est refusée. PATCH par ce compte : le challenge doit aussi appartenir à son entreprise.
  19.  * L'admin n'est pas concerné.
  20.  */
  21. class TeamCreateSubscriber implements EventSubscriberInterface
  22. {
  23.     private Security $security;
  24.     public function __construct(Security $security)
  25.     {
  26.         $this->security = $security;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             KernelEvents::VIEW => ['checkCreate', EventPriorities::PRE_VALIDATE],
  32.         ];
  33.     }
  34.     public function checkCreate(ViewEvent $event): void
  35.     {
  36.         $team = $event->getControllerResult();
  37.         if (!$team instanceof Team || $this->security->isGranted(User::ROLE_ADMIN)) {
  38.             return;
  39.         }
  40.         $attributes = $event->getRequest()->attributes;
  41.         $user = $this->security->getUser();
  42.         $managedCompany = $user instanceof User && $this->security->isGranted(User::ROLE_COMPANY) ? $user->getCompany() : null;
  43.         if ('patch' === $attributes->get('_api_item_operation_name')) {
  44.             if (null !== $managedCompany) {
  45.                 $this->assertTeamplayOfCompany($team, $managedCompany);
  46.             }
  47.             return;
  48.         }
  49.         if ('post' !== $attributes->get('_api_collection_operation_name')) {
  50.             return;
  51.         }
  52.         if (null !== $managedCompany && null !== $team->getCompany() && $team->getCompany()->getId() !== $managedCompany->getId()) {
  53.             throw new AccessDeniedHttpException('Cette entreprise n\'est pas la vôtre.');
  54.         }
  55.         $company = $managedCompany ?? ($user instanceof User && $user->getClient() ? $user->getClient()->getCompany() : null);
  56.         if (null === $company) {
  57.             throw new AccessDeniedHttpException('Aucune entreprise rattachée à votre compte.');
  58.         }
  59.         $team->setCompany($company);
  60.         $this->assertTeamplayOfCompany($team, $company);
  61.     }
  62.     private function assertTeamplayOfCompany(Team $team, Company $company): void
  63.     {
  64.         $teamplay = $team->getTeamplay();
  65.         if ($teamplay instanceof Teamplay && (null === $teamplay->getCompany() || $teamplay->getCompany()->getId() !== $company->getId())) {
  66.             throw new AccessDeniedHttpException('Ce challenge n\'appartient pas à votre entreprise.');
  67.         }
  68.     }
  69. }