src/Security/Voter/ClienteVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. class ClienteVoter extends Voter
  9. {
  10.     // group must be separated by single underline, multiple words could be separated by double underline
  11.     // for example DASHBOARD_MEMO_VISUALIZZA__ASSEGNATARIO -> DASHBOARD -> MEMO -> VISUALIZZA ASSEGNATARIO
  12.     // Don't use double underline in constant names
  13.     // translate each expression separately at messages.it.yml
  14.     // Add __disabled suffix to make permission disabled and checked by default
  15.     const VISUALIZZA_DASHBOARD_CLIENTE  'CLIENTE_VISUALIZZA__DASHBOARD__CLIENTE';
  16.     const CANCELLA  'CLIENTE_CANCELLA';
  17.     const VISUALIZZA  'CLIENTE_VISUALIZZA__DISABLED';
  18.     const ACCESSO_ALLA_SEZIONE  'CLIENTE_ACCESSO__ALLA__SEZIONE';
  19.     const ORDER 1;
  20.     const PERMISSIONS_LIST = [
  21.         self::VISUALIZZA_DASHBOARD_CLIENTE,
  22.         self::CANCELLA,
  23.         self::ACCESSO_ALLA_SEZIONE,
  24.         self::VISUALIZZA,
  25.     ];
  26.     private $security;
  27.     public function __construct(Security $security)
  28.     {
  29.         $this->security $security;
  30.     }
  31.     protected function supports($attribute$subject)
  32.     {
  33.         // if the attribute isn't one we support, return false
  34.         if (!in_array($attributeself::PERMISSIONS_LIST)) {
  35.             return false;
  36.         }
  37.         return true;
  38.     }
  39.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  40.     {
  41.         $user $token->getUser();
  42.         // if the user is anonymous, do not grant access
  43.         if (!$user instanceof UserInterface) {
  44.             return false;
  45.         }
  46.         if($this->security->isGranted(User::ROLE_SUPER_ADMIN)) return true;
  47.         return in_array($attribute$user->getPermissions());
  48.     }
  49. }