src/Security/Voter/GroupVoter.php line 32

Open in your IDE?
  1. <?php
  2. /*
  3. * Disclaimer: This source code is protected by copyright law and by
  4. * international conventions.
  5. *
  6. * Any reproduction or partial or total distribution of the source code, by any
  7. * means whatsoever, is strictly forbidden.
  8. *
  9. * Anyone not complies with these provisions will be guilty of the offense of
  10. * infringement and the penal sanctions provided for by law.
  11. *
  12. * © 2022 All rights reserved.
  13. */
  14. namespace App\Security\Voter;
  15. use App\Entity\Permission;
  16. use App\Entity\User;
  17. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  18. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  19. use Symfony\Component\Security\Core\Security;
  20. /**
  21.  * Class UserVoter
  22.  *
  23.  * @author Rémy P. <r.peyron@ingeno.eu>
  24.  * @package App\Security\Voter
  25.  */
  26. class GroupVoter extends Voter implements GroupVoterInterface
  27. {
  28.     /**
  29.      * @var Security
  30.      */
  31.     private $security;
  32.     public function __construct (Security $security)
  33.     {
  34.         $this->security $security;
  35.     }
  36.     static function getAttributes (): array
  37.     {
  38.         return [self::SHOWself::EDITself::DELETEself::VIEWself::CREATE];
  39.     }
  40.     protected function supports (string $attribute$subject): bool
  41.     {
  42.         if (!in_array($attributeself::getAttributes())) {
  43.             return false;
  44.         }
  45.         return true;
  46.     }
  47.     protected function voteOnAttribute (string $attribute$subjectTokenInterface $token): bool
  48.     {
  49.         $user $token->getUser();
  50.         if (!$user instanceof User) {
  51.             // the user must be logged in; if not, deny access
  52.             return false;
  53.         }
  54.         if (!$user->isActive()) return false;
  55.         if ($this->security->isGranted(User::ROLE_SUPER_ADMIN)) return true;
  56.         /** @var Permission $permission */
  57.         foreach ($user->getUserGroup()->getPermissions()->toArray() as $permission) {
  58.             $match $permission->getModule()->getName() === $subject;
  59.             if (!$match) continue;
  60.             switch ($attribute) {
  61.                 case self::VIEW:
  62.                     return $permission->getCanView();
  63.                 case self::CREATE:
  64.                     return $permission->getCanCreate();
  65.                 case self::SHOW:
  66.                     return $permission->getCanShow();
  67.                 case self::EDIT:
  68.                     return $permission->getCanEdit();
  69.                 case self::DELETE:
  70.                     return $permission->getCanDelete();
  71.                 default:
  72.                     return false;
  73.             }
  74.         }
  75.         throw new \LogicException('This code should not be reached!');
  76.     }
  77. }