src/Security/Voter/AllEntityVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Constant\ClassNameRoleActionConstant;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class AllEntityVoter extends Voter
  9. {
  10.     protected function supports(string $attribute$subject): bool
  11.     {
  12.         return in_array($attribute, [
  13.             ClassNameRoleActionConstant::VIEW,
  14.             ClassNameRoleActionConstant::CREATE,
  15.             ClassNameRoleActionConstant::EDIT,
  16.             ClassNameRoleActionConstant::DELETE,
  17.             ClassNameRoleActionConstant::API,
  18.         ]);
  19.     }
  20.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  21.     {
  22.         /** @var User $user */
  23.         $user $token->getUser();
  24.         // if the user is anonymous, do not grant access
  25.         if (!$user instanceof UserInterface) {
  26.             return false;
  27.         }
  28.         $className strtolower((new \ReflectionClass($subject))->getShortName());
  29.         $settingArray = [
  30.             'reproduct',
  31.             'season',
  32.             'period',
  33.             'culture',
  34.         ];
  35.         if(in_array($className,$settingArray)){
  36.             $className 'settings';
  37.         }
  38.         // ... (check conditions and return true to grant permission) ...
  39.         $roleGroup $user->getRoleGroup();
  40.         if (!empty($roleGroup)) {
  41.             $roleActions $roleGroup->getRoleActions();
  42.             if ($roleActions->containsKey($className)) {
  43.                 $roleAction $roleActions->get($className);
  44.                 return $roleAction->$attribute();
  45.             }
  46.         }
  47.         return false;
  48.     }
  49. }