Skip to content

Commit

Permalink
Added security check and a small optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 3, 2024
1 parent aae240b commit 0f04f54
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
33 changes: 25 additions & 8 deletions src/Controller/RemoveWishlistItemAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use Doctrine\Persistence\ManagerRegistry;
use Setono\Doctrine\ORMTrait;
use Setono\SyliusWishlistPlugin\Model\UserWishlistInterface;
use Setono\SyliusWishlistPlugin\Model\WishlistInterface;
use Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface;
use Setono\SyliusWishlistPlugin\Security\Voter\WishlistVoter;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand All @@ -19,25 +22,39 @@ final class RemoveWishlistItemAction
public function __construct(
private readonly WishlistProviderInterface $wishlistProvider,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly Security $security,
ManagerRegistry $managerRegistry,
/** @var class-string<UserWishlistInterface> $userWishlistClass */
private readonly string $userWishlistClass,
) {
$this->managerRegistry = $managerRegistry;
}

public function __invoke(string $uuid, int $id): RedirectResponse
{
$wishlist = $this->getWishlist($uuid);
$manager = $this->getManager($this->userWishlistClass);

// todo soooo ugly
foreach ($wishlist->getItems() as $item) {
if ($item->getId() === $id) {
$wishlist->removeItem($item);
/** @var UserWishlistInterface|null $wishlist */
$wishlist = $manager->createQueryBuilder()
->select('o')
->from($this->userWishlistClass, 'o')
->andWhere('o.uuid = :uuid')
->setParameter('uuid', $uuid)
->getQuery()
->getOneOrNullResult()
;

break;
}
if (null === $wishlist) {
throw new NotFoundHttpException(sprintf('Wishlist with uuid %s not found', $uuid));
}

if (!$this->security->isGranted(WishlistVoter::EDIT, $wishlist)) {
throw new NotFoundHttpException(sprintf('Wishlist with uuid %s not found', $uuid));
}

$this->getManager($wishlist)->flush();
$wishlist->removeItem($id);

$manager->flush();

return new RedirectResponse($this->urlGenerator->generate('setono_sylius_wishlist_shop_wishlist_show', [
'uuid' => $uuid,
Expand Down
15 changes: 13 additions & 2 deletions src/Model/Wishlist.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,22 @@ public function addItem(WishlistItemInterface $item): void
}
}

public function removeItem(WishlistItemInterface $item): void
public function removeItem(WishlistItemInterface|int $item): void
{
if ($this->hasItem($item)) {
if ($item instanceof WishlistItemInterface && $this->hasItem($item)) {
$this->items->removeElement($item);
$item->setWishlist(null);

return;
}

foreach ($this->items as $wishlistItem) {
if ($wishlistItem->getId() === $item) {
$this->items->removeElement($wishlistItem);
$wishlistItem->setWishlist(null);

return;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Model/WishlistInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function hasItems(): bool;

public function addItem(WishlistItemInterface $item): void;

public function removeItem(WishlistItemInterface $item): void;
public function removeItem(WishlistItemInterface|int $item): void;

public function hasItem(WishlistItemInterface $item): bool;

Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/services/controller.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
<service id="Setono\SyliusWishlistPlugin\Controller\RemoveWishlistItemAction" public="true">
<argument type="service" id="Setono\SyliusWishlistPlugin\Provider\WishlistProviderInterface"/>
<argument type="service" id="router"/>
<argument type="service" id="Symfony\Bundle\SecurityBundle\Security"/>
<argument type="service" id="doctrine"/>
<argument>%setono_sylius_wishlist.model.user_wishlist.class%</argument>
</service>

<service id="Setono\SyliusWishlistPlugin\Controller\AddWishlistToCartAction" public="true">
Expand Down

0 comments on commit 0f04f54

Please sign in to comment.