src/Security/Voter/Procedure/AuctionVoter.php line 17

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\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 AuctionVoter extends Voter
  13. {
  14.     public const AUCTION_SHOW 'auction_show';
  15.     private Security $security;
  16.     private BidFetcher $bidFetcher;
  17.     public function __construct(
  18.         Security $security,
  19.         BidFetcher $bidFetcher
  20.     ) {
  21.         $this->security $security;
  22.         $this->bidFetcher $bidFetcher;
  23.     }
  24.     protected function supports(string $attribute$subject): bool
  25.     {
  26.         return in_array($attribute, [
  27.             self::AUCTION_SHOW
  28.         ], true);
  29.     }
  30.     /**
  31.      * @param string $attribute
  32.      * @param DetailView $subject
  33.      * @param TokenInterface $token
  34.      * @return bool
  35.      * @throws \Doctrine\DBAL\Exception
  36.      */
  37.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  38.     {
  39.         // Moderators have full access
  40.         if ($this->security->isGranted('ROLE_MODERATOR')) {
  41.             return true;
  42.         }
  43.         $user $token->getUser();
  44.         if (!$user instanceof UserIdentity) {
  45.             return false;
  46.         }
  47.         /*** Список разрешенных профиль ид*/
  48.         $accessProfileIds = [];
  49.         $accessProfileIds[] = $subject->profile_id;
  50.         $findBids $this->bidFetcher->findAllBidsByLot(
  51.             $subject->lot_id,
  52.             [
  53.                 Status::approved()->getName()
  54.             ]
  55.         );
  56.         $participantIds array_map(function ($bid) {
  57.             return $bid->getParticipantId();
  58.         }, $findBids);
  59.         $accessProfileIds array_merge($accessProfileIds$participantIds);
  60.         if (in_array($user->getProfileId(), $accessProfileIds) === false) {
  61.             throw new AccessDeniedException('Доступ запрещен. У вас недостаточно прав для совершения этого действия.');
  62.         }
  63.         return true;
  64.     }
  65. }