src/Security/TemperatureNotificationVoter.php line 15

Open in your IDE?
  1. <?php
  2. /**
  3.  * @license Silk Software House Sp. Z O. O.
  4.  */
  5. declare(strict_types=1);
  6. namespace App\Security;
  7. use App\Entity\TemperatureNotification;
  8. use App\Entity\User;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. class TemperatureNotificationVoter extends Voter
  12. {
  13.     public const SHOW 'show';
  14.     public const NEW = 'new';
  15.     public const EDIT 'edit';
  16.     public const DELETE 'delete';
  17.     protected function supports(string $attribute$subject): bool
  18.     {
  19.         return $subject instanceof TemperatureNotification
  20.             && in_array($attribute, [self::SHOWself::NEW, self::EDITself::DELETE], true);
  21.     }
  22.     /**
  23.      * @param TemperatureNotification $subject
  24.      */
  25.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  26.     {
  27.         $user $token->getUser();
  28.         if (!$user instanceof User) {
  29.             return false;
  30.         }
  31.         if (self::NEW === $attribute && null === $subject->getId()) {
  32.             return true;
  33.         }
  34.         return null !== $subject->getUser()
  35.             && $subject->getUser()->getId() === $user->getId();
  36.     }
  37. }