src/Security/Voter/User/NotificationVoter.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\User;
  4. use App\ReadModel\Procedure\Lot\DetailView;
  5. use App\Security\UserIdentity;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  9. use Symfony\Component\Security\Core\Security;
  10. class NotificationVoter extends Voter
  11. {
  12.     public const NOTIFICATION_SHOW 'notification_show';
  13.     private Security $security;
  14.     public function __construct(
  15.         Security $security
  16.     ) {
  17.         $this->security $security;
  18.     }
  19.     protected function supports(string $attribute$subject): bool
  20.     {
  21.         return in_array($attribute, [
  22.             self::NOTIFICATION_SHOW
  23.         ], true);
  24.     }
  25.     /**
  26.      * @param string $attribute
  27.      * @param DetailView $subject
  28.      * @param TokenInterface $token
  29.      * @return bool
  30.      */
  31.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  32.     {
  33.         // Moderators have full access
  34.         if ($this->security->isGranted('ROLE_MODERATOR')) {
  35.             return true;
  36.         }
  37.         $user $token->getUser();
  38.         if (!$user instanceof UserIdentity) {
  39.             return false;
  40.         }
  41.         if ($user->getId() !== $subject->id) {
  42.             throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  43.         }
  44.         return true;
  45.     }
  46. }