From 8b1edb75c7cd5d8225d69cd935024214483c9616 Mon Sep 17 00:00:00 2001 From: Stephen Senjaya Date: Fri, 19 Jan 2024 16:22:21 +0700 Subject: [PATCH 1/7] MEL-1509 change session to laravel session --- src/Data/Repositories/Session.php | 5 ++--- src/Vendor/Laravel/ServiceProvider.php | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Data/Repositories/Session.php b/src/Data/Repositories/Session.php index c4f05db2..b4dfafa4 100644 --- a/src/Data/Repositories/Session.php +++ b/src/Data/Repositories/Session.php @@ -4,7 +4,6 @@ use Carbon\Carbon; use PragmaRX\Support\Config; -use PragmaRX\Support\PhpSession; use Ramsey\Uuid\Uuid as UUID; class Session extends Repository @@ -17,11 +16,11 @@ class Session extends Repository protected $relations = ['device', 'user', 'log', 'language', 'agent', 'referer', 'geoIp', 'cookie']; - public function __construct($model, Config $config, PhpSession $session) + public function __construct($model, Config $config) { $this->config = $config; - $this->session = $session; + $this->session = session(); parent::__construct($model); } diff --git a/src/Vendor/Laravel/ServiceProvider.php b/src/Vendor/Laravel/ServiceProvider.php index b9d62bf5..c9689b6d 100644 --- a/src/Vendor/Laravel/ServiceProvider.php +++ b/src/Vendor/Laravel/ServiceProvider.php @@ -280,8 +280,7 @@ public function registerRepositories() $app['tracker.config'], new Session( $sessionModel, - $app['tracker.config'], - new PhpSession() + $app['tracker.config'] ), $logRepository, new Path($pathModel), From 283e18b8ba064629829fff7a41997f6a54849b5e Mon Sep 17 00:00:00 2001 From: Stephen Senjaya Date: Wed, 24 Apr 2024 14:04:06 +0700 Subject: [PATCH 2/7] add status on session --- src/Data/Repositories/Session.php | 6 +++++- src/Tracker.php | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Data/Repositories/Session.php b/src/Data/Repositories/Session.php index b4dfafa4..9af56b82 100644 --- a/src/Data/Repositories/Session.php +++ b/src/Data/Repositories/Session.php @@ -69,6 +69,10 @@ private function sessionIsReliable() { $data = $this->getSessionData(); + if(!isset($data['status']) || $data['status'] == 0){ + return false; + } + if (isset($data['user_id']) && isset($data['user_type'])) { if ($data['user_id'] !== $this->sessionInfo['user_id'] && $data['user_type'] !== $this->sessionInfo['user_type']) { return false; @@ -136,7 +140,7 @@ private function ensureSessionDataIsComplete() if (in_array($key, ['user_agent', 'impersonation_id'])) { continue; } - if ($sessionData[$key] !== $value) { + if (!isset($sessionData[$key]) || $sessionData[$key] !== $value) { if (!isset($model)) { $model = $this->find($this->sessionInfo['id']); } diff --git a/src/Tracker.php b/src/Tracker.php index 8f36b96d..fff5495c 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -255,6 +255,7 @@ protected function makeSessionData() // it's internally used to check if the user agent changed // during a session. 'user_agent' => $this->dataRepositoryManager->getCurrentUserAgent(), + 'status' => 1, ]; $authSessionPrefix = $this->config->get('auth_session_prefix'); From 426d91880bc6f726cc75e0a1e61001b4498ed674 Mon Sep 17 00:00:00 2001 From: Dina Date: Wed, 8 May 2024 11:42:04 +0900 Subject: [PATCH 3/7] ability to execute auth method on multiple guards --- src/Services/Authentication.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/Services/Authentication.php b/src/Services/Authentication.php index f62514d2..b83de2ea 100644 --- a/src/Services/Authentication.php +++ b/src/Services/Authentication.php @@ -33,16 +33,17 @@ private function executeAuthMethod($method) $guards[] = null; } - foreach ($this->getAuthentication() as $auth) { + foreach ($this->getAuthentication() as $authIoc) { foreach ($guards as $guard) { // Call guard() if not null if ($guard && $guard != 'null') { - $auth = $auth->guard($guard); + $auth = $authIoc->guard($guard); } - } - if (is_callable([$auth, $method], true, $callable_name)) { - if ($data = $auth->$method()) { - return $data; + + if (is_callable([$auth, $method], true, $callable_name)) { + if ($data = $auth->$method()) { + return $data; + } } } } From 50aab4c25d5719e2fcd23dd295ac99410465066b Mon Sep 17 00:00:00 2001 From: Stephen Senjaya Date: Mon, 20 May 2024 13:18:18 +0700 Subject: [PATCH 4/7] MEL-1686 update status and change getCurrent to get from database --- src/Data/Repositories/Session.php | 14 +++++++++++--- src/Tracker.php | 15 +++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Data/Repositories/Session.php b/src/Data/Repositories/Session.php index 9af56b82..edd25aaa 100644 --- a/src/Data/Repositories/Session.php +++ b/src/Data/Repositories/Session.php @@ -175,6 +175,12 @@ private function storeSession() $this->putSessionData($this->sessionInfo); } + public function updateSessionOnCacheByUuid($uuid, $data){ + list($model, $cacheKey) = $this->cache->findCached($uuid, 'uuid', app()->make('tracker.config')->get('session_model')); + + $this->cache->cachePut($cacheKey, $data); + } + private function getSystemSessionId() { $sessionData = $this->getSessionData(); @@ -283,11 +289,13 @@ public function users($minutes, $results) return $this->getModel()->users($minutes, $results); } - public function getCurrent() - { - return $this->getModel(); + public function getCurrent(){ + return $this->newQuery()->find($this->getModel()['id']); } + public function fetchLatest(){ + $session = $this->newQuery()->find($this->getModel()['id']); + } public function updateSessionData($data) { $session = $this->checkIfUserChanged($data, $this->find($this->getSessionData('id'))); diff --git a/src/Tracker.php b/src/Tracker.php index fff5495c..021d1d38 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -582,4 +582,19 @@ public function updateGeoIp() return $success; } + + /** + * Update the Status database. + * + * @return bool + */ + public function updateStatus($session, $status) + { + $session = $this->dataRepositoryManager->sessionRepository->find($session['_id']); + $session->setAttribute('status', $status); + + $this->dataRepositoryManager->sessionRepository->updateSessionOnCacheByUuid($session['uuid'], $session); + + return $session; + } } From 45f3ac3c510bfc5fe30549f4c8b7b0a9ca0774f5 Mon Sep 17 00:00:00 2001 From: Stephen Senjaya Date: Mon, 20 May 2024 13:32:28 +0700 Subject: [PATCH 5/7] MEL-1686 fix updateStatus method --- src/Tracker.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Tracker.php b/src/Tracker.php index 021d1d38..9ccd8f6c 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -592,6 +592,7 @@ public function updateStatus($session, $status) { $session = $this->dataRepositoryManager->sessionRepository->find($session['_id']); $session->setAttribute('status', $status); + $session->save(); $this->dataRepositoryManager->sessionRepository->updateSessionOnCacheByUuid($session['uuid'], $session); From b945e47c31ad0593c2a570a0cf47af87c6de5507 Mon Sep 17 00:00:00 2001 From: Dina Date: Fri, 24 May 2024 11:50:07 +0900 Subject: [PATCH 6/7] rename status to bool is_active --- src/Data/Repositories/Session.php | 5 +---- src/Tracker.php | 19 ++++++++----------- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/Data/Repositories/Session.php b/src/Data/Repositories/Session.php index edd25aaa..91883d2f 100644 --- a/src/Data/Repositories/Session.php +++ b/src/Data/Repositories/Session.php @@ -69,7 +69,7 @@ private function sessionIsReliable() { $data = $this->getSessionData(); - if(!isset($data['status']) || $data['status'] == 0){ + if(!isset($data['is_active']) || $data['is_active'] === false){ return false; } @@ -293,9 +293,6 @@ public function getCurrent(){ return $this->newQuery()->find($this->getModel()['id']); } - public function fetchLatest(){ - $session = $this->newQuery()->find($this->getModel()['id']); - } public function updateSessionData($data) { $session = $this->checkIfUserChanged($data, $this->find($this->getSessionData('id'))); diff --git a/src/Tracker.php b/src/Tracker.php index 9ccd8f6c..b478944b 100644 --- a/src/Tracker.php +++ b/src/Tracker.php @@ -250,12 +250,12 @@ protected function makeSessionData() 'language_id' => $this->getLanguageId(), 'is_robot' => $this->isRobot(), 'impersonation_id' => null, - + // The key user_agent is not present in the sessions table, but // it's internally used to check if the user agent changed // during a session. 'user_agent' => $this->dataRepositoryManager->getCurrentUserAgent(), - 'status' => 1, + 'is_active' => true, ]; $authSessionPrefix = $this->config->get('auth_session_prefix'); @@ -444,8 +444,7 @@ public function pageViewsByCountry($minutes, $results = true) public function allowConsole() { - return - (!$this->laravel->runningInConsole()) || + return (!$this->laravel->runningInConsole()) || $this->config->get('console_log_enabled', false); } @@ -584,18 +583,16 @@ public function updateGeoIp() } /** - * Update the Status database. - * - * @return bool + * @return Session */ - public function updateStatus($session, $status) + public function updateSessionStatus($session, bool $status) { $session = $this->dataRepositoryManager->sessionRepository->find($session['_id']); - $session->setAttribute('status', $status); + $session->setAttribute('is_active', $status); $session->save(); - + $this->dataRepositoryManager->sessionRepository->updateSessionOnCacheByUuid($session['uuid'], $session); - + return $session; } } From a3410181c80173df332ba584ea2dfa7df14fb523 Mon Sep 17 00:00:00 2001 From: Dina Date: Fri, 24 May 2024 11:56:27 +0900 Subject: [PATCH 7/7] fix authentication_guards when it is empty --- src/Services/Authentication.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Services/Authentication.php b/src/Services/Authentication.php index b83de2ea..e509668f 100644 --- a/src/Services/Authentication.php +++ b/src/Services/Authentication.php @@ -40,6 +40,10 @@ private function executeAuthMethod($method) $auth = $authIoc->guard($guard); } + if (!isset($auth)) { + $auth = $authIoc->guard(); + } + if (is_callable([$auth, $method], true, $callable_name)) { if ($data = $auth->$method()) { return $data;