diff --git a/src/controller/AbstractController.php b/src/controller/AbstractController.php index 57d9af5..e443807 100644 --- a/src/controller/AbstractController.php +++ b/src/controller/AbstractController.php @@ -2,6 +2,7 @@ namespace controller; use core\CoreInterface; +use router\Router; /** * @@ -37,4 +38,11 @@ abstract class AbstractController $variables['user'] = $this->core->getUser(); return $variables; } + + protected function route(?array $get =[]):void{ + $router = new Router(); + $router->setCore($this->core); + $router->setGet($get); + $router->route(); + } } diff --git a/src/controller/user/User.php b/src/controller/user/User.php index e9e8b6f..a95472d 100644 --- a/src/controller/user/User.php +++ b/src/controller/user/User.php @@ -3,6 +3,10 @@ namespace controller\user; use controller\AbstractDefaultController; 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 { + + /** + * + * @var UserRepository + */ + private $repository; + + public function __construct(CoreInterface $core) + { + parent::__construct($core); + $this->repository = new UserRepository($core); + } + public function logout(): void { $this->core->setUser(null); - $router = new Router(); - $router->setCore($this->core); - $router->setGet([]); - $router->route(); + $this->route(); } public function login(): void { - $this->render('user/login.html.twig'); + 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'); + } + } + + 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 { $this->render('user/register.html.twig'); } - } diff --git a/src/entity/user/User.php b/src/entity/user/User.php index 34a2cf3..50289dc 100644 --- a/src/entity/user/User.php +++ b/src/entity/user/User.php @@ -56,12 +56,11 @@ final class User implements UserInterface } /** - * In a real application you should use a salt ;) * @param string $password */ public function setPasswordHashByPassword(string $password): void { - $this->passwordHash = password_hash($password, PASSWORD_BCRYPT); + $this->passwordHash = $this->hashPassword($password); } public function getEmail(): string @@ -77,6 +76,17 @@ final class User implements UserInterface { 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']); + } + } diff --git a/src/entity/user/UserTest.php b/src/entity/user/UserTest.php index 1bfc6de..93d6b6d 100644 --- a/src/entity/user/UserTest.php +++ b/src/entity/user/UserTest.php @@ -14,7 +14,9 @@ class UserTest extends TestCase const EMAIL = 'test@mail.world'; - const HASH = '1235'; + const HASH = '$2y$10$123581347112358134711urUo63Gbn3BFTEe9UGbPxJwrZ80q.LbK'; + + const PASSWORD = 'passwort:)'; const ID = 5678; @@ -27,7 +29,7 @@ class UserTest extends TestCase $this->user = new User(); $this->user->setName(self::NAME); $this->user->setEmail(self::EMAIL); - $this->user->setPasswordHash(self::HASH); + $this->user->setPasswordHashByPassword(self::PASSWORD); $this->user->setId(self::ID); } diff --git a/src/repository/user/User.php b/src/repository/user/User.php index 7510811..20455d4 100644 --- a/src/repository/user/User.php +++ b/src/repository/user/User.php @@ -3,6 +3,7 @@ namespace repository\user; use repository\AbstractRepository; 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; + } } diff --git a/src/repository/user/UserInterface.php b/src/repository/user/UserInterface.php index f9901d9..6a531dd 100644 --- a/src/repository/user/UserInterface.php +++ b/src/repository/user/UserInterface.php @@ -12,6 +12,6 @@ interface UserInterface { public function addUser(UserEntityInterface $user):void; - public function getUserByMailAndHash(string $mail,string $hash):UserEntityInterface; + public function getUserByMailAndHash(UserEntityInterface $user):UserEntityInterface; } diff --git a/src/template/frames/exception.html.twig b/src/template/frames/exception.html.twig new file mode 100644 index 0000000..fa822e7 --- /dev/null +++ b/src/template/frames/exception.html.twig @@ -0,0 +1,5 @@ +{% extends "frames/default.html.twig" %} +{% block content %} +

Error!

+

{{ message }}

+{% endblock %} \ No newline at end of file diff --git a/src/template/user/login.html.twig b/src/template/user/login.html.twig index 335984c..e68ff8f 100644 --- a/src/template/user/login.html.twig +++ b/src/template/user/login.html.twig @@ -2,14 +2,14 @@ {% block title %}login{% endblock %} {% block content %}

Login

-
+
- - + +
- - + +