Use Native Session functions (global "$_SESSION" variable) for Memory class because of the direct usage of the $_SESSION class all around the codebase

This commit is contained in:
Philipp Holzer 2019-12-10 22:29:49 +01:00
parent eca3396851
commit b9f8762eb3
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
4 changed files with 13 additions and 73 deletions

View File

@ -4,7 +4,7 @@ namespace Friendica\Core\Session;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Session; use Friendica\Core\Session;
use Friendica\Database\Database; use Friendica\Database\Database as DBA;
use Friendica\Model\User\Cookie; use Friendica\Model\User\Cookie;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use SessionHandlerInterface; use SessionHandlerInterface;
@ -16,7 +16,7 @@ use SessionHandlerInterface;
*/ */
final class Database extends Native implements SessionHandlerInterface final class Database extends Native implements SessionHandlerInterface
{ {
/** @var Database */ /** @var DBA */
private $dba; private $dba;
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
@ -30,7 +30,7 @@ final class Database extends Native implements SessionHandlerInterface
* @param LoggerInterface $logger * @param LoggerInterface $logger
* @param array $server * @param array $server
*/ */
public function __construct(Configuration $config, Cookie $cookie, Database $dba, LoggerInterface $logger, array $server) public function __construct(Configuration $config, Cookie $cookie, DBA $dba, LoggerInterface $logger, array $server)
{ {
parent::__construct($config, $cookie); parent::__construct($config, $cookie);

View File

@ -4,11 +4,11 @@ namespace Friendica\Core\Session;
/** /**
* Usable for backend processes (daemon/worker) and testing * Usable for backend processes (daemon/worker) and testing
*
* @todo after replacing the last direct $_SESSION call, use a internal array instead of the global variable
*/ */
final class Memory implements ISession final class Memory extends Native
{ {
private $data = [];
public function start() public function start()
{ {
// Backward compatibility until all Session variables are replaced // Backward compatibility until all Session variables are replaced
@ -17,69 +17,4 @@ final class Memory implements ISession
$this->clear(); $this->clear();
return $this; return $this;
} }
}
/**
* @inheritDoc
*/
public function exists(string $name)
{
return isset($this->data[$name]);
}
/**
* @inheritDoc
*/
public function get(string $name, $defaults = null)
{
return $this->data[$name] ?? $defaults;
}
/**
* @inheritDoc
*/
public function set(string $name, $value)
{
$this->data[$name] = $value;
}
/**
* @inheritDoc
*/
public function setMultiple(array $values)
{
foreach ($values as $key => $value) {
$this->data[$key] = $value;
}
}
/**
* @inheritDoc
*/
public function remove(string $name)
{
if ($this->exists($name)) {
unset($this->data[$name]);
return true;
}
return false;
}
/**
* @inheritDoc
*/
public function clear()
{
$this->data = [];
return true;
}
/**
* @inheritDoc
*/
public function delete()
{
$this->data = [];
return true;
}
}

View File

@ -47,7 +47,7 @@ class SessionFactory
try { try {
if ($mode->isInstall() || $mode->isBackend()) { if ($mode->isInstall() || $mode->isBackend()) {
$session = new Session\Memory(); $session = new Session\Memory($config, $cookie);
} else { } else {
$session_handler = $config->get('system', 'session_handler', self::DEFAULT); $session_handler = $config->get('system', 'session_handler', self::DEFAULT);

View File

@ -11,6 +11,7 @@ use Friendica\BaseObject;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Config\PConfiguration; use Friendica\Core\Config\PConfiguration;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\Session\ISession;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
@ -111,6 +112,10 @@ class ApiTest extends DatabaseTest
// User ID that we know is not in the database // User ID that we know is not in the database
$this->wrongUserId = 666; $this->wrongUserId = 666;
/** @var ISession $session */
$session = BaseObject::getClass(ISession::class);
$session->start();
// Most API require login so we force the session // Most API require login so we force the session
$_SESSION = [ $_SESSION = [
'allow_api' => true, 'allow_api' => true,