From 5e8c945170086e32cb9e8d4ffb29933909299890 Mon Sep 17 00:00:00 2001 From: tracer Date: Mon, 24 Oct 2022 20:07:59 +0200 Subject: [PATCH] added password hashing --- src/Entity/User.php | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/Entity/User.php b/src/Entity/User.php index e004bea..ef4cecd 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -14,16 +14,31 @@ use App\Enums\UserAuth; class User { public function __construct( - private string $nick = '', - private string $password = '', - private string $first = '', - private string $last = '', - private int $id = 0, - private bool $isAdmin = false, - private UserAuth $userAuth = UserAuth::AUTH_ANONYMOUS + private string $nick = '', + private string $password = '', + private readonly string $newPassword = '', + private string $first = '', + private string $last = '', + private int $id = 0, + private bool $isAdmin = false, + private UserAuth $userAuth = UserAuth::AUTH_ANONYMOUS ) { - // empty body + if (!empty($this->newPassword)) { + echo "password"; + $this->password = password_hash(password: $this->newPassword, algo: PASSWORD_ARGON2I); + } + + if (session_status() === PHP_SESSION_ACTIVE) { + // ANONYMOUS has id 0 + if ($this->id != 0) { + if ($this->isAdmin) { + $this->userAuth = UserAuth::AUTH_ADMIN; + } else { + $this->userAuth = UserAuth::AUTH_USER; + } + } + } } public function getNick(): string @@ -86,12 +101,12 @@ class User $this->isAdmin = $isAdmin; } - public function getAuth() + public function getAuth(): UserAuth { - return UserAuth::AUTH_ANONYMOUS; + return $this->userAuth; } - public function setAuth(UserAuth $userAuth) + public function setAuth(UserAuth $userAuth): void { $this->userAuth = $userAuth; }