From d6501d36d5fe7aea1564ef685c35a1923d32a8a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 24 Sep 2023 00:44:44 +0200 Subject: [PATCH] make it possible to change email and username --- src/Controller/UserController.php | 30 ++++++++++++++++++++++++++++++ views/account/account.php | 8 ++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 2058690..1f0d5cf 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -8,6 +8,7 @@ use SokoWeb\Interfaces\Response\IRedirect; use SokoWeb\OAuth\GoogleOAuth; use MapGuesser\PersistentData\Model\User; use MapGuesser\Repository\GuessRepository; +use MapGuesser\Repository\UserRepository; use MapGuesser\Repository\UserConfirmationRepository; use MapGuesser\Repository\UserInChallengeRepository; use MapGuesser\Repository\UserPasswordResetterRepository; @@ -19,6 +20,8 @@ use SokoWeb\Util\JwtParser; class UserController implements IAuthenticationRequired { + private UserRepository $userRepository; + private UserConfirmationRepository $userConfirmationRepository; private UserPasswordResetterRepository $userPasswordResetterRepository; @@ -31,6 +34,7 @@ class UserController implements IAuthenticationRequired public function __construct() { + $this->userRepository = new UserRepository(); $this->userConfirmationRepository = new UserConfirmationRepository(); $this->userPasswordResetterRepository = new UserPasswordResetterRepository(); $this->userPlayedPlaceRepository = new UserPlayedPlaceRepository(); @@ -148,6 +152,32 @@ class UserController implements IAuthenticationRequired return new JsonContent(['error' => ['errorText' => $error]]); } + $newEmail = \Container::$request->post('email'); + if ($newEmail !== $user->getEmail()) { + if (!filter_var($newEmail, FILTER_VALIDATE_EMAIL)) { + return new JsonContent(['error' => ['errorText' => 'The given email address is not valid.']]); + } + + if ($this->userRepository->getByEmail($newEmail) !== null) { + return new JsonContent(['error' => ['errorText' => 'The given email address belongs to another account.']]); + } + + $user->setEmail($newEmail); + } + + $newUsername = \Container::$request->post('username'); + if (strlen($newUsername) > 0 && $newUsername !== $user->getUsername()) { + if (filter_var($newUsername, FILTER_VALIDATE_EMAIL)) { + return new JsonContent(['error' => ['errorText' => 'Please select a username that is not a valid email address.']]); + } + + if ($this->userRepository->getByUsername($newUsername) !== null) { + return new JsonContent(['error' => ['errorText' => 'The given username is already taken.']]); + } + + $user->setUsername($newUsername); + } + if (strlen(\Container::$request->post('password_new')) > 0) { if (strlen(\Container::$request->post('password_new')) < 6) { return new JsonContent([ diff --git a/views/account/account.php b/views/account/account.php index c709d48..a2a31f1 100644 --- a/views/account/account.php +++ b/views/account/account.php @@ -5,11 +5,11 @@ @section(main)

Account

-
+

Please confirm your identity with your password or with Google to modify your account.

-
@@ -23,8 +23,8 @@

- - + +