Skip to content

Commit d1092da

Browse files
committed
Add list-owner enforcement for managing list items from the front-end
1 parent 5c1cc31 commit d1092da

File tree

3 files changed

+44
-42
lines changed

3 files changed

+44
-42
lines changed

src/controllers/BaseController.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
namespace verbb\wishlist\controllers;
33

44
use verbb\wishlist\Wishlist;
5+
use verbb\wishlist\elements\ListElement;
56
use verbb\wishlist\models\Settings;
67

78
use Craft;
89
use craft\helpers\StringHelper;
910
use craft\web\Controller;
1011

1112
use yii\web\ForbiddenHttpException;
13+
use yii\web\HttpException;
1214
use yii\web\Response;
1315

1416
class BaseController extends Controller
@@ -27,6 +29,44 @@ protected function enforceEnabledList($list): void
2729
}
2830
}
2931

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+
3070
protected function returnSuccess($message, $params = [], $object = null): Response
3171
{
3272
$request = Craft::$app->getRequest();

src/controllers/ItemsController.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public function actionAdd(): ?Response
187187

188188
// Check if we're allowed to manage lists
189189
$this->enforceEnabledList($item->getList());
190+
$this->enforceListPermissions($item->getList());
190191

191192
// Set any additional options on the item
192193
$options = $postItem['options'] ?? [];
@@ -252,6 +253,7 @@ public function actionRemove(): ?Response
252253

253254
// Check if we're allowed to manage lists
254255
$this->enforceEnabledList($list);
256+
$this->enforceListPermissions($list);
255257

256258
$errors = [];
257259

@@ -348,6 +350,7 @@ public function actionToggle(): ?Response
348350

349351
// Check if we're allowed to manage lists
350352
$this->enforceEnabledList($list);
353+
$this->enforceListPermissions($list);
351354

352355
$errors = [];
353356

@@ -457,6 +460,7 @@ public function actionUpdate(): ?Response
457460

458461
// Check if we're allowed to manage lists
459462
$this->enforceEnabledList($item->getList());
463+
$this->enforceListPermissions($item->getList());
460464

461465
$item->setFieldValuesFromRequest('fields');
462466

src/controllers/ListsController.php

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -492,48 +492,6 @@ public function actionShareByEmail(): ?Response
492492
}
493493

494494

495-
// Protected Methods
496-
// =========================================================================
497-
498-
protected function enforceListPermissions(ListElement $list, bool $enforceOwner = true): void
499-
{
500-
if (!$list->getType()) {
501-
Craft::error('Attempting to access a list that doesn’t have a type', __METHOD__);
502-
throw new HttpException(404);
503-
}
504-
505-
// If this is a front-end request, ensure that it's the owner of the list making changes
506-
if ($enforceOwner) {
507-
if (Craft::$app->getRequest()->getIsSiteRequest()) {
508-
$currentUser = Craft::$app->getUser()->getIdentity();
509-
510-
// If an admin, assume they have permission to edit another list
511-
if (Craft::$app->getUser()->getIsAdmin()) {
512-
return;
513-
}
514-
515-
// If logged in, easy check
516-
if ($currentUser) {
517-
if ($currentUser->id !== $list->userId) {
518-
throw new HttpException(403);
519-
}
520-
521-
return;
522-
}
523-
524-
if ($list->sessionId !== Craft::$app->getSession()->get('wishlist_list')) {
525-
// Check if the guests session matches the lists
526-
throw new HttpException(403);
527-
}
528-
529-
return;
530-
}
531-
532-
$this->requirePermission('wishlist-manageListType:' . $list->getType()->uid);
533-
}
534-
}
535-
536-
537495
// Private Methods
538496
// =========================================================================
539497

0 commit comments

Comments
 (0)