make it possible to change email and username
This commit is contained in:
parent
2c706cc7f3
commit
36f4b6b4d0
@ -8,6 +8,7 @@ use SokoWeb\Interfaces\Response\IRedirect;
|
|||||||
use SokoWeb\OAuth\GoogleOAuth;
|
use SokoWeb\OAuth\GoogleOAuth;
|
||||||
use MapGuesser\PersistentData\Model\User;
|
use MapGuesser\PersistentData\Model\User;
|
||||||
use MapGuesser\Repository\GuessRepository;
|
use MapGuesser\Repository\GuessRepository;
|
||||||
|
use MapGuesser\Repository\UserRepository;
|
||||||
use MapGuesser\Repository\UserConfirmationRepository;
|
use MapGuesser\Repository\UserConfirmationRepository;
|
||||||
use MapGuesser\Repository\UserInChallengeRepository;
|
use MapGuesser\Repository\UserInChallengeRepository;
|
||||||
use MapGuesser\Repository\UserPasswordResetterRepository;
|
use MapGuesser\Repository\UserPasswordResetterRepository;
|
||||||
@ -19,6 +20,8 @@ use SokoWeb\Util\JwtParser;
|
|||||||
|
|
||||||
class UserController implements IAuthenticationRequired
|
class UserController implements IAuthenticationRequired
|
||||||
{
|
{
|
||||||
|
private UserRepository $userRepository;
|
||||||
|
|
||||||
private UserConfirmationRepository $userConfirmationRepository;
|
private UserConfirmationRepository $userConfirmationRepository;
|
||||||
|
|
||||||
private UserPasswordResetterRepository $userPasswordResetterRepository;
|
private UserPasswordResetterRepository $userPasswordResetterRepository;
|
||||||
@ -31,6 +34,7 @@ class UserController implements IAuthenticationRequired
|
|||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
|
$this->userRepository = new UserRepository();
|
||||||
$this->userConfirmationRepository = new UserConfirmationRepository();
|
$this->userConfirmationRepository = new UserConfirmationRepository();
|
||||||
$this->userPasswordResetterRepository = new UserPasswordResetterRepository();
|
$this->userPasswordResetterRepository = new UserPasswordResetterRepository();
|
||||||
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
|
$this->userPlayedPlaceRepository = new UserPlayedPlaceRepository();
|
||||||
@ -148,6 +152,32 @@ class UserController implements IAuthenticationRequired
|
|||||||
return new JsonContent(['error' => ['errorText' => $error]]);
|
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 (preg_match('/^[a-zA-Z0-9_\-\.]+$/', $newUsername) !== 1) {
|
||||||
|
return new JsonContent(['error' => ['errorText' => 'Username can contain only english letters, digits, - (hyphen), . (dot), _ (underscore).']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
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')) > 0) {
|
||||||
if (strlen(\Container::$request->post('password_new')) < 6) {
|
if (strlen(\Container::$request->post('password_new')) < 6) {
|
||||||
return new JsonContent([
|
return new JsonContent([
|
||||||
|
@ -5,11 +5,11 @@
|
|||||||
@section(main)
|
@section(main)
|
||||||
<h2>Account</h2>
|
<h2>Account</h2>
|
||||||
<div class="box">
|
<div class="box">
|
||||||
<form id="accountForm" action="/account" method="post" data-observe-inputs="password_new,password_new_confirm">
|
<form id="accountForm" action="/account" method="post" data-reload-on-success="true" data-observe-inputs="email,username,password_new,password_new_confirm">
|
||||||
<?php if ($user['password'] !== null && $user['google_sub'] !== null): ?>
|
<?php if ($user['password'] !== null && $user['google_sub'] !== null): ?>
|
||||||
<p class="justify small">Please confirm your identity with your password or with Google to modify your account.</p>
|
<p class="justify small">Please confirm your identity with your password or with Google to modify your account.</p>
|
||||||
<div class="inputWithButton">
|
<div class="inputWithButton">
|
||||||
<input type="password" class="text name="password" placeholder="Current password" autocomplete="current-password" required minlength="6" autofocus><!--
|
<input type="password" class="text" name="password" placeholder="Current password" autocomplete="current-password" required minlength="6" autofocus><!--
|
||||||
--><button id="authenticateWithGoogleButton" class="yellow" type="button">Google</button>
|
--><button id="authenticateWithGoogleButton" class="yellow" type="button">Google</button>
|
||||||
</div>
|
</div>
|
||||||
<?php elseif ($user['password'] !== null): ?>
|
<?php elseif ($user['password'] !== null): ?>
|
||||||
@ -23,8 +23,8 @@
|
|||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<hr>
|
<hr>
|
||||||
<?php /* TODO: disabled for the time being, email modification should be implemented */ ?>
|
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" autocomplete="username" value="<?= $user['email'] ?>">
|
||||||
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" autocomplete="username" value="<?= $user['email'] ?>" disabled>
|
<input type="username" class="text big fullWidth marginTop" name="username" placeholder="Username" value="<?= $user['username'] ?>">
|
||||||
<input type="password" class="text big fullWidth marginTop" name="password_new" placeholder="New password" autocomplete="new-password" minlength="6">
|
<input type="password" class="text big fullWidth marginTop" name="password_new" placeholder="New password" autocomplete="new-password" minlength="6">
|
||||||
<input type="password" class="text big fullWidth marginTop" name="password_new_confirm" placeholder="New password confirmation" autocomplete="new-password" minlength="6">
|
<input type="password" class="text big fullWidth marginTop" name="password_new_confirm" placeholder="New password confirmation" autocomplete="new-password" minlength="6">
|
||||||
<p id="accountFormError" class="formError justify marginTop"></p>
|
<p id="accountFormError" class="formError justify marginTop"></p>
|
||||||
|
Loading…
Reference in New Issue
Block a user