MAPG-140 MAPG-154 store user in session based on id

This commit is contained in:
Bence Pőcze 2020-06-19 18:16:52 +02:00
parent a9eec05288
commit 5ff720e3c6
7 changed files with 46 additions and 24 deletions

View File

@ -22,9 +22,7 @@ class LoginController
public function getLoginForm()
{
$session = $this->request->session();
if ($session->get('user')) {
if ($this->request->user() !== null) {
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
}
@ -34,9 +32,7 @@ class LoginController
public function login(): IContent
{
$session = $this->request->session();
if ($session->get('user')) {
if ($this->request->user() !== null) {
$data = ['success' => true];
return new JsonContent($data);
}
@ -58,7 +54,7 @@ class LoginController
return new JsonContent($data);
}
$session->set('user', $user);
$this->request->setUser($user);
$data = ['success' => true];
return new JsonContent($data);
@ -66,7 +62,7 @@ class LoginController
public function logout(): IRedirect
{
$this->request->session()->delete('user');
$this->request->setUser(null);
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
}

View File

@ -31,9 +31,7 @@ class SignupController
public function getSignupForm()
{
$session = $this->request->session();
if ($session->get('user')) {
if ($this->request->user() !== null) {
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
}
@ -43,9 +41,7 @@ class SignupController
public function signup(): IContent
{
$session = $this->request->session();
if ($session->get('user')) {
if ($this->request->user() !== null) {
//TODO: return with some error
$data = ['success' => true];
return new JsonContent($data);
@ -102,9 +98,7 @@ class SignupController
public function activate()
{
$session = $this->request->session();
if ($session->get('user')) {
if ($this->request->user() !== null) {
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
}
@ -132,16 +126,14 @@ class SignupController
\Container::$dbConnection->commit();
$session->set('user', $user);
$this->request->setUser($user);
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
}
public function cancel()
{
$session = $this->request->session();
if ($session->get('user')) {
if ($this->request->user() !== null) {
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
}

View File

@ -66,8 +66,6 @@ class UserController implements ISecured
$this->pdm->saveToDb($user);
$this->request->session()->set('user', $user);
$data = ['success' => true];
return new JsonContent($data);
}

View File

@ -8,5 +8,9 @@ interface IUser
public function hasPermission(int $permission): bool;
public function getUniqueId();
public function getDisplayName(): string;
public function checkPassword(string $password): bool;
}

View File

@ -14,5 +14,7 @@ interface IRequest
public function session(): ISession;
public function setUser(?IUser $user): void;
public function user(): ?IUser;
}

View File

@ -77,6 +77,11 @@ class User extends Model implements IUser
}
}
public function getUniqueId()
{
return $this->id;
}
public function getDisplayName(): string
{
return $this->email;

View File

@ -3,6 +3,9 @@
use MapGuesser\Interfaces\Authentication\IUser;
use MapGuesser\Interfaces\Request\IRequest;
use MapGuesser\Interfaces\Request\ISession;
use MapGuesser\PersistentData\Model\User;
use MapGuesser\PersistentData\PersistentDataManager;
use MapGuesser\Repository\UserRepository;
class Request implements IRequest
{
@ -16,12 +19,24 @@ class Request implements IRequest
private Session $session;
private UserRepository $userRepository;
private ?User $user = null;
public function __construct(string $base, array &$get, array &$post, array &$session)
{
$this->base = $base;
$this->get = &$get;
$this->post = &$post;
$this->session = new Session($session);
$this->userRepository = new UserRepository();
$userId = $this->session->get('userId');
if ($userId !== null) {
$this->user = $this->userRepository->getById($userId);
}
}
public function setParsedRouteParams(array &$routeParams)
@ -61,8 +76,18 @@ class Request implements IRequest
return $this->session;
}
public function setUser(?IUser $user): void
{
if ($user === null) {
$this->session->delete('userId');
return;
}
$this->session->set('userId', $user->getUniqueId());
}
public function user(): ?IUser
{
return $this->session->get('user');
return $this->user;
}
}