src/Security/Voter/Procedure/ContractVoter.php line 14

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\Procedure;
  4. use App\ReadModel\Contract\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 ContractVoter extends Voter
  11. {
  12.     public const CONTRACT_SHOW 'contract_show';
  13.     /** @var Security */
  14.     private Security $security;
  15.     /**
  16.      * @param Security $security
  17.      */
  18.     public function __construct(Security $security)
  19.     {
  20.         $this->security $security;
  21.     }
  22.     protected function supports(string $attribute$subject): bool
  23.     {
  24.         return in_array($attribute, [
  25.             self::CONTRACT_SHOW,
  26.             ]) && $subject instanceof DetailView;
  27.     }
  28.     /**
  29.      * @param string $attribute
  30.      * @param DetailView $subject
  31.      * @param TokenInterface $token
  32.      * @return bool
  33.      */
  34.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  35.     {
  36.         // Moderators have full access
  37.         if ($this->security->isGranted('ROLE_MODERATOR')) {
  38.             return true;
  39.         }
  40.         $user $token->getUser();
  41.         if (!$user instanceof UserIdentity) {
  42.             return false;
  43.         }
  44.         /*** Список разрешенных профиль ид*/
  45.         $accessProfileIds = [];
  46.         $accessProfileIds[] = $subject->getOrganizerProfileId();
  47.         $accessProfileIds[] = $subject->getParticipantProfileId();
  48.         if (in_array($user->getProfileId(), $accessProfileIds) === false) {
  49.             throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  50.         }
  51.         switch ($attribute) {
  52.             case self::CONTRACT_SHOW:
  53.                 //is owner contract
  54.                 if ($subject->getParticipantProfileId() === $user->getProfileId()) {
  55.                     if ($subject->getStatus()->isNew()) {
  56.                         throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  57.                     }
  58.                 }
  59.                 break;
  60.         }
  61.         return true;
  62.     }
  63. }