Skip to content

Commit

Permalink
WIP refactor session wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-peugnet committed Dec 24, 2023
1 parent 9c96b22 commit 50922a2
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 121 deletions.
10 changes: 3 additions & 7 deletions app/class/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,11 @@ public static function issecure()
}

/**
* @param bool $trailingslash If not empty basepath, add a trailing slash after the basepath
* @return string Basepath without trailing slash
*/
public static function basepath(bool $trailingslash = false): string
public static function basepath(): string
{
if ($trailingslash && !empty(self::$basepath)) {
return self::$basepath . '/';
} else {
return self::$basepath;
}
return self::$basepath;
}

public static function route404()
Expand Down
30 changes: 10 additions & 20 deletions app/class/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

class Controller
{
/** @var Session */
protected $session;
protected Servicesession $servicesession;

protected Workspace $workspace;

Expand All @@ -37,8 +36,8 @@ class Controller

public function __construct(AltoRouter $router)
{
$this->session = new Session($_SESSION['user' . Config::basepath()] ?? []);
$this->workspace = new Workspace($_SESSION['user' . Config::basepath()]['workspace'] ?? []);
$this->servicesession = new Servicesession();
$this->workspace = $this->servicesession->getworkspace();
$this->usermanager = new Modeluser();

$this->user = new User();
Expand All @@ -52,24 +51,23 @@ public function __construct(AltoRouter $router)
protected function setuser()
{
// check session, then cookies
if (!empty($this->session->user)) {
$sessionuser = $this->session->user;
if (!is_null($this->servicesession->getuser())) {
$sessionuser = $this->servicesession->getuser();
try {
$this->user = $this->usermanager->get($sessionuser);
} catch (Notfoundexception $e) {
Logger::warning("Deleted session using non existing user : '$sessionuser'");
$this->session->empty(); // empty the session as a non existing user was set
$this->servicesession->empty(); // empty the session as a non existing user was set
}
} elseif (!empty($_COOKIE['authtoken'])) {
try {
$modelconnect = new Modelconnect();
$datas = $modelconnect->checkcookie();
$cookieuser = $datas['userid'];
$user = $this->usermanager->get($datas['userid']);
if ($user->checksession($datas['wsession'])) {
$this->user = $user;
$this->session->addtosession("wsession", $datas['wsession']);
$this->session->addtosession("user", $user->id());
$this->servicesession->setwsessionid($datas['wsession']);
$this->servicesession->setuser($user->id());
} else {
$modelconnect->deleteauthcookie(); // As not listed in the user
}
Expand Down Expand Up @@ -185,24 +183,16 @@ public function sendstatflashmessage(int $count, int $total, string $message)
protected function disconnect()
{
try {
$this->user->destroysession($this->session->wsession);
$this->user->destroysession($this->servicesession->getwsessionid());
$cookiemanager = new Modelconnect();
$cookiemanager->deleteauthcookie();
$this->session->empty();
$this->servicesession->empty();
$this->usermanager->update($this->user);
} catch (Databaseexception $e) {
Logger::errorex($e);
}
}

/**
* @todo user Session object instead
*/
protected function workspace2session(): void
{
$_SESSION['user' . Config::basepath()]['workspace'] = $this->workspace->dry();
}

/**
* Tell if the current user can edit the given Page
*
Expand Down
8 changes: 4 additions & 4 deletions app/class/Controllerconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ protected function login(string $route, ?string $paramid = null): void
} else {
$this->user->connectcounter();
$this->usermanager->add($this->user);
$this->session->addtosession('user', $this->user->id());
$this->servicesession->setuser($this->user->id());
Model::sendflashmessage("Successfully logged in as " . $this->user->id(), Model::FLASH_SUCCESS);

if (!empty($_POST['rememberme'])) {
if ($this->user->cookie() > 0) {
$this->modelconnect = new Modelconnect();
$wsession = $this->user->newsession();
$wsessionid = $this->user->newsession();
$this->modelconnect->createauthcookie(
$this->user->id(),
$wsession,
$wsessionid,
$this->user->cookie()
);
$this->usermanager->add($this->user);
$this->session->addtosession('wsession', $wsession);
$this->servicesession->setwsessionid($wsessionid);
} else {
$message = "Can't remember you beccause user cookie conservation time is set to 0 days";
Model::sendflashmessage($message, Model::FLASH_WARNING);
Expand Down
1 change: 0 additions & 1 deletion app/class/Controllerhome.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ public function columns()
$user = $this->usermanager->get($this->user->id());
$user->hydrate($_POST);
$this->usermanager->add($user);
$this->usermanager->writesession($user);
Model::sendflashmessage('Display settings successfully saved', Model::FLASH_SUCCESS);
} catch (Databaseexception $e) {
Model::sendflashmessage('Error while trying to save display settings', Model::FLASH_ERROR);
Expand Down
2 changes: 1 addition & 1 deletion app/class/Controllermedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function desktop()

if (isset($_GET['display'])) {
$this->workspace->setmediadisplay($_GET['display']);
$this->workspace2session();
$this->servicesession->setworkspace($this->workspace);
}

$vars['filtercode'] = !empty($_POST); // indicate that filter code has been generated
Expand Down
2 changes: 1 addition & 1 deletion app/class/Controllerworkspace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function update()
{
if ($this->user->isinvite()) {
$this->workspace->hydrate($_POST);
$this->workspace2session();
$this->servicesession->setworkspace($this->workspace);
}
if (isset($_POST['page'])) {
$this->routedirect('pageedit', ['page' => $_POST['page']]);
Expand Down
8 changes: 4 additions & 4 deletions app/class/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ public static function iconpath()
*/
public static function getflashmessages(): array
{
if (!empty($_SESSION['user' . Config::basepath()]['flashmessages'])) {
$flashmessage = $_SESSION['user' . Config::basepath()]['flashmessages'];
$_SESSION['user' . Config::basepath()]['flashmessages'] = [];
if (!empty($_SESSION['flashmessages'])) {
$flashmessage = $_SESSION['flashmessages'];
$_SESSION['flashmessages'] = [];
if (is_array($flashmessage)) {
return $flashmessage;
} else {
Expand All @@ -172,7 +172,7 @@ public static function sendflashmessage(string $content, string $type = self::FL
if (!key_exists($type, self::FLASH_MESSAGE_TYPES)) {
$type = self::FLASH_INFO;
}
$_SESSION['user' . Config::basepath()]['flashmessages'][] = ['content' => $content, 'type' => $type];
$_SESSION['flashmessages'][] = ['content' => $content, 'type' => $type];
}


Expand Down
12 changes: 0 additions & 12 deletions app/class/Modeluser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,6 @@ public function __construct()
$this->storeinit(self::USER_REPO_NAME);
}

/**
* Write session cookie according to users datas
*
* @param User $user Current user to keep in session
*/
public function writesession(User $user)
{
$_SESSION['user' . Config::basepath()]['level'] = $user->level();
$_SESSION['user' . Config::basepath()]['id'] = $user->id();
$_SESSION['user' . Config::basepath()]['columns'] = $user->columns();
}


public function logout()
{
Expand Down
65 changes: 65 additions & 0 deletions app/class/Servicesession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Wcms;

class Servicesession
{
public function setvisitor(bool $visitor): void
{
$_SESSION['visitor'] = $visitor;
}

public function getvisitor(): bool
{
return $_SESSION['visitor'];
}

public function setuser(string $userid): void
{
$_SESSION['user'] = $userid;
}

public function getuser(): ?string
{
return $_SESSION['user'];
}

public function setwsessionid(string $wsessionid): void
{
$_SESSION['wsession'] = $wsessionid;
}

public function getwsessionid(): string
{
return $_SESSION['wsession'] ?? '';
}

public function setopt(array $opt): void
{
$_SESSION['opt'] = $opt;
}

public function getopt(): array
{
return $_SESSION['opt'] ?? [];
}

public function setworkspace(Workspace $workspace): void
{
$_SESSION['workspace'] = $workspace->dry();
}

public function getworkspace(): Workspace
{
$datas = $_SESSION['workspace'] ?? [];
return new Workspace($datas);
}

/**
* Empty current user session
*/
public function empty(): void
{
$_SESSION = [];
}
}
68 changes: 0 additions & 68 deletions app/class/Session.php

This file was deleted.

6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?php

session_start();


require('./vendor/autoload.php');

try {
Expand All @@ -14,6 +11,9 @@
$app = new Wcms\Application();
$app->wakeup();

session_set_cookie_params(['path' => Wcms\Config::basepath() . '/']);
session_start();

if (class_exists('Whoops\Run') && !empty(Wcms\Config::debug())) {
$whoops = new \Whoops\Run();
$handler = new \Whoops\Handler\PrettyPageHandler();
Expand Down

0 comments on commit 50922a2

Please sign in to comment.