mirror of
https://github.com/kevinveenbirkenbach/coding-challenge-online-shop.git
synced 2024-11-01 00:53:10 +01:00
Solved user session bug
This commit is contained in:
parent
7470686c56
commit
228c261db1
@ -3,6 +3,9 @@ namespace core;
|
||||
|
||||
use entity\order\Order;
|
||||
use entity\user\UserInterface;
|
||||
use repository\user\User as UserRepository;
|
||||
use repository\user\UserInterface as UserRepositoryInterface;
|
||||
use entity\user\User;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -46,8 +49,14 @@ final class Core implements CoreInterface
|
||||
*/
|
||||
private $basket;
|
||||
|
||||
/**
|
||||
* @var UserRepositoryInterface
|
||||
*/
|
||||
private $userRepository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
session_start();
|
||||
$this->initTwig();
|
||||
$this->initDatabase();
|
||||
$this->initUser();
|
||||
@ -70,10 +79,18 @@ final class Core implements CoreInterface
|
||||
*/
|
||||
private function initUser(): void
|
||||
{
|
||||
$this->userRepository = new UserRepository($this);
|
||||
if($_SESSION['user']){
|
||||
$this->user = $_SESSION['user'];
|
||||
$this->user = $this->getUserBySession();
|
||||
}
|
||||
}
|
||||
|
||||
private function getUserBySession():UserInterface{
|
||||
$user = new User();
|
||||
$user->setPasswordHash($_SESSION['user']['hash']);
|
||||
$user->setEmail($_SESSION['user']['email']);
|
||||
return $this->userRepository->getUserByMailAndHash($user);
|
||||
}
|
||||
|
||||
private function initTwig(): void
|
||||
{
|
||||
@ -101,9 +118,21 @@ final class Core implements CoreInterface
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
private function setUserSession():void{
|
||||
if($this->user){
|
||||
$_SESSION['user'] = [
|
||||
'email'=>$this->user->getEmail(),
|
||||
'hash'=>$this->user->getPasswordHash(),
|
||||
];
|
||||
}else{
|
||||
unset($_SESSION['user']);
|
||||
}
|
||||
}
|
||||
|
||||
public function setUser(?UserInterface $user = null): void
|
||||
{
|
||||
$_SESSION['user'] = $this->user = $user;
|
||||
$this->user = $user;
|
||||
$this->setUserSession();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,9 @@ class CoreTest extends TestCase
|
||||
protected function setUp():void{
|
||||
$this->core = new Core();
|
||||
$this->user = new User();
|
||||
$this->user->setId(1);
|
||||
$this->user->setEmail('test@mail.test');
|
||||
$this->user->setPasswordHashByPassword('passwort:)');
|
||||
$this->core->setUser($this->user);
|
||||
}
|
||||
|
||||
@ -42,7 +45,7 @@ class CoreTest extends TestCase
|
||||
}
|
||||
|
||||
public function testSession():void{
|
||||
$this->assertEquals($this->core->getUser(), $_SESSION['user']);
|
||||
$this->assertEquals($this->core->getUser()->getPasswordHash(), $_SESSION['user']['hash']);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user