src/Security/AdminEntityAccessVoter.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security;
  3. use App\Entity\LeadInterface;
  4. use App\Entity\OrganizationInterface;
  5. use App\Entity\PageInterface;
  6. use App\Entity\SiteInterface;
  7. use App\Entity\UserInterface;
  8. use App\Repository\SiteRepository;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. class AdminEntityAccessVoter extends Voter
  13. {
  14.     /**
  15.      * Available voter attribute.
  16.      *
  17.      * @see $this->supports()
  18.      */
  19.     const ACCEED 'ACCEED';
  20.     /**
  21.      * The current site.
  22.      *
  23.      * @var SiteInterface
  24.      */
  25.     protected $site;
  26.     /**
  27.      * The current toutre name.
  28.      *
  29.      * @var string
  30.      */
  31.     protected $routeName;
  32.     /**
  33.      * Constructor
  34.      *
  35.      * @param RequestStack $requestStack
  36.      * @param SiteRepository $siteRepository
  37.      */
  38.     public function __construct(RequestStack $requestStackSiteRepository $siteRepository)
  39.     {
  40.         $request $requestStack->getCurrentRequest();
  41.         $this->site $siteRepository->findOneBy([
  42.             'url' => $request->getSchemeAndHttpHost()
  43.         ]);
  44.         $this->routeName $request->attributes->get('_route');
  45.     }
  46.     /**
  47.      * {@inheritDoc}
  48.      */
  49.     protected function supports(string $attribute$subject): bool
  50.     {
  51.         if ('admin' !== $this->routeName || self::ACCEED !== $attribute) {
  52.             return false;
  53.         }
  54.         return true;
  55.     }
  56.     /**
  57.      * {@inheritDoc}
  58.      */
  59.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  60.     {
  61.         if (!$this->site) {
  62.             return true;
  63.         }
  64.         $user $token->getUser();
  65.         if (!$user instanceof UserInterface) {
  66.             // Deny access to anonymous users.
  67.             return false;
  68.         }
  69.         return $this->canAcceed($subject$user);
  70.     }
  71.     /**
  72.      * Whether the current user can acceed or not.
  73.      *
  74.      * @param $subject
  75.      * @param UserInterface $user
  76.      * @return boolean
  77.      */
  78.     private function canAcceed($subjectUserInterface $user): bool
  79.     {
  80.         if (!$this->site && in_array('ROLE_ADMIN'$user->getRoles())) {
  81.             // Admin can acceed to create new sites for example.
  82.             return true;
  83.         }
  84.         if ($subject === null) {
  85.             // $subject is null when creating the entity.
  86.             return true;
  87.         }
  88.         $currentOrganization $this->site->getOrganization();
  89.         if ($subject instanceof OrganizationInterface) {
  90.             return $currentOrganization === $subject;
  91.         }
  92.         if ($subject instanceof SiteInterface) {
  93.             return $currentOrganization === $subject->getOrganization();
  94.         }
  95.         if ($subject instanceof PageInterface) {
  96.             return $currentOrganization === $subject->getSite()->getOrganization();
  97.         }
  98.         if ($subject instanceof LeadInterface) {
  99.             return $currentOrganization === $subject->getFormPage()->getSite()->getOrganization();
  100.         }
  101.         if ($subject instanceof UserInterface) {
  102.             return $subject->getOrganizations()->contains($subject);
  103.         }
  104.         return false;
  105.     }
  106. }