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