Skip to content

Commit 064885f

Browse files
committed
Merge branch 'craft-4' of https://github.com/verbb/wishlist into craft-5
# Conflicts: # CHANGELOG.md # composer.json # src/controllers/BaseController.php # src/controllers/ItemsController.php
2 parents decfebc + a927e69 commit 064885f

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@
115115
### Deprecated
116116
- Deprecated `craft.wishlist.item()`. Use `craft.wishlist.items(params)` to find items, or `craft.wishlist.addItemUrl/toggleItemUrl/removeItemUrl` to manage items.
117117

118+
## 2.0.17 - 2025-09-12
119+
120+
### Added
121+
- Add list-owner enforcement for managing list items from the front-end.
122+
118123
## 2.0.16 - 2025-07-18
119124

120125
### Changed

src/controllers/BaseController.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use craft\web\Controller;
1111

1212
use yii\web\ForbiddenHttpException;
13+
use yii\web\HttpException;
1314
use yii\web\Response;
1415

1516
class BaseController extends Controller
@@ -28,6 +29,44 @@ protected function enforceEnabledList(?ListElement $list): void
2829
}
2930
}
3031

32+
protected function enforceListPermissions(ListElement $list, bool $enforceOwner = true): void
33+
{
34+
if (!$list->getType()) {
35+
Craft::error('Attempting to access a list that doesn’t have a type', __METHOD__);
36+
throw new HttpException(404);
37+
}
38+
39+
// If this is a front-end request, ensure that it's the owner of the list making changes
40+
if ($enforceOwner) {
41+
if (Craft::$app->getRequest()->getIsSiteRequest()) {
42+
$currentUser = Craft::$app->getUser()->getIdentity();
43+
44+
// If an admin, assume they have permission to edit another list
45+
if (Craft::$app->getUser()->getIsAdmin()) {
46+
return;
47+
}
48+
49+
// If logged in, easy check
50+
if ($currentUser) {
51+
if ($currentUser->id !== $list->userId) {
52+
throw new HttpException(403);
53+
}
54+
55+
return;
56+
}
57+
58+
if ($list->sessionId !== Craft::$app->getSession()->get('wishlist_list')) {
59+
// Check if the guests session matches the lists
60+
throw new HttpException(403);
61+
}
62+
63+
return;
64+
}
65+
66+
$this->requirePermission('wishlist-manageListType:' . $list->getType()->uid);
67+
}
68+
}
69+
3170
protected function returnSuccess(string $message, array $params = [], ?object $object = null): Response
3271
{
3372
// Try and determine the action automatically

src/controllers/ItemsController.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,9 @@ public function actionAdd(): ?Response
177177
continue;
178178
}
179179

180+
// Check if we're allowed to manage lists
181+
$this->enforceListPermissions($list);
182+
180183
// Create the item for the list and element, with additional attributes
181184
$item = $this->_getOrCreateItem($list, $element, $postItem);
182185

@@ -246,6 +249,9 @@ public function actionToggle(): ?Response
246249
continue;
247250
}
248251

252+
// Check if we're allowed to manage lists
253+
$this->enforceListPermissions($list);
254+
249255
// Create the item for the list and element, with additional attributes
250256
$item = $this->_getOrCreateItem($list, $element, $postItem);
251257

@@ -312,6 +318,9 @@ public function actionRemove(): ?Response
312318
continue;
313319
}
314320

321+
// Check if we're allowed to manage lists
322+
$this->enforceListPermissions($list);
323+
315324
// Create the item for the list and element, with additional attributes
316325
$item = $this->_getOrCreateItem($list, $element, $postItem);
317326

@@ -373,6 +382,7 @@ public function actionUpdate(): ?Response
373382

374383
// Check if we're allowed to manage lists
375384
$this->enforceEnabledList($item->getList());
385+
$this->enforceListPermissions($item->getList());
376386

377387
$item->setFieldValues($fields);
378388
$item->setOptions($options);
@@ -539,6 +549,7 @@ private function _getOrCreateLists(array $postItem): array
539549

540550
// Check if we're allowed to manage lists
541551
$this->enforceEnabledList($list);
552+
$this->enforceListPermissions($list);
542553

543554
$lists[] = $list;
544555
}

src/controllers/ListsController.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -626,48 +626,6 @@ public function actionDuplicateList(): ?Response
626626
}
627627

628628

629-
// Protected Methods
630-
// =========================================================================
631-
632-
protected function enforceListPermissions(ListElement $list, bool $enforceOwner = true): void
633-
{
634-
if (!$list->getType()) {
635-
Craft::error('Attempting to access a list that doesn’t have a type', __METHOD__);
636-
throw new HttpException(404);
637-
}
638-
639-
// If this is a front-end request, ensure that it's the owner of the list making changes
640-
if ($enforceOwner) {
641-
if (Craft::$app->getRequest()->getIsSiteRequest()) {
642-
$currentUser = Craft::$app->getUser()->getIdentity();
643-
644-
// If an admin, assume they have permission to edit another list
645-
if (Craft::$app->getUser()->getIsAdmin()) {
646-
return;
647-
}
648-
649-
// If logged in, easy check
650-
if ($currentUser) {
651-
if ($currentUser->id !== $list->userId) {
652-
throw new HttpException(403);
653-
}
654-
655-
return;
656-
}
657-
658-
if ($list->sessionId !== Craft::$app->getSession()->get('wishlist_list')) {
659-
// Check if the guests session matches the lists
660-
throw new HttpException(403);
661-
}
662-
663-
return;
664-
}
665-
666-
$this->requirePermission('wishlist-manageListType:' . $list->getType()->uid);
667-
}
668-
}
669-
670-
671629
// Private Methods
672630
// =========================================================================
673631

0 commit comments

Comments
 (0)