From 265a687a70469eb9eb3b66fd03c2de0070a23305 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 9 Apr 2023 01:35:40 +0200 Subject: [PATCH] make it possible to user username --- .../structure/20230409_0101_username.sql | 3 +++ src/Cli/AddUserCommand.php | 5 +++++ src/Controller/LoginController.php | 8 ++++---- src/PersistentData/Model/User.php | 14 +++++++++++++- src/Repository/UserRepository.php | 17 +++++++++++++++++ views/account/account.php | 1 + views/login/login.php | 2 +- 7 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 database/migrations/structure/20230409_0101_username.sql diff --git a/database/migrations/structure/20230409_0101_username.sql b/database/migrations/structure/20230409_0101_username.sql new file mode 100644 index 0000000..adfcb72 --- /dev/null +++ b/database/migrations/structure/20230409_0101_username.sql @@ -0,0 +1,3 @@ +ALTER TABLE `users` +ADD `username` varchar(100) DEFAULT NULL, +ADD UNIQUE `username` (`username`); diff --git a/src/Cli/AddUserCommand.php b/src/Cli/AddUserCommand.php index 27a0d0b..980cb79 100644 --- a/src/Cli/AddUserCommand.php +++ b/src/Cli/AddUserCommand.php @@ -21,6 +21,11 @@ class AddUserCommand extends Command public function execute(InputInterface $input, OutputInterface $output): int { + if (!filter_var($input->getArgument('email'), FILTER_VALIDATE_EMAIL)) { + $output->writeln('Please provide a valid email address.'); + return 1; + } + $user = new User(); $user->setEmail($input->getArgument('email')); $user->setPlainPassword($input->getArgument('password')); diff --git a/src/Controller/LoginController.php b/src/Controller/LoginController.php index 34baae4..7a83d8c 100644 --- a/src/Controller/LoginController.php +++ b/src/Controller/LoginController.php @@ -109,11 +109,11 @@ class LoginController return new JsonContent(['success' => true]); } - $user = $this->userRepository->getByEmail($this->request->post('email')); + $user = $this->userRepository->getByEmailOrUsername($this->request->post('email')); if ($user === null || !$user->checkPassword($this->request->post('password'))) { return new JsonContent([ 'error' => [ - 'errorText' => 'No user found with the given email address or the given password is wrong. You can request password reset!' ] ]); @@ -200,11 +200,11 @@ class LoginController } } - $user = $this->userRepository->getByEmail($this->request->post('email')); + $user = $this->userRepository->getByEmailOrUsername($this->request->post('email')); if ($user === null) { return new JsonContent([ 'error' => [ - 'errorText' => 'No user found with the given email address.' + 'errorText' => 'No user found with the given email address / username.' ] ]); } diff --git a/src/PersistentData/Model/User.php b/src/PersistentData/Model/User.php index 1a284d4..92c4495 100644 --- a/src/PersistentData/Model/User.php +++ b/src/PersistentData/Model/User.php @@ -8,12 +8,14 @@ class User extends Model implements IUser { protected static string $table = 'users'; - protected static array $fields = ['email', 'password', 'type', 'google_sub', 'created']; + protected static array $fields = ['email', 'username', 'password', 'type', 'google_sub', 'created']; private static array $types = ['user', 'admin']; private string $email = ''; + private ?string $username = null; + private ?string $password = null; private string $type = 'user'; @@ -27,6 +29,11 @@ class User extends Model implements IUser $this->email = $email; } + public function setUsername(?string $username): void + { + $this->username = $username; + } + public function setPassword(?string $hashedPassword): void { $this->password = $hashedPassword; @@ -64,6 +71,11 @@ class User extends Model implements IUser return $this->email; } + public function getUsername(): ?string + { + return $this->username; + } + public function getPassword(): ?string { return $this->password; diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index cc03b44..3882d9d 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -27,6 +27,23 @@ class UserRepository implements IUserRepository return $this->pdm->selectFromDb($select, User::class); } + public function getByUsername(string $username): ?User + { + $select = new Select(\Container::$dbConnection); + $select->where('username', '=', $username); + + return $this->pdm->selectFromDb($select, User::class); + } + + public function getByEmailOrUsername(string $emailOrUsername): ?User + { + if (filter_var($emailOrUsername, FILTER_VALIDATE_EMAIL)) { + return $this->getByEmail($emailOrUsername); + } + + return $this->getByUsername($emailOrUsername); + } + public function getByGoogleSub(string $sub): ?User { $select = new Select(\Container::$dbConnection); diff --git a/views/account/account.php b/views/account/account.php index 315c71b..6a26290 100644 --- a/views/account/account.php +++ b/views/account/account.php @@ -25,6 +25,7 @@
+

diff --git a/views/login/login.php b/views/login/login.php index 6c3cc79..d8dabfb 100644 --- a/views/login/login.php +++ b/views/login/login.php @@ -4,7 +4,7 @@

Login

- +