src/Security/Voter/Procedure/RebidVoter.php line 16

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter\Procedure;
  4. use App\Model\Work\Procedure\Entity\Lot\Bid\Status;
  5. use App\ReadModel\Procedure\Bid\BidFetcher;
  6. use App\ReadModel\Procedure\Lot\DetailView;
  7. use App\Security\UserIdentity;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  11. use Symfony\Component\Security\Core\Security;
  12. class RebidVoter extends Voter
  13. {
  14.     public const REBID_LIST 'rebid_list';
  15.     public const REBID_SHOW 'rebid_show';
  16.     public const REBID_APPLY 'rebid_apply';
  17.     public const REBID_PROTOCOL_DOCUMENT_UPLOAD 'rebid_protocol_document_upload';
  18.     private BidFetcher $bidFetcher;
  19.     private Security $security;
  20.     public function __construct(Security $securityBidFetcher $bidFetcher)
  21.     {
  22.         $this->security $security;
  23.         $this->bidFetcher $bidFetcher;
  24.     }
  25.     protected function supports(string $attribute$subject): bool
  26.     {
  27.         return in_array($attribute, [
  28.             self::REBID_SHOW,
  29.             self::REBID_APPLY,
  30.             self::REBID_LIST,
  31.             self::REBID_PROTOCOL_DOCUMENT_UPLOAD
  32.         ], true);
  33.     }
  34.     /**
  35.      * @param string $attribute
  36.      * @param DetailView $subject
  37.      * @param TokenInterface $token
  38.      * @return bool
  39.      */
  40.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  41.     {
  42.         // Moderators have full access
  43.         if ($this->security->isGranted('ROLE_MODERATOR')) {
  44.             return true;
  45.         }
  46.         $user $token->getUser();
  47.         // Only authenticated UserIdentity can proceed
  48.         if (!$user instanceof UserIdentity) {
  49.             return false;
  50.         }
  51.         if ($user->getProfileId() === null) {
  52.             throw new AccessDeniedException('Профиль не найден. У вас недостаточно прав для совершения этого действия.');
  53.         }
  54.         /*** Список разрешенных профиль ид*/
  55.         $accessProfileIds = [];
  56.         $accessProfileIds[] = $subject->organizer_profile_id;
  57.         $findBids $this->bidFetcher->findAllBidsByLot(
  58.             $subject->id,
  59.             [
  60.                 Status::sent()->getName(),
  61.                 Status::approved()->getName()
  62.             ]
  63.         );
  64.         $participantIds array_map(function ($bid) {
  65.             return $bid->getParticipantId();
  66.         }, $findBids);
  67.         $accessProfileIds array_merge($accessProfileIds$participantIds);
  68.         if (in_array($user->getProfileId(), $accessProfileIds) === false) {
  69.             throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  70.         }
  71.         switch ($attribute) {
  72.             case self::REBID_APPLY:
  73.                 //is owner procedure
  74.                 if ($subject->organizer_profile_id === $user->getProfileId()) {
  75.                     throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  76.                 }
  77.                 break;
  78.             case self::REBID_PROTOCOL_DOCUMENT_UPLOAD:
  79.             case self::REBID_LIST:
  80.                 //is not owner procedure
  81.                 if ($subject->organizer_profile_id !== $user->getProfileId()) {
  82.                     throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  83.                 }
  84.                 break;
  85.         }
  86.         return true;
  87.     }
  88. }