Implemented login

This commit is contained in:
Kevin Frantz 2018-07-15 10:51:53 +02:00
parent 80be8e7361
commit a444f4805c
8 changed files with 94 additions and 18 deletions

View File

@ -2,6 +2,7 @@
namespace controller; namespace controller;
use core\CoreInterface; use core\CoreInterface;
use router\Router;
/** /**
* *
@ -37,4 +38,11 @@ abstract class AbstractController
$variables['user'] = $this->core->getUser(); $variables['user'] = $this->core->getUser();
return $variables; return $variables;
} }
protected function route(?array $get =[]):void{
$router = new Router();
$router->setCore($this->core);
$router->setGet($get);
$router->route();
}
} }

View File

@ -3,6 +3,10 @@ namespace controller\user;
use controller\AbstractDefaultController; use controller\AbstractDefaultController;
use router\Router; use router\Router;
use controller\AbstractController;
use core\CoreInterface;
use repository\user\User as UserRepository;
use entity\user\User as UserEntity;
/** /**
* *
@ -11,24 +15,50 @@ use router\Router;
*/ */
final class User extends AbstractDefaultController implements UserInterface final class User extends AbstractDefaultController implements UserInterface
{ {
/**
*
* @var UserRepository
*/
private $repository;
public function __construct(CoreInterface $core)
{
parent::__construct($core);
$this->repository = new UserRepository($core);
}
public function logout(): void public function logout(): void
{ {
$this->core->setUser(null); $this->core->setUser(null);
$router = new Router(); $this->route();
$router->setCore($this->core);
$router->setGet([]);
$router->route();
} }
public function login(): void public function login(): void
{ {
if ($this->post) {
try {
$this->loginRoutine();
} catch (\Exception $exception) {
$this->render('frames/exception.html.twig',['message'=>$exception->getMessage()]);
}
} else {
$this->render('user/login.html.twig'); $this->render('user/login.html.twig');
} }
}
private function loginRoutine(): void
{
$requestedUser = new UserEntity();
$requestedUser->setPasswordHashByPassword($this->post['password']);
$requestedUser->setEmail($this->post['email']);
$this->core->setUser($this->repository->getUserByMailAndHash($requestedUser));
$this->route();
}
public function register(): void public function register(): void
{ {
$this->render('user/register.html.twig'); $this->render('user/register.html.twig');
} }
} }

View File

@ -56,12 +56,11 @@ final class User implements UserInterface
} }
/** /**
* In a real application you should use a salt ;)
* @param string $password * @param string $password
*/ */
public function setPasswordHashByPassword(string $password): void public function setPasswordHashByPassword(string $password): void
{ {
$this->passwordHash = password_hash($password, PASSWORD_BCRYPT); $this->passwordHash = $this->hashPassword($password);
} }
public function getEmail(): string public function getEmail(): string
@ -78,5 +77,16 @@ final class User implements UserInterface
return $this->id; return $this->id;
} }
/**
* In a real application you should use a salt ;)
* @param string $password
* @return string
*/
private function hashPassword(string $password): string
{
return password_hash($password, PASSWORD_BCRYPT,['salt' => '1235813471123581347112358134']);
}
} }

View File

@ -14,7 +14,9 @@ class UserTest extends TestCase
const EMAIL = 'test@mail.world'; const EMAIL = 'test@mail.world';
const HASH = '1235'; const HASH = '$2y$10$123581347112358134711urUo63Gbn3BFTEe9UGbPxJwrZ80q.LbK';
const PASSWORD = 'passwort:)';
const ID = 5678; const ID = 5678;
@ -27,7 +29,7 @@ class UserTest extends TestCase
$this->user = new User(); $this->user = new User();
$this->user->setName(self::NAME); $this->user->setName(self::NAME);
$this->user->setEmail(self::EMAIL); $this->user->setEmail(self::EMAIL);
$this->user->setPasswordHash(self::HASH); $this->user->setPasswordHashByPassword(self::PASSWORD);
$this->user->setId(self::ID); $this->user->setId(self::ID);
} }

View File

@ -3,6 +3,7 @@ namespace repository\user;
use repository\AbstractRepository; use repository\AbstractRepository;
use entity\user\UserInterface as UserEntityInterface; use entity\user\UserInterface as UserEntityInterface;
use entity\user\User as UserEntity;
/** /**
* *
@ -22,7 +23,27 @@ final class User extends AbstractRepository implements UserInterface
]); ]);
} }
public function getUserByMailAndHash(string $mail, string $hash): UserEntityInterface public function getUserByMailAndHash(UserEntityInterface $user): UserEntityInterface
{} {
$statement = $this->database->prepare('SELECT * FROM `user` WHERE `hash` = ? AND `email`=?;');
$statement->execute([
$user->getPasswordHash(),
$user->getEmail(),
]);
$result = $statement->fetch();
if($result){
return $this->fetchToUser($result);
}
throw new \Exception('Verification data is not valid!');
}
private function fetchToUser(array $fetch):UserEntityInterface{
$user = new UserEntity();
$user->setPasswordHash($fetch['hash']);
$user->setName($fetch['name']);
$user->setEmail($fetch['email']);
$user->setId($fetch['id']);
return $user;
}
} }

View File

@ -12,6 +12,6 @@ interface UserInterface
{ {
public function addUser(UserEntityInterface $user):void; public function addUser(UserEntityInterface $user):void;
public function getUserByMailAndHash(string $mail,string $hash):UserEntityInterface; public function getUserByMailAndHash(UserEntityInterface $user):UserEntityInterface;
} }

View File

@ -0,0 +1,5 @@
{% extends "frames/default.html.twig" %}
{% block content %}
<h1>Error!</h1>
<p>{{ message }}</p>
{% endblock %}

View File

@ -2,14 +2,14 @@
{% block title %}login{% endblock %} {% block title %}login{% endblock %}
{% block content %} {% block content %}
<h1>Login</h1> <h1>Login</h1>
<form> <form method='post'>
<div class="form-group"> <div class="form-group">
<label for="exampleInputEmail1">Email address</label> <label for="email">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email"> <input type="email" name="email" class="form-control" id="email" aria-describedby="emailHelp" placeholder="Enter email">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="exampleInputPassword1">Password</label> <label for="password">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password"> <input type="password" name="password" class="form-control" id="password" placeholder="Password">
</div> </div>
<button type="submit" class="btn btn-primary">Submit</button> <button type="submit" class="btn btn-primary">Submit</button>
</form> </form>