From e0426201c5adfc5e1a4b27dd6d6edde4c3030e50 Mon Sep 17 00:00:00 2001 From: indy koning Date: Thu, 28 Mar 2024 16:04:16 +0100 Subject: [PATCH 1/5] Add user context whenever possible --- Model/SentryInteraction.php | 98 +++++++++++++++++++++++++++++++ Model/SentryLog.php | 24 +------- Plugin/GlobalExceptionCatcher.php | 1 + 3 files changed, 102 insertions(+), 21 deletions(-) diff --git a/Model/SentryInteraction.php b/Model/SentryInteraction.php index ea484f4..290a44d 100644 --- a/Model/SentryInteraction.php +++ b/Model/SentryInteraction.php @@ -7,15 +7,113 @@ // phpcs:disable Magento2.Functions.DiscouragedFunction use function Sentry\captureException; +use function Sentry\configureScope; use function Sentry\init; +use Magento\Authorization\Model\UserContextInterface; +use Magento\Backend\Model\Auth\Session as AdminSession; +use Magento\Customer\Model\Session as CustomerSession; +use Magento\Framework\App\Area; +use Magento\Framework\App\State; +use Magento\Framework\Exception\LocalizedException; +use Sentry\State\Scope; class SentryInteraction { + public function __construct( + private UserContextInterface $userContext, + private State $appState + ) { } + public function initialize($config) { init($config); } + private function canGetUserData() + { + try { + return @$this->appState->getAreaCode(); + } catch (LocalizedException $ex) { + return false; + } + } + + private function getSessionUserData() + { + if (!$this->canGetUserData()) { + return []; + } + + $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); + + if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML) { + $adminSession = $objectManager->get(AdminSession::class); + + if($adminSession->isLoggedIn()) { + return [ + 'id' => $adminSession->getUser()->getId(), + 'email' => $adminSession->getUser()->getEmail(), + 'user_type' => UserContextInterface::USER_TYPE_ADMIN, + ]; + } + } + + if ($this->appState->getAreaCode() === Area::AREA_FRONTEND) { + $customerSession = $objectManager->get(CustomerSession::class); + + if($customerSession->loggedIn()) { + return [ + 'id' => $customerSession->getCustomer()->getId(), + 'email' => $customerSession->getCustomer()->getEmail(), + 'website_id' => $customerSession->getCustomer()->getWebsiteId(), + 'store_id' => $customerSession->getCustomer()->getStoreId(), + 'user_type' => UserContextInterface::USER_TYPE_CUSTOMER, + ]; + } + } + + return []; + } + + public function addUserContext() + { + $userId = null; + $userType = null; + $userData = []; + try { + $userId = $this->userContext->getUserId(); + if ($userId) { + $userType = $this->userContext->getUserType(); + } + + if ($this->canGetUserData() && count($userData = $this->getSessionUserData())) { + $userId = $userData['id'] || $userId; + $userType = $userData['user_type'] || $userType; + unset($userData['user_type']); + } + + if (!$userId) { + return; + } + + configureScope(function (Scope $scope) use ($userType, $userId, $userData) { + $scope->setUser([ + 'id' => $userId, + ...$userData, + 'user_type' => match($userType) { + UserContextInterface::USER_TYPE_INTEGRATION => 'integration', + UserContextInterface::USER_TYPE_ADMIN => 'admin', + UserContextInterface::USER_TYPE_CUSTOMER => 'customer', + UserContextInterface::USER_TYPE_GUEST => 'guest', + default => 'unknown' + }, + ]); + dump($scope->getUser()); + }); + } catch (\Throwable $e) { + } + } + public function captureException(\Throwable $ex) { ob_start(); diff --git a/Model/SentryLog.php b/Model/SentryLog.php index 3d1a169..1000ebc 100755 --- a/Model/SentryLog.php +++ b/Model/SentryLog.php @@ -45,6 +45,7 @@ public function __construct( Data $data, Session $customerSession, State $appState, + private SentryInteraction $sentryInteraction, array $handlers = [], array $processors = [] ) { @@ -77,13 +78,14 @@ public function send($message, $logLevel, $context = []) \Sentry\configureScope( function (SentryScope $scope) use ($context, $customTags): void { $this->setTags($scope, $customTags); - $this->setUser($scope); if (false === empty($context)) { $scope->setContext('Custom context', $context); } } ); + $this->sentryInteraction->addUserContext(); + if ($message instanceof \Throwable) { $lastEventId = \Sentry\captureException($message); } else { @@ -100,26 +102,6 @@ function (SentryScope $scope) use ($context, $customTags): void { } } - private function setUser(SentryScope $scope): void - { - try { - if (!$this->canGetCustomerData() - || !$this->customerSession->isLoggedIn()) { - return; - } - - $customerData = $this->customerSession->getCustomer(); - $scope->setUser([ - 'id' => $customerData->getEntityId(), - 'email' => $customerData->getEmail(), - 'website_id' => $customerData->getWebsiteId(), - 'store_id' => $customerData->getStoreId(), - ]); - } catch (SessionException $e) { - return; - } - } - private function canGetCustomerData() { try { diff --git a/Plugin/GlobalExceptionCatcher.php b/Plugin/GlobalExceptionCatcher.php index 85ee3ef..8756a61 100755 --- a/Plugin/GlobalExceptionCatcher.php +++ b/Plugin/GlobalExceptionCatcher.php @@ -90,6 +90,7 @@ public function aroundLaunch(AppInterface $subject, callable $proceed) } catch (\Throwable $ex) { try { if ($this->sentryHelper->shouldCaptureException($ex)) { + $this->sentryInteraction->addUserContext(); $this->sentryInteraction->captureException($ex); } } catch (\Throwable $bigProblem) { From 742351020da59b83d59eb6f436926cc525c77677 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 28 Mar 2024 15:04:34 +0000 Subject: [PATCH 2/5] Apply fixes from StyleCI --- Model/SentryInteraction.php | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/Model/SentryInteraction.php b/Model/SentryInteraction.php index 290a44d..257e3ed 100644 --- a/Model/SentryInteraction.php +++ b/Model/SentryInteraction.php @@ -6,9 +6,6 @@ // phpcs:disable Magento2.Functions.DiscouragedFunction -use function Sentry\captureException; -use function Sentry\configureScope; -use function Sentry\init; use Magento\Authorization\Model\UserContextInterface; use Magento\Backend\Model\Auth\Session as AdminSession; use Magento\Customer\Model\Session as CustomerSession; @@ -17,12 +14,17 @@ use Magento\Framework\Exception\LocalizedException; use Sentry\State\Scope; +use function Sentry\captureException; +use function Sentry\configureScope; +use function Sentry\init; + class SentryInteraction { public function __construct( private UserContextInterface $userContext, private State $appState - ) { } + ) { + } public function initialize($config) { @@ -49,10 +51,10 @@ private function getSessionUserData() if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML) { $adminSession = $objectManager->get(AdminSession::class); - if($adminSession->isLoggedIn()) { + if ($adminSession->isLoggedIn()) { return [ - 'id' => $adminSession->getUser()->getId(), - 'email' => $adminSession->getUser()->getEmail(), + 'id' => $adminSession->getUser()->getId(), + 'email' => $adminSession->getUser()->getEmail(), 'user_type' => UserContextInterface::USER_TYPE_ADMIN, ]; } @@ -61,13 +63,13 @@ private function getSessionUserData() if ($this->appState->getAreaCode() === Area::AREA_FRONTEND) { $customerSession = $objectManager->get(CustomerSession::class); - if($customerSession->loggedIn()) { + if ($customerSession->loggedIn()) { return [ - 'id' => $customerSession->getCustomer()->getId(), - 'email' => $customerSession->getCustomer()->getEmail(), + 'id' => $customerSession->getCustomer()->getId(), + 'email' => $customerSession->getCustomer()->getEmail(), 'website_id' => $customerSession->getCustomer()->getWebsiteId(), 'store_id' => $customerSession->getCustomer()->getStoreId(), - 'user_type' => UserContextInterface::USER_TYPE_CUSTOMER, + 'user_type' => UserContextInterface::USER_TYPE_CUSTOMER, ]; } } @@ -80,6 +82,7 @@ public function addUserContext() $userId = null; $userType = null; $userData = []; + try { $userId = $this->userContext->getUserId(); if ($userId) { @@ -100,7 +103,7 @@ public function addUserContext() $scope->setUser([ 'id' => $userId, ...$userData, - 'user_type' => match($userType) { + 'user_type' => match ($userType) { UserContextInterface::USER_TYPE_INTEGRATION => 'integration', UserContextInterface::USER_TYPE_ADMIN => 'admin', UserContextInterface::USER_TYPE_CUSTOMER => 'customer', From ce3ff957f17cf9ce7d2b8ceb73d57687ce4d3c63 Mon Sep 17 00:00:00 2001 From: indy koning Date: Thu, 28 Mar 2024 16:13:18 +0100 Subject: [PATCH 3/5] Remove debugging code --- Model/SentryInteraction.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Model/SentryInteraction.php b/Model/SentryInteraction.php index 290a44d..1b5e4b9 100644 --- a/Model/SentryInteraction.php +++ b/Model/SentryInteraction.php @@ -51,8 +51,8 @@ private function getSessionUserData() if($adminSession->isLoggedIn()) { return [ - 'id' => $adminSession->getUser()->getId(), - 'email' => $adminSession->getUser()->getEmail(), + 'id' => $adminSession->getUser()->getId(), + 'email' => $adminSession->getUser()->getEmail(), 'user_type' => UserContextInterface::USER_TYPE_ADMIN, ]; } @@ -63,11 +63,11 @@ private function getSessionUserData() if($customerSession->loggedIn()) { return [ - 'id' => $customerSession->getCustomer()->getId(), - 'email' => $customerSession->getCustomer()->getEmail(), + 'id' => $customerSession->getCustomer()->getId(), + 'email' => $customerSession->getCustomer()->getEmail(), 'website_id' => $customerSession->getCustomer()->getWebsiteId(), 'store_id' => $customerSession->getCustomer()->getStoreId(), - 'user_type' => UserContextInterface::USER_TYPE_CUSTOMER, + 'user_type' => UserContextInterface::USER_TYPE_CUSTOMER, ]; } } @@ -108,7 +108,6 @@ public function addUserContext() default => 'unknown' }, ]); - dump($scope->getUser()); }); } catch (\Throwable $e) { } From fa3c1d2556ef376cde55932eb482349d85864d5f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 6 Jun 2024 13:08:57 +0000 Subject: [PATCH 4/5] Apply fixes from StyleCI --- Model/SentryInteraction.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Model/SentryInteraction.php b/Model/SentryInteraction.php index 61800eb..8ea9c11 100644 --- a/Model/SentryInteraction.php +++ b/Model/SentryInteraction.php @@ -6,9 +6,6 @@ // phpcs:disable Magento2.Functions.DiscouragedFunction -use function Sentry\captureException; -use function Sentry\configureScope; -use function Sentry\init; use Magento\Authorization\Model\UserContextInterface; use Magento\Backend\Model\Auth\Session as AdminSession; use Magento\Customer\Model\Session as CustomerSession; @@ -18,6 +15,10 @@ use ReflectionClass; use Sentry\State\Scope; +use function Sentry\captureException; +use function Sentry\configureScope; +use function Sentry\init; + class SentryInteraction { public function __construct( @@ -94,6 +95,7 @@ public function addUserContext() $userData = []; \Magento\Framework\Profiler::start('SENTRY::add_user_context'); + try { $userId = $this->userContext->getUserId(); if ($userId) { From 704efd59ea707fd23d9960c60a8cd6fabbf0b25e Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Wed, 6 Nov 2024 13:27:27 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- Model/SentryInteraction.php | 6 +++--- Model/SentryLog.php | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Model/SentryInteraction.php b/Model/SentryInteraction.php index 50ff30d..44e7471 100644 --- a/Model/SentryInteraction.php +++ b/Model/SentryInteraction.php @@ -24,8 +24,8 @@ class SentryInteraction /** * SentryInteraction constructor. * - * @param UserContextInterface $userContext - * @param State $appState + * @param UserContextInterface $userContext + * @param State $appState */ public function __construct( private UserContextInterface $userContext, @@ -156,7 +156,7 @@ public function addUserContext() } \Magento\Framework\Profiler::stop('SENTRY::add_user_context'); } - + /** * Capture passed exception. * diff --git a/Model/SentryLog.php b/Model/SentryLog.php index 4a8265f..54eb3ea 100755 --- a/Model/SentryLog.php +++ b/Model/SentryLog.php @@ -3,7 +3,6 @@ namespace JustBetter\Sentry\Model; use JustBetter\Sentry\Helper\Data; -use JustBetter\Sentry\Model\SentryInteraction; use Magento\Customer\Model\Session; use Magento\Framework\App\Area; use Magento\Framework\App\State;