src/Security/DashboardVoter.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 DashboardVoter extends Voter
  10. {
  11.     // these strings are just invented: you can use anything
  12.     public const CAN_VIEW_ELEVATOR 'canViewElevator';
  13.     public const CAN_VIEW_NEA 'canViewNea';
  14.     private $security;
  15.     public function __construct(Security $security)
  16.     {
  17.         $this->security $security;
  18.     }
  19.     protected function supports($attribute$subject): bool
  20.     {
  21.         // if the attribute isn't one we support, return false
  22.         if (!in_array($attribute, [self::CAN_VIEW_ELEVATORself::CAN_VIEW_NEA])) {
  23.             return false;
  24.         }
  25.         // only vote on `Post` objects
  26.         if (!$subject instanceof Nea) {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  32.     {
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             // the user must be logged in; if not, deny access
  36.             return false;
  37.         }
  38.         // you know $subject is a Post object, thanks to `supports()`
  39.         /** @var Post $post */
  40.         $nea $subject;
  41.         switch ($attribute) {
  42.             case self::CAN_VIEW_ELEVATOR:
  43.                 return $this->canViewElevator($user);
  44.             case self::CAN_VIEW_NEA:
  45.                 return $this->canViewNea($user);
  46.         }
  47.         throw new \LogicException('This code should not be reached!');
  48.     }
  49.     private function canViewElevator(User $user): bool
  50.     {
  51.         if (
  52.             in_array('ROLE_ELV'$user->getRoles())
  53.             && (
  54.                 $this->security->isGranted('ROLE_OPERATOR')
  55.                 || $this->security->isGranted('ROLE_CUSTODIAN')
  56.                 || $this->security->isGranted('ROLE_DL')
  57.                 || $this->security->isGranted('ROLE_SBS')
  58.                 || $this->security->isGranted('ROLE_LIFTMANAGER')
  59.             )
  60.         ) {
  61.             return true;
  62.         }
  63.         return false;
  64.     }
  65.     private function canViewNea(User $user): bool
  66.     {
  67.         if (
  68.             in_array('ROLE_NEA'$user->getRoles())
  69.             && (
  70.                 $this->security->isGranted('ROLE_OPERATOR')
  71.                 || $this->security->isGranted('ROLE_CUSTODIAN')
  72.                 || $this->security->isGranted('ROLE_DL')
  73.                 || $this->security->isGranted('ROLE_SBS')
  74.                 || $this->security->isGranted('ROLE_LIFTMANAGER')
  75.             )
  76.         ) {
  77.             return true;
  78.         }
  79.         return false;
  80.     }
  81. }