src/Controller/DefaultController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notification;
  4. use App\Entity\Workspace;
  5. use App\Entity\Workspace\Member;
  6. use App\Entity\Workspace\Stream;
  7. use App\Entity\Workspace\Stream\Collaborator;
  8. use App\EventSubscriber\InvitationSubscriber;
  9. use Doctrine\Common\Util\ClassUtils;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class DefaultController extends AbstractController
  18. {
  19.     /**
  20.      * @Route("/{reactRouting}", name="loader", requirements={"reactRouting"="^(?!_profiler|_wdt|api|invitation|notification|download|tracking|build/storybook).+"}, defaults={"reactRouting": null})
  21.      */
  22.     public function indexAction(): Response
  23.     {
  24.         return $this->render('default/index.html.twig');
  25.     }
  26.     /**
  27.      * @Route("/invitation/{type}/{token}", name="invitation", requirements={"type"="workspace|stream"})
  28.      *
  29.      * @param Request             $request
  30.      * @param TranslatorInterface $translator
  31.      * @param string              $type
  32.      * @param string              $token
  33.      *
  34.      * @return RedirectResponse
  35.      */
  36.     public function invitationAction(Request $requestTranslatorInterface $translatorstring $typestring $token): RedirectResponse
  37.     {
  38.         $invitee null;
  39.         switch ($type) {
  40.             case 'workspace':
  41.                 /** @var Member|null $invitee */
  42.                 $invitee $this->getDoctrine()
  43.                     ->getManagerForClass(Member::class)
  44.                     ->getRepository(Member::class)
  45.                     ->findOneBy([
  46.                        'invitationToken' => $token,
  47.                     ]);
  48.                 break;
  49.             case 'stream':
  50.                 /** @var Collaborator|null $invitee */
  51.                 $invitee $this->getDoctrine()
  52.                     ->getManagerForClass(Collaborator::class)
  53.                     ->getRepository(Collaborator::class)
  54.                     ->findOneBy([
  55.                        'invitationToken' => $token,
  56.                     ]);
  57.                 break;
  58.         }
  59.         if (!$invitee) {
  60.             throw new \InvalidArgumentException(
  61.                 $translator->trans('error.invitation.invalid_token')
  62.             );
  63.         }
  64.         if ($invitee->isExpired()) {
  65.             throw new \InvalidArgumentException(
  66.                 $translator->trans('error.invitation.expired_token')
  67.             );
  68.         }
  69.         if (!$invitee->getUser() && $invitee->getEmail()) {
  70.             $response = new RedirectResponse(
  71.                 $this->generateUrl('loader', ['reactRouting' => 'registration'])
  72.             );
  73.             $response->headers->setCookie(
  74.                 Cookie::create('inviteEmail')
  75.                     ->withValue($invitee->getEmail())
  76.                     ->withExpires(time() + 60)
  77.                     ->withHttpOnly(false)
  78.                     ->withSameSite(Cookie::SAMESITE_NONE)
  79.             );
  80.         } elseif ($invitee->getUser()) {
  81.             $response = new RedirectResponse(
  82.                 $this->generateUrl('loader', ['reactRouting' => 'login'])
  83.             );
  84.         }
  85.         if ($response) {
  86.             $request->getSession()->set(InvitationSubscriber::SESSION_NAME, [
  87.                 'id' => $invitee->getId(),
  88.                 'objectClass' => ClassUtils::getClass($invitee),
  89.             ]);
  90.             return $response;
  91.         }
  92.         throw new \InvalidArgumentException(
  93.             $translator->trans('error.invitation.invalid_token')
  94.         );
  95.     }
  96. }