src/EventSubscriber/OrganizationSubscriber.php line 82

  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  9. use App\Controller\OrganizationAuthenticatedController;
  10. use App\Repository\Usm\UserRepository;
  11. class OrganizationSubscriber implements EventSubscriberInterface
  12. {
  13.     private $tokenStorage;
  14.     private $userRepository;
  15.     
  16.     public function __construct(TokenStorageInterface $tokenStorageUserRepository $userRepository)
  17.     {
  18.          $this->tokenStorage $tokenStorage;
  19.          $this->userRepository $userRepository;
  20.     }
  21.     public function onKernelController(ControllerEvent $event)
  22.     {
  23.         $controller $event->getController();
  24.         // when a controller class defines multiple action methods, the controller
  25.         // is returned as [$controllerInstance, 'methodName']
  26.         if (is_array($controller)) {
  27.             $controller $controller[0];
  28.         }
  29.         
  30.         if (!$token $this->tokenStorage->getToken()) {
  31.             return ;
  32.         }
  33.         if (!$user $token->getUser()) {
  34.             return ;
  35.         }
  36.         if ($controller instanceof OrganizationAuthenticatedController) {
  37.             $accessDenied false;
  38.             
  39.             if ($event->getRequest()->headers->has('Organization')){
  40.                 $headerOrganizationId $event->getRequest()->headers->get('Organization');
  41.                 
  42.                 $organizationId $this->userRepository->getUserOrganization($user->getId());
  43.                 
  44.                 $organizationValid false;
  45.                 
  46.                 if ($headerOrganizationId == $organizationId){
  47.                     $organizationValid true;
  48.                 }else{
  49.                     $userRoles $user->getRoles();
  50.                     
  51.                     if (in_array('ROLE_ADMIN'$userRoles)){
  52.                         $organizationValid true;
  53.                     }
  54.                 }
  55.                 
  56.                 if ($organizationValid){
  57.                     // mark the request as having passed owner authentication
  58.                     $event->getRequest()->attributes->set('auth_organization_id'$organizationId);
  59.                     $event->getRequest()->getSession()->set('__organization'$organizationId);
  60.                 }else{
  61.                     $accessDenied true;
  62.                 }
  63.             }
  64.             
  65.             if ($accessDenied) {
  66.                 throw new AccessDeniedHttpException('This action needs a valid organization!');
  67.             }
  68.         }
  69.     }
  70.     
  71.     public function onKernelResponse(ResponseEvent $event)
  72.     {
  73.         // check to see if onKernelController marked this as a token "auth'ed" request
  74.         if (!$organizationId $event->getRequest()->attributes->get('auth_organization_id')) {
  75.             return;
  76.         }
  77.         $response $event->getResponse();
  78.         // create a hash and set it as a response header
  79.         $hash sha1($response->getContent().$organizationId);
  80.         
  81.         $response->headers->set('X-ORGANIZATION-ID'$hash);
  82.     }
  83.     public static function getSubscribedEvents()
  84.     {
  85.         return [
  86.             KernelEvents::CONTROLLER => 'onKernelController',
  87.             KernelEvents::RESPONSE => 'onKernelResponse',
  88.         ];
  89.     }
  90. }