custom/plugins/SwagPlatformSecurity/src/Fixes/NEXT15681/SecurityFix.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\Security\Fixes\NEXT15681;
  3. use Shopware\Core\Content\Product\Aggregate\ProductReview\ProductReviewEntity;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\Validation\Exception\ConstraintViolationException;
  7. use Shopware\Core\PlatformRequest;
  8. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  9. use Swag\Security\Components\AbstractSecurityFix;
  10. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Validator\ConstraintViolation;
  13. use Symfony\Component\Validator\ConstraintViolationList;
  14. class SecurityFix extends AbstractSecurityFix
  15. {
  16.     /**
  17.      * @var EntityRepositoryInterface $productReviewRepository
  18.      */
  19.     private $productReviewRepository;
  20.     public function __construct(EntityRepositoryInterface $productReviewRepository)
  21.     {
  22.         $this->productReviewRepository $productReviewRepository;
  23.     }
  24.     public static function getTicket(): string
  25.     {
  26.         return 'NEXT-15681';
  27.     }
  28.     public function onControllerArguments(ControllerArgumentsEvent $event)
  29.     {
  30.         $request $event->getRequest();
  31.         if (!in_array($request->attributes->get('_route'), ['store-api.product-review.save''frontend.detail.review.save'])) {
  32.             return;
  33.         }
  34.         $id $request->get('id');
  35.         if ($id === null || !$request->attributes->has(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT)) {
  36.             return;
  37.         }
  38.         /** @var SalesChannelContext $context */
  39.         $context $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  40.         $criteria = new Criteria([$id]);
  41.         /** @var ProductReviewEntity|null $review */
  42.         $review $this->productReviewRepository->search($criteria$context->getContext())->first();
  43.         if ($review === null) {
  44.             return;
  45.         }
  46.         if ($review->getCustomerId() === $context->getCustomer()->getId()) {
  47.             return;
  48.         }
  49.         throw new ConstraintViolationException(new ConstraintViolationList([new ConstraintViolation(sprintf('Cannot find product_review with id: %s'$id), '', [], '/''/id'$id)]), $request->request->all());
  50.     }
  51.     public static function getMaxVersion(): ?string
  52.     {
  53.         return '6.4.3.1';
  54.     }
  55.     public static function getMinVersion(): string
  56.     {
  57.         return '6.3.2.0';
  58.     }
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments'
  63.         ];
  64.     }
  65. }