src/Security/NeaVoter.php line 13

Open in your IDE?
  1. <?php
  2. // src/Security/PostVoter.php
  3. namespace App\Security;
  4. use App\Entity\Nea;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. class NeaVoter extends Voter
  10. {
  11.     // these strings are just invented: you can use anything
  12.     public const EDIT 'edit';
  13.     public const VIEW 'view';
  14.     public const DELETE 'delete';
  15.     public const REFURBISHMENT 'refurbishment';
  16.     public const ZUES 'zues';
  17.     private $security;
  18.     public function __construct(Security $security)
  19.     {
  20.         $this->security $security;
  21.     }
  22.     protected function supports($attribute$subject): bool
  23.     {
  24.         // if the attribute isn't one we support, return false
  25.         if (!in_array($attribute, [self::EDITself::VIEWself::DELETEself::REFURBISHMENTself::ZUES])) {
  26.             return false;
  27.         }
  28.         // only vote on `Post` objects
  29.         if (!$subject instanceof Nea) {
  30.             return false;
  31.         }
  32.         return true;
  33.     }
  34.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  35.     {
  36.         $user $token->getUser();
  37.         if (!$user instanceof User) {
  38.             // the user must be logged in; if not, deny access
  39.             return false;
  40.         }
  41.         // you know $subject is a Post object, thanks to `supports()`
  42.         /** @var Post $post */
  43.         $nea $subject;
  44.         switch ($attribute) {
  45.             case self::REFURBISHMENT:
  46.                 return $this->canSetRefurbishment($nea$user);
  47.             case self::ZUES:
  48.                 return $this->canSetZues($nea$user);
  49.         }
  50.         throw new \LogicException('This code should not be reached!');
  51.     }
  52.     private function canSetRefurbishment(Nea $neaUser $user): bool
  53.     {
  54.         if ($this->security->isGranted('ROLE_CUSTODIAN')) {
  55.             return true;
  56.         }
  57.         return false;
  58.     }
  59.     private function canSetZues(Nea $neaUser $user): bool
  60.     {
  61.         // this assumes that the Post object has a `getOwner()` method
  62.         if (
  63.             $this->security->isGranted('ROLE_CUSTODIAN')
  64.             || $this->security->isGranted('ROLE_ZUES')
  65.         ) {
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     private function canView(Nea $neaUser $user): bool
  71.     {
  72.         if (
  73.             $this->security->isGranted('ROLE_CUSTODIAN')
  74.             || $this->security->isGranted('ROLE_DL')
  75.             || $this->security->isGranted('ROLE_ZUES')
  76.         ) {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81. }