src/Security/Voter/Pratica231Voter.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 Pratica231Voter 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_ASSEGNATARIO  'PRATICA__231_ASSEGNATARIO__DISABLED';
  16.     const ACCESSO_ALLA_SEZIONE  'PRATICA__231_ACCESSO__ALLA__SEZIONE';
  17.     const VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO  'PRATICA__231_PRENDI__I__PERMESSI__DELL__ASSEGNATARIO';
  18.     const ORDER 10;
  19.     const PERMISSIONS_LIST = [
  20.         self::VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO,
  21.         self::ACCESSO_ALLA_SEZIONE,
  22.         self::VISUALIZZA_ASSEGNATARIO,
  23.     ];
  24.     private $security;
  25.     public function __construct(Security $security)
  26.     {
  27.         $this->security $security;
  28.     }
  29.     protected function supports($attribute$subject)
  30.     {
  31.         // if the attribute isn't one we support, return false
  32.         if (!in_array($attributeself::PERMISSIONS_LIST)) {
  33.             return false;
  34.         }
  35.         return true;
  36.     }
  37.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  38.     {
  39.         $user $token->getUser();
  40.         // if the user is anonymous, do not grant access
  41.         if (!$user instanceof UserInterface) {
  42.             return false;
  43.         }
  44.         if($this->security->isGranted(User::ROLE_SUPER_ADMIN)) return true;
  45.         return in_array($attribute$user->getPermissions());
  46.     }
  47. }