src/Security/PageVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Main\Page;
  4. use App\Model\User\Entity\User\User;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class PageVoter extends Voter
  10. {
  11.     const VISIBLE 'visible';
  12.     const WRITABLE 'writable';
  13.     const DELETED 'deleted';
  14.     private $requestStack;
  15.     private $em;
  16.     public function __construct(RequestStack $requestStackEntityManagerInterface $em)
  17.     {
  18.         $this->requestStack $requestStack;
  19.         $this->em $em;
  20.     }
  21.     protected function supports($attribute$subject)
  22.     {
  23.         if ($subject instanceof Page && $attribute == self::VISIBLE || $attribute == self::WRITABLE || $attribute == self::DELETED) {
  24.             return true;
  25.         }
  26.         return false;
  27.     }
  28.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  29.     {
  30.         $user $token->getUser();
  31.         if (!$user instanceof User) {
  32.             return false;
  33.         }
  34.         /** @var $subject Page */
  35.         foreach ($subject->getPageAccesses() as $pageAccess) {
  36.             if ($pageAccess->getPage()->getId() == $subject->getId() && $pageAccess->getRole()->getId() == $user->getRoleDb()->getId()) {
  37.                 if ($attribute == self::VISIBLE && $pageAccess->getVisible() == 1) {
  38.                     return true;
  39.                 }
  40.                 if ($attribute == self::WRITABLE && $pageAccess->getWritable() == 1) {
  41.                     return true;
  42.                 }
  43.                 if ($attribute == self::DELETED && $pageAccess->getDeleted() == 1) {
  44.                     return true;
  45.                 }
  46.             }
  47.         }
  48.         return false;
  49.     }
  50. }