src/Security/Voter/MachinePurposeDictionaryVoter.php line 10

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace App\Security\Voter;
  3. use App\Entity\MachinePurposeDictionary;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class MachinePurposeDictionaryVoter extends Voter
  8. {
  9.     public const VIEW 'view';
  10.     public const EDIT 'edit';
  11.     public const DELETE 'delete';
  12.     /**
  13.      * @param string $attribute
  14.      * @param mixed  $subject
  15.      *
  16.      * @return bool
  17.      */
  18.     protected function supports($attribute$subject): bool
  19.     {
  20.         $supports true;
  21.         if (!\in_array($attribute, [self::VIEWself::EDITself::DELETE], true)) {
  22.             $supports false;
  23.         }
  24.         if (!$subject instanceof MachinePurposeDictionary) {
  25.             $supports false;
  26.         }
  27.         return $supports;
  28.     }
  29.     /**
  30.      * @param string         $attribute
  31.      * @param mixed          $subject
  32.      * @param TokenInterface $token
  33.      *
  34.      * @return bool
  35.      */
  36.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  37.     {
  38.         $user $token->getUser();
  39.         $hasVotePassed false;
  40.         if (!$user instanceof User) {
  41.             return $hasVotePassed;
  42.         }
  43.         /** @var MachinePurposeDictionary $entity */
  44.         $entity $subject;
  45.         switch ($attribute) {
  46.             case self::VIEW:
  47.                 $hasVotePassed =  $this->canView($entity);
  48.                 break;
  49.             case self::EDIT:
  50.                 $hasVotePassed =  $this->canEdit($entity);
  51.                 break;
  52.             case self::DELETE:
  53.                 $hasVotePassed =  $this->canDelete($entity);
  54.                 break;
  55.         }
  56.         return $hasVotePassed;
  57.     }
  58.     /**
  59.      * @param MachinePurposeDictionary $entity
  60.      *
  61.      * @return bool
  62.      */
  63.     private function canView(MachinePurposeDictionary $entity): bool
  64.     {
  65.         // TODO Wykonać dostępy na podstawie roli użytkownika
  66.         // Admin i Projekt widzi i może robić wszystko
  67.         // Default/użytkownik może widzieć element i detale
  68.         return true;
  69.     }
  70.     /**
  71.      * @param MachinePurposeDictionary $toolFamily
  72.      *
  73.      * @return bool
  74.      */
  75.     private function canEdit(MachinePurposeDictionary $toolFamily): bool
  76.     {
  77.         // TODO Wykonać dostępy na podstawie roli użytkownika
  78.         // Admin i Projekt widzi i może robić wszystko
  79.         // Default/użytkownik może widzieć element i detale
  80.          return true;
  81.     }
  82.     /**
  83.      * @param MachinePurposeDictionary $toolFamily
  84.      *
  85.      * @return bool
  86.      */
  87.     private function canArchive(MachinePurposeDictionary $toolFamily): bool
  88.     {
  89.         // TODO Wykonać dostępy na podstawie roli użytkownika
  90.         // Admin i Projekt widzi i może robić wszystko
  91.         // Default/użytkownik może widzieć element i detale
  92.         return true;
  93.     }
  94.     /**
  95.      * @param MachinePurposeDictionary $toolFamily
  96.      *
  97.      * @return bool
  98.      */
  99.     private function canDelete(MachinePurposeDictionary $toolFamily): bool
  100.     {
  101.         // TODO Wykonać dostępy na podstawie roli użytkownika
  102.         // Admin i Projekt widzi i może robić wszystko
  103.         // Default/użytkownik może widzieć element i detale
  104.         return true;
  105.     }
  106. }