Skip to content

Commit

Permalink
Use objectmanager to check if session is already started to prevent p…
Browse files Browse the repository at this point in the history
…remature logout
  • Loading branch information
indykoning committed Jun 6, 2024
2 parents 5640971 + 9647503 commit 8ab279f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
# Changelog
## [3.6.0] - 2024-03-29
This release drops support for php 7.4 as it has been completely EOL for over a year. [https://www.php.net/supported-versions.php](https://www.php.net/supported-versions.php)
### Fixed
* Check if Sentry is defined before running init (https://github.com/justbetter/magento2-sentry/pull/129) thanks to https://github.com/netzkollektiv
### Changed
* Use property promotions (https://github.com/justbetter/magento2-sentry/pull/130) thanks to https://github.com/cirolosapio
* Raised sentry/sdk version to 4.0+
## [3.5.2] - 2024-03-18
### Fixed
* Fix start errors without database connection (https://github.com/justbetter/magento2-sentry/pull/125) thanks to https://github.com/fredden
Expand All @@ -22,7 +29,6 @@
## [3.2.0] - 2022-07-07
### Fixed
* Changed addAlert to addRecord for Test error (https://github.com/justbetter/magento2-sentry/pull/98) thanks to https://github.com/peterjaap

### Added
* Send context data top Sentry as Custom Data (https://github.com/justbetter/magento2-sentry/pull/97) thanks to https://github.com/oneserv-heuser
## [3.1.0] - 2022-06-14
Expand Down
26 changes: 19 additions & 7 deletions Model/SentryInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@

// 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 ReflectionClass;
use Sentry\State\Scope;

use function Sentry\captureException;
use function Sentry\configureScope;
use function Sentry\init;

class SentryInteraction
{
public function __construct(
Expand All @@ -34,7 +34,7 @@ public function initialize($config)
private function canGetUserData()
{
try {
return @$this->appState->getAreaCode();
return in_array(@$this->appState->getAreaCode(), [Area::AREA_ADMINHTML, Area::AREA_FRONTEND]);
} catch (LocalizedException $ex) {
return false;
}
Expand All @@ -47,8 +47,15 @@ private function getSessionUserData()
}

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reflectionClass = new ReflectionClass($objectManager);
$sharedInstances = $reflectionClass->getProperty('_sharedInstances');
$sharedInstances->setAccessible(true);

if ($this->appState->getAreaCode() === Area::AREA_ADMINHTML) {
if (!array_key_exists(ltrim(AdminSession::class, '\\'), $sharedInstances->getValue($objectManager))) {
// Don't intitialise session if it has not already been started, this causes problems with dynamic resources.
return [];
}
$adminSession = $objectManager->get(AdminSession::class);

if ($adminSession->isLoggedIn()) {
Expand All @@ -61,6 +68,9 @@ private function getSessionUserData()
}

if ($this->appState->getAreaCode() === Area::AREA_FRONTEND) {
if (!array_key_exists(ltrim(CustomerSession::class, '\\'), $sharedInstances->getValue($objectManager))) {
return [];
}
$customerSession = $objectManager->get(CustomerSession::class);

if ($customerSession->loggedIn()) {
Expand All @@ -83,15 +93,16 @@ public function addUserContext()
$userType = null;
$userData = [];

\Magento\Framework\Profiler::start('SENTRY::add_user_context');
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;
$userId = $userData['id'] ?? $userId;
$userType = $userData['user_type'] ?? $userType;
unset($userData['user_type']);
}

Expand All @@ -114,6 +125,7 @@ public function addUserContext()
});
} catch (\Throwable $e) {
}
\Magento\Framework\Profiler::stop('SENTRY::add_user_context');
}

public function captureException(\Throwable $ex)
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "justbetter/magento2-sentry",
"description": "Magento 2 Logger for Sentry",
"type": "magento2-module",
"version": "3.5.2",
"version": "3.6.0",
"license": "MIT",
"require": {
"php": ">=8.0",
"sentry/sdk": "^3.0",
"sentry/sdk": "^4.0",
"monolog/monolog": ">=2.7.0",
"magento/framework": "*",
"nyholm/psr7": "^1.2",
Expand Down

0 comments on commit 8ab279f

Please sign in to comment.