Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log active user sessions to database (#5339)
Co-authored-by: rex-bot <78283150+rex-bot@users.noreply.github.com>
- Loading branch information
Showing
11 changed files
with
166 additions
and
12 deletions.
There are no files selected for viewing
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
/** | ||
* @package redaxo\core\login | ||
*/ | ||
class rex_user_session | ||
{ | ||
use rex_singleton_trait; | ||
|
||
private const SESSION_VAR_LAST_DB_UPDATE = 'last_db_update'; | ||
|
||
private function __construct() | ||
{ | ||
rex_extension::register('RESPONSE_SHUTDOWN', [self::class, 'clearExpiredSessions']); | ||
} | ||
|
||
public function storeCurrentSession(): void | ||
{ | ||
if (false === session_id()) { | ||
return; | ||
} | ||
|
||
$login = new rex_backend_login(); | ||
$userId = $login->getSessionVar(rex_login::SESSION_IMPERSONATOR, null); | ||
if (null === $userId) { | ||
$userId = $login->getSessionVar(rex_login::SESSION_USER_ID); | ||
} | ||
|
||
rex_sql::factory() | ||
->setTable(rex::getTable('user_session')) | ||
->setValue('session_id', session_id()) | ||
->setValue('user_id', $userId) | ||
->setValue('ip', rex_request::server('REMOTE_ADDR', 'string')) | ||
->setValue('useragent', rex_request::server('HTTP_USER_AGENT', 'string')) | ||
->setValue('starttime', rex_sql::datetime($login->getSessionVar(rex_login::SESSION_START_TIME, time()))) | ||
->setValue('last_activity', rex_sql::datetime($login->getSessionVar(rex_login::SESSION_LAST_ACTIVITY))) | ||
->insertOrUpdate(); | ||
|
||
$login->setSessionVar(self::SESSION_VAR_LAST_DB_UPDATE, time()); | ||
} | ||
|
||
public function clearCurrentSession(): void | ||
{ | ||
if (false === session_id()) { | ||
return; | ||
} | ||
|
||
rex_sql::factory() | ||
->setTable(rex::getTable('user_session')) | ||
->setWhere('session_id = ?', [session_id()]) | ||
->delete(); | ||
} | ||
|
||
public function updateLastActivity(): void | ||
{ | ||
if (false === session_id()) { | ||
return; | ||
} | ||
|
||
$login = new rex_backend_login(); | ||
|
||
// only once a minute | ||
if ($login->getSessionVar(self::SESSION_VAR_LAST_DB_UPDATE, 0) > (time() - 60)) { | ||
return; | ||
} | ||
|
||
$this->storeCurrentSession(); | ||
} | ||
|
||
public static function clearExpiredSessions(): void | ||
{ | ||
rex_sql::factory() | ||
->setTable(rex::getTable('user_session')) | ||
->setWhere('UNIX_TIMESTAMP(last_activity) < ?', [time() - (int) rex::getProperty('session_duration')]) | ||
->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -294,3 +294,5 @@ | |
</form>'; | ||
|
||
echo $content; | ||
|
||
require rex_path::core('pages/profile.sessions.php'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
$list = rex_list::factory('Select session_id, ip, useragent, starttime, last_activity from rex_user_session where user_id = '.rex::requireUser()->getId()); | ||
|
||
$list->setColumnLabel('session_id', rex_i18n::msg('session_id')); | ||
$list->setColumnLabel('ip', rex_i18n::msg('ip')); | ||
$list->setColumnLabel('useragent', rex_i18n::msg('user_agent')); | ||
$list->setColumnLabel('starttime', rex_i18n::msg('starttime')); | ||
$list->setColumnLabel('last_activity', rex_i18n::msg('last_activity')); | ||
|
||
$list->setColumnFormat('last_activity', 'custom', static function () use ($list) { | ||
if (session_id() === $list->getValue('session_id')) { | ||
return rex_i18n::msg('active_session'); | ||
} | ||
return rex_formatter::date((string) $list->getValue('last_activity'), 'd.m.Y H:i'); | ||
}); | ||
$list->setColumnFormat('starttime', 'custom', static function () use ($list) { | ||
return rex_formatter::date((string) $list->getValue('starttime'), 'd.m.Y H:i'); | ||
}); | ||
$content = $list->get(); | ||
|
||
$fragment = new rex_fragment(); | ||
$fragment->setVar('title', rex_i18n::msg('session_caption')); | ||
$fragment->setVar('content', $content, false); | ||
echo $fragment->parse('core/page/section.php'); |