src/Security/Voter/OnlineShop/FamilySecurityVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\OnlineShop;
  3. use App\Entity\OnlineShop\Family;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesManagerEnum;
  6. use App\Enum\VotersEnum;
  7. use LogicException;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. final class FamilySecurityVoter extends Voter
  12. {
  13.     private Security $security;
  14.     private array $voters;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.         $this->voters = [
  19.             VotersEnum::LIST_FAMILY_ONLINE_SHOP,
  20.             VotersEnum::CREATE_FAMILY_ONLINE_SHOP,
  21.             VotersEnum::READ,
  22.             VotersEnum::UPDATE,
  23.             VotersEnum::DELETE,
  24.             VotersEnum::EXPORT_FAMILY_ONLINE_SHOP,
  25.         ];
  26.     }
  27.     protected function supports(string $attribute$subject): bool
  28.     {
  29.         // first check the $subject and last if the $attribute is supported,
  30.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  31.         if ($subject && !$subject instanceof Family) {
  32.             // only vote on these objects
  33.             return false;
  34.         }
  35.         if (in_array($attribute$this->voters)) {
  36.             // if the attribute is one we support
  37.             return true;
  38.         }
  39.         return false;
  40.     }
  41.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  42.     {
  43.         $user $token->getUser();
  44.         if (!$user instanceof User) {
  45.             // the user must be logged in; if not, deny access
  46.             return false;
  47.         }
  48.         switch ($attribute) {
  49.             case VotersEnum::LIST_FAMILY_ONLINE_SHOP:
  50.                 return $this->canList();
  51.             case VotersEnum::CREATE_FAMILY_ONLINE_SHOP:
  52.                 return $this->canCreate();
  53.             case VotersEnum::READ:
  54.                 return $this->canRead();
  55.             case VotersEnum::UPDATE:
  56.                 return $this->canUpdate();
  57.             case VotersEnum::DELETE:
  58.                 return $this->canDelete();
  59.             case VotersEnum::EXPORT_FAMILY_ONLINE_SHOP:
  60.                 return $this->canExport();
  61.         }
  62.         throw new LogicException('This code should not be reached!');
  63.     }
  64.     private function canList(): bool
  65.     {
  66.         return $this->isAdminUser();
  67.     }
  68.     private function canCreate(): bool
  69.     {
  70.         return $this->isAdminUser();
  71.     }
  72.     private function canRead(): bool
  73.     {
  74.         return $this->isAdminUser();
  75.     }
  76.     private function canUpdate(): bool
  77.     {
  78.         return $this->isAdminUser();
  79.     }
  80.     private function canDelete(): bool
  81.     {
  82.         return $this->isAdminUser();
  83.     }
  84.     private function canExport(): bool
  85.     {
  86.         return $this->isAdminUser();
  87.     }
  88.     private function isAdminUser(): bool
  89.     {
  90.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_ONLINE_SHOP);
  91.     }
  92. }