src/Security/Voter/DashboardVoter.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 DashboardVoter 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 CDU_VISUALIZZA  'DASHBOARD_CDU_VISUALIZZA';
  16.     const MEMO_VISUALIZZA_ASSEGNATARIO  'DASHBOARD_MEMO_VISUALIZZA__ASSEGNATARIO__DISABLED';
  17.     const APPUNTAMENTI_VISUALIZZA_ASSEGNATARIO  'DASHBOARD_APPUNTAMENTI_ASSEGNATARIO__DISABLED';
  18.     const SCADENZA_VISUALIZZA  'DASHBOARD_SCADENZA_VISUALIZZA__DISABLED';
  19.     const MYSELF_VISUALIZZA  'DASHBOARD_MYSELF_VISUALIZZA__DISABLED';
  20.     const MEMO_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO  'DASHBOARD_MEMO_PRENDI__I__PERMESSI__DELL__ASSEGNATARIO';
  21.     const APPUNTAMENTI_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO  'DASHBOARD_APPUNTAMENTI_PRENDI__I__PERMESSI__DELL__ASSEGNATARIO';
  22.     const ORDER 5;
  23.     const PERMISSIONS_LIST = [
  24.         self::MEMO_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO,
  25.         self::APPUNTAMENTI_VISUALIZZA_PRENDI_I_PERMESSI_DELL_ASSEGNATARIO,
  26.         self::MEMO_VISUALIZZA_ASSEGNATARIO,
  27.         self::CDU_VISUALIZZA,
  28.         self::APPUNTAMENTI_VISUALIZZA_ASSEGNATARIO,
  29.         self::SCADENZA_VISUALIZZA,
  30.         self::MYSELF_VISUALIZZA,
  31.     ];
  32.     private $security;
  33.     public function __construct(Security $security)
  34.     {
  35.         $this->security $security;
  36.     }
  37.     protected function supports($attribute$subject)
  38.     {
  39.         // if the attribute isn't one we support, return false
  40.         if (!in_array($attributeself::PERMISSIONS_LIST)) {
  41.             return false;
  42.         }
  43.         return true;
  44.     }
  45.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  46.     {
  47.         $user $token->getUser();
  48.         // if the user is anonymous, do not grant access
  49.         if (!$user instanceof UserInterface) {
  50.             return false;
  51.         }
  52.         if($this->security->isGranted(User::ROLE_SUPER_ADMIN)) return true;
  53.         return in_array($attribute$user->getPermissions());
  54.     }
  55. }