Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues with extending modSessionHandler and flushing all sessions #16522

Open
wants to merge 2 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 14 additions & 17 deletions core/src/Revolution/Processors/Security/Flush.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,29 @@
*/
class Flush extends Processor
{
/**
* @return array
*/
public function getLanguageTopics()

Check warning on line 25 in core/src/Revolution/Processors/Security/Flush.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Security/Flush.php#L25

Added line #L25 was not covered by tests
{
return ['topmenu'];

Check warning on line 27 in core/src/Revolution/Processors/Security/Flush.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Security/Flush.php#L27

Added line #L27 was not covered by tests
}

public function checkPermissions()
{
return $this->modx->hasPermission('flush_sessions');
}

public function process()
{
if ($this->modx->getOption('session_handler_class',null,modSessionHandler::class) === modSessionHandler::class) {
if (!$this->flushSessions()) {
return $this->failure($this->modx->lexicon('flush_sessions_err'));
}
} else {
$sessionHandler = $this->modx->services->get('session_handler');
if (!method_exists($sessionHandler, 'flushSessions')) {

Check warning on line 38 in core/src/Revolution/Processors/Security/Flush.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Security/Flush.php#L37-L38

Added lines #L37 - L38 were not covered by tests
return $this->failure($this->modx->lexicon('flush_sessions_not_supported'));
}
return $this->success();
}

public function flushSessions()
{
$flushed = true;
$sessionTable = $this->modx->getTableName(modSession::class);
if ($this->modx->query("TRUNCATE TABLE {$sessionTable}") == false) {
$flushed = false;
} else {
$this->modx->user->endSession();
$flushed = call_user_func_array([$sessionHandler, 'flushSessions'], [&$this->modx]);
if (!$flushed) {
return $this->failure($this->modx->lexicon('flush_sessions_err'));

Check warning on line 43 in core/src/Revolution/Processors/Security/Flush.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Security/Flush.php#L41-L43

Added lines #L41 - L43 were not covered by tests
}
return $flushed;
return $this->success();

Check warning on line 45 in core/src/Revolution/Processors/Security/Flush.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/Processors/Security/Flush.php#L45

Added line #L45 was not covered by tests
}
}
23 changes: 20 additions & 3 deletions core/src/Revolution/modSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @package MODX\Revolution
*/
class modSessionHandler
class modSessionHandler implements \SessionHandlerInterface
{
/**
* @var modX A reference to the modX instance controlling this session
Expand All @@ -42,7 +42,7 @@
*
* @param modX &$modx A reference to a {@link modX} instance.
*/
function __construct(modX &$modx)
public function __construct(modX &$modx)

Check warning on line 45 in core/src/Revolution/modSessionHandler.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modSessionHandler.php#L45

Added line #L45 was not covered by tests
{
$this->modx = &$modx;
$gcMaxlifetime = (integer)$this->modx->getOption('session_gc_maxlifetime');
Expand All @@ -68,7 +68,7 @@
* @return boolean Always returns true; actual connection is managed by
* {@link modX}.
*/
public function open()
public function open($path, $name)

Check warning on line 71 in core/src/Revolution/modSessionHandler.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modSessionHandler.php#L71

Added line #L71 was not covered by tests
{
return true;
}
Expand Down Expand Up @@ -166,6 +166,23 @@
return $this->modx->removeCollection(modSession::class, ["{$this->modx->escape('access')} < {$maxtime}"]);
}

/**
* Removes all sessions, logging out all users.
*
* @param modX $modx
* @return boolean
*/
public static function flushSessions(modX $modx)

Check warning on line 175 in core/src/Revolution/modSessionHandler.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modSessionHandler.php#L175

Added line #L175 was not covered by tests
{
$sessionTable = $modx->getTableName(modSession::class);
if ($modx->query("TRUNCATE TABLE {$sessionTable}") == false) {
return false;

Check warning on line 179 in core/src/Revolution/modSessionHandler.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modSessionHandler.php#L177-L179

Added lines #L177 - L179 were not covered by tests
}

$modx->user->endSession();
return true;

Check warning on line 183 in core/src/Revolution/modSessionHandler.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modSessionHandler.php#L182-L183

Added lines #L182 - L183 were not covered by tests
}

/**
* Gets the {@link modSession} object, respecting the cache flag represented by cacheLifetime.
*
Expand Down
24 changes: 7 additions & 17 deletions core/src/Revolution/modX.php
Original file line number Diff line number Diff line change
Expand Up @@ -2458,7 +2458,7 @@
}

$deprecation = $this->_getDeprecatedMethod($since, $deprecatedDef, $recommendation);
$deprecation->addCaller($caller['class'] ?? '', $caller['function'] ?? '', $deprecatedMethod['file'], $deprecatedMethod['line']);

Check warning on line 2461 in core/src/Revolution/modX.php

View workflow job for this annotation

GitHub Actions / phpcs

Line exceeds 120 characters; contains 137 characters
}

/**
Expand Down Expand Up @@ -2740,25 +2740,15 @@
$contextKey= $this->context instanceof modContext ? $this->context->get('key') : null;
if ($this->getOption('session_enabled', $options, true) || isset($_GET['preview'])) {
if (!in_array($this->getSessionState(), [modX::SESSION_STATE_INITIALIZED, modX::SESSION_STATE_EXTERNAL, modX::SESSION_STATE_UNAVAILABLE], true)) {
$sh = false;
if ($sessionHandlerClass = $this->getOption('session_handler_class', $options)) {
if ($shClass = $this->loadClass($sessionHandlerClass, '', false, true)) {
if ($sh = new $shClass($this)) {
session_set_save_handler(
[& $sh, 'open'],
[& $sh, 'close'],
[& $sh, 'read'],
[& $sh, 'write'],
[& $sh, 'destroy'],
[& $sh, 'gc']
);
}
$sessionHandlerClass = $this->getOption('session_handler_class', $options);
if (is_string($sessionHandlerClass) && !empty($sessionHandlerClass) && class_exists($sessionHandlerClass)) {
$sh = new $sessionHandlerClass($this);
if ($sh instanceof \SessionHandlerInterface) {
$this->services->add('session_handler', $sh);
session_set_save_handler($sh);

Check warning on line 2748 in core/src/Revolution/modX.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modX.php#L2743-L2748

Added lines #L2743 - L2748 were not covered by tests
}
}
if (
(is_string($sessionHandlerClass) && !$sh instanceof $sessionHandlerClass) ||
!is_string($sessionHandlerClass)
) {
if (!$this->services->has('session_handler')) {

Check warning on line 2751 in core/src/Revolution/modX.php

View check run for this annotation

Codecov / codecov/patch

core/src/Revolution/modX.php#L2751

Added line #L2751 was not covered by tests
$sessionSavePath = $this->getOption('session_save_path', $options);
if ($sessionSavePath && is_writable($sessionSavePath)) {
session_save_path($sessionSavePath);
Expand Down