From 7dfd95a2291ccaa813cd5c0912f6b9386b56ae2d Mon Sep 17 00:00:00 2001 From: Felix Gehrmann Date: Tue, 23 Mar 2021 18:07:58 +0100 Subject: [PATCH 1/6] Fix feedback verification for guests --- .../views/Widgets/FeedbackOrderWidget.twig | 4 +- src/Services/FeedbackService.php | 96 +++++++++++-------- 2 files changed, 56 insertions(+), 44 deletions(-) diff --git a/resources/views/Widgets/FeedbackOrderWidget.twig b/resources/views/Widgets/FeedbackOrderWidget.twig index d7960df7..b64ae92d 100644 --- a/resources/views/Widgets/FeedbackOrderWidget.twig +++ b/resources/views/Widgets/FeedbackOrderWidget.twig @@ -17,8 +17,8 @@ :item-images="{{ Twig.print("data.itemImages | json_encode") }}" :options="{{ options | json_encode }}" :split-item-bundles="{{ Twig.print("splitItemBundle | json_encode") }}" - access-key="{{ request.get("accessKey") }}" - order-id="{{ request.get("orderId") }}"> + access-key="{{ Twig.print("feedbackServices.feedback.getOrderAccessKey(data.order.id)") }}" + order-id="{{ Twig.print("data.order.id | json_encode") }}"> {{ Twig.elseif("#{ isPreview | json_encode } and not urls.is('confirmation')") }} diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index ba2f1735..f9948f5a 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -46,7 +46,8 @@ public function __construct( FeedbackAverageRepositoryContract $feedbackAverageRepository, AccountService $accountService, LocalizationRepositoryContract $localizationRepository - ) { + ) + { $this->request = $request; $this->coreHelper = $coreHelper; $this->feedbackRepository = $feedbackRepository; @@ -133,16 +134,16 @@ function () use ($accountService) { return $accountService->getAccountContactId(); } ); + $order = null; // Check if accessKey for order is available - if ($creatorContactId <= 0) - { + if ($creatorContactId <= 0) { $orderId = $this->request->input('orderId'); $accessKey = $this->request->input('accessKey'); - if(strlen($orderId) && strlen($accessKey)) - { - $creatorContactId = $this->getUserIdFromOrder($orderId, $accessKey); + if ($orderId != '' && $accessKey != '') { + $order = $this->getOrder($orderId, $accessKey); + $creatorContactId = $this->getUserIdFromOrder($order); } } @@ -183,8 +184,8 @@ function () use ($accountService) { } // The following checks cannot be applied to guests - if ($creatorContactId != 0) { - $hasPurchased = $this->hasPurchasedVariation($creatorContactId, $this->request->input('targetId'), $allowFeedbacksOnlyIfPurchased); + if ($creatorContactId != 0 || !is_null($order)) { + $hasPurchased = !is_null($order) || $this->hasPurchasedVariation($creatorContactId, $this->request->input('targetId')); if ($allowFeedbacksOnlyIfPurchased && !$hasPurchased) { return 'Not allowed to create review without purchasing the item first'; @@ -196,22 +197,22 @@ function () use ($accountService) { 'feedbackRelationSourceId' => $options['feedbackRelationTargetId'] ]; } + } - if (!empty($numberOfFeedbacks) && $numberOfFeedbacks != 0) { - // Get the feedbacks that this user created on this item - $countFeedbacksOfUserPerItem = $this->listFeedbacks( - 1, - 50, - [], - [ - 'sourceId' => $creatorContactId, - 'targetId' => $options['feedbackRelationTargetId'] - ] - )->getTotalCount(); - - if ($countFeedbacksOfUserPerItem >= $numberOfFeedbacks) { - return 'Too many reviews'; - } + if (!empty($numberOfFeedbacks) && $numberOfFeedbacks != 0 && $creatorContactId != 0) { + // Get the feedbacks that this user created on this item + $countFeedbacksOfUserPerItem = $this->listFeedbacks( + 1, + 50, + [], + [ + 'sourceId' => $creatorContactId, + 'targetId' => $options['feedbackRelationTargetId'] + ] + )->getTotalCount(); + + if ($countFeedbacksOfUserPerItem >= $numberOfFeedbacks) { + return 'Too many reviews'; } } @@ -394,14 +395,12 @@ public function getAuthenticatedUserMulti($itemIds = [], $variationIds = []) $contactId = $this->accountService->getAccountContactId(); - if(!$contactId) - { + if (!$contactId) { // Check for accessKey $accessKey = $this->request->input("accessKey"); $orderId = $this->request->input("orderId"); - if(strlen($orderId) && strlen($accessKey)) - { + if (strlen($orderId) && strlen($accessKey)) { $contactId = $this->getUserIdFromOrder($orderId, $accessKey); } } @@ -414,7 +413,7 @@ public function getAuthenticatedUserMulti($itemIds = [], $variationIds = []) if (count($variationIds)) { if ($isLoggedIn && $allowFeedbacksOnlyIfPurchased) { foreach ($variationIds as $variationId) { - $hasPurchased[$variationId] = $this->hasPurchasedVariation($contactId, $variationId, $allowFeedbacksOnlyIfPurchased); + $hasPurchased[$variationId] = $this->hasPurchasedVariation($contactId, $variationId); } } else { foreach ($variationIds as $variationId) { @@ -482,6 +481,12 @@ public function getAuthenticatedUser($itemId, $variationId) return $result; } + public function getOrderAccessKey($orderId) + { + $orderRepository = pluginApp(OrderRepositoryContract::class); + return $orderRepository->generateAccessKey($orderId); + } + /** * Calculate if the user has reached the maximum amount of feedbacks for the given itemId * @param $itemId @@ -518,24 +523,22 @@ private function isFeedbackLimitReached($itemId, $contactId, $numberOfFeedbacks) * @param $mandatoryPurchase * @return bool */ - private function hasPurchasedVariation($contactId, $variationId, $mandatoryPurchase) + private function hasPurchasedVariation($contactId, $variationId) { $hasPurchased = false; - if ($mandatoryPurchase) { - $orderRepository = pluginApp(OrderRepositoryContract::class); - $orders = $orderRepository->allOrdersByContact($contactId); - $purchasedVariations = []; + $orderRepository = pluginApp(OrderRepositoryContract::class); + $orders = $orderRepository->allOrdersByContact($contactId); + $purchasedVariations = []; - foreach ($orders->getResult() as $order) { - foreach ($order->orderItems as $orderItem) { - $purchasedVariations[] = $orderItem->itemVariationId; - } + foreach ($orders->getResult() as $order) { + foreach ($order->orderItems as $orderItem) { + $purchasedVariations[] = $orderItem->itemVariationId; } - - $hasPurchased = in_array($variationId, $purchasedVariations); } + $hasPurchased = in_array($variationId, $purchasedVariations); + return $hasPurchased; } @@ -592,14 +595,23 @@ private function determineVisibility($releaseLevel, $creatorId = 1) || $releaseLevel === self::RELEASE_LEVEL_ALL; } - private function getUserIdFromOrder($orderId, $accessKey) + /** + * @param $orderId + * @param $accessKey + * @return \Order|null + */ + private function getOrder($orderId, $accessKey) { /** @var OrderRepositoryContract $orderRepository */ $orderRepository = pluginApp(OrderRepositoryContract::class); $order = $orderRepository->findOrderByAccessKey($orderId, $accessKey); - if ($order !== null) - { + return $order; + } + + private function getUserIdFromOrder($order) + { + if ($order !== null) { foreach ($order->relations as $relation) { if ($relation['referenceType'] === 'contact' && (int)$relation['referenceId'] > 0) { return $relation['referenceId']; From c70fd932af8e1cea2a7a672496a029b4b6ae1f2b Mon Sep 17 00:00:00 2001 From: Felix Gehrmann Date: Wed, 24 Mar 2021 10:46:47 +0100 Subject: [PATCH 2/6] Fix build error --- src/Services/FeedbackService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index f9948f5a..0cd102f0 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -401,7 +401,8 @@ public function getAuthenticatedUserMulti($itemIds = [], $variationIds = []) $orderId = $this->request->input("orderId"); if (strlen($orderId) && strlen($accessKey)) { - $contactId = $this->getUserIdFromOrder($orderId, $accessKey); + $order = $this->getOrder($orderId, $accessKey); + $contactId = $this->getUserIdFromOrder($order); } } From 5a913e153fb17c136dc0b61deae196e6a77a7c55 Mon Sep 17 00:00:00 2001 From: Felix Gehrmann Date: Wed, 24 Mar 2021 10:50:35 +0100 Subject: [PATCH 3/6] Add changelogs --- meta/documents/changelog_de.md | 11 +++++++++++ meta/documents/changelog_en.md | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/meta/documents/changelog_de.md b/meta/documents/changelog_de.md index e829e9a5..f94eb4d4 100644 --- a/meta/documents/changelog_de.md +++ b/meta/documents/changelog_de.md @@ -1,5 +1,16 @@ # Release Notes für Feedback +## v3.6.3 (2021-X-X) + +### TODO + +- Zur Nutzung des Feedback-Plugins muss Ceres in Version 5.0 oder höher installiert sein. +- Nach dem Update auf Version 3.6.3 müssen Widgets im Menü **CMS » ShopBuilder** durch Klick auf **Inhalte neu generieren** aktualisiert werden. + +### Behoben + +- Bewertungen von Gastbestellungen konnten nicht verifiziert werden. Dies wurde behoben. + ## v3.6.2 (2021-03-03) ### TODO diff --git a/meta/documents/changelog_en.md b/meta/documents/changelog_en.md index c880afa8..44cb71d9 100644 --- a/meta/documents/changelog_en.md +++ b/meta/documents/changelog_en.md @@ -1,5 +1,17 @@ # Release Notes for Feedback +## v3.6.3 (2021-X-X) + +### TODO + +- This version of the feedback plugin is only compatible with Ceres v5.0 or higher. +- After updating the feedback plugin to v3.6.3, it is necessary to re-generate ShopBuilder widgets via the **Regenerate contents** button in the **CMS » ShopBuilder** menu. + +### Fixed + +- Feedbacks could not be verified, when written from a guests order confirmation. This has been fixed. + + ## v3.6.2 (2021-03-03) ### TODO From 721e6bb142c63ec3b80f09879828d357dcd6f3c2 Mon Sep 17 00:00:00 2001 From: Felix Gehrmann Date: Fri, 26 Mar 2021 14:09:32 +0100 Subject: [PATCH 4/6] use auth guard, fix edge case for guest --- resources/views/Widgets/FeedbackOrderWidget.twig | 2 +- src/Services/FeedbackService.php | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/resources/views/Widgets/FeedbackOrderWidget.twig b/resources/views/Widgets/FeedbackOrderWidget.twig index b64ae92d..bab4c49f 100644 --- a/resources/views/Widgets/FeedbackOrderWidget.twig +++ b/resources/views/Widgets/FeedbackOrderWidget.twig @@ -17,7 +17,7 @@ :item-images="{{ Twig.print("data.itemImages | json_encode") }}" :options="{{ options | json_encode }}" :split-item-bundles="{{ Twig.print("splitItemBundle | json_encode") }}" - access-key="{{ Twig.print("feedbackServices.feedback.getOrderAccessKey(data.order.id)") }}" + access-key="{{ Twig.print(Twig.call("feedbackServices.feedback.getOrderAccessKey", [Twig.var("data.order.id")])) }}" order-id="{{ Twig.print("data.order.id | json_encode") }}"> diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 0cd102f0..24adabc8 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -149,7 +149,7 @@ function () use ($accountService) { $allowGuestFeedbacks = $this->coreHelper->configValueAsBool(FeedbackCoreHelper::KEY_ALLOW_GUEST_FEEDBACKS); - if (!$allowGuestFeedbacks && $creatorContactId == 0) { + if (!$allowGuestFeedbacks && $creatorContactId == 0 && $order == null) { return 'Guests are not allowed to write feedbacks'; } @@ -485,7 +485,15 @@ public function getAuthenticatedUser($itemId, $variationId) public function getOrderAccessKey($orderId) { $orderRepository = pluginApp(OrderRepositoryContract::class); - return $orderRepository->generateAccessKey($orderId); + $authHelper = pluginApp(AuthHelper::class); + + $accessKey = $authHelper->processUnguarded( + function () use ($orderRepository, $orderId) { + return $orderRepository->generateAccessKey($orderId); + } + ); + + return $accessKey; } /** From c124bc2522b7aad291f3843bac2ac6deffcf9daf Mon Sep 17 00:00:00 2001 From: Felix Gehrmann Date: Fri, 26 Mar 2021 14:13:49 +0100 Subject: [PATCH 5/6] changelogs and plugin json --- meta/documents/changelog_de.md | 3 ++- meta/documents/changelog_en.md | 4 +++- plugin.json | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/meta/documents/changelog_de.md b/meta/documents/changelog_de.md index f94eb4d4..1549ba30 100644 --- a/meta/documents/changelog_de.md +++ b/meta/documents/changelog_de.md @@ -1,6 +1,6 @@ # Release Notes für Feedback -## v3.6.3 (2021-X-X) +## v3.6.3 (2021-03-26) ### TODO @@ -10,6 +10,7 @@ ### Behoben - Bewertungen von Gastbestellungen konnten nicht verifiziert werden. Dies wurde behoben. +- Durch fehlerhaft gecachte Daten wurden Kundenbewertungen auf der Bestellbestätigung teilweise falsch zugeordnet. ## v3.6.2 (2021-03-03) diff --git a/meta/documents/changelog_en.md b/meta/documents/changelog_en.md index 44cb71d9..c12ca468 100644 --- a/meta/documents/changelog_en.md +++ b/meta/documents/changelog_en.md @@ -1,6 +1,6 @@ # Release Notes for Feedback -## v3.6.3 (2021-X-X) +## v3.6.3 (2021-03-26) ### TODO @@ -10,6 +10,8 @@ ### Fixed - Feedbacks could not be verified, when written from a guests order confirmation. This has been fixed. +- Incorrectly cached data lead to wrongly linked feedbacks on the order confirmation. + ## v3.6.2 (2021-03-03) diff --git a/plugin.json b/plugin.json index 67ee8d62..07021fb4 100644 --- a/plugin.json +++ b/plugin.json @@ -1,5 +1,5 @@ { - "version": "3.6.2", + "version": "3.6.3", "name": "Feedback", "marketplaceName": { "de": "Kunden-Feedback", From 3999ea7295b5c6e2c3a3eb4c4d293e4c9e76a165 Mon Sep 17 00:00:00 2001 From: Felix Gehrmann Date: Fri, 26 Mar 2021 14:17:59 +0100 Subject: [PATCH 6/6] lower db load via pagination --- src/Services/FeedbackService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Services/FeedbackService.php b/src/Services/FeedbackService.php index 24adabc8..e420905c 100644 --- a/src/Services/FeedbackService.php +++ b/src/Services/FeedbackService.php @@ -203,7 +203,7 @@ function () use ($accountService) { // Get the feedbacks that this user created on this item $countFeedbacksOfUserPerItem = $this->listFeedbacks( 1, - 50, + 1, [], [ 'sourceId' => $creatorContactId,