make it possible to user username

This commit is contained in:
Bence Pőcze 2023-04-09 01:35:40 +02:00
parent 366abf61b3
commit 265a687a70
Signed by: bence
GPG Key ID: DC5BD6E95A333E6D
7 changed files with 44 additions and 6 deletions

View File

@ -0,0 +1,3 @@
ALTER TABLE `users`
ADD `username` varchar(100) DEFAULT NULL,
ADD UNIQUE `username` (`username`);

View File

@ -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('<error>Please provide a valid email address.</error>');
return 1;
}
$user = new User();
$user->setEmail($input->getArgument('email'));
$user->setPlainPassword($input->getArgument('password'));

View File

@ -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 <a href="/password/requestReset?email=' .
'errorText' => 'No user found with the given email address / username or the given password is wrong. You can <a href="/password/requestReset?email=' .
urlencode($this->request->post('email')) . '" title="Request password reset">request password reset</a>!'
]
]);
@ -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.'
]
]);
}

View File

@ -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;

View File

@ -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);

View File

@ -25,6 +25,7 @@
<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" value="<?= $user['email'] ?>" disabled>
<input type="text" class="text big fullWidth marginTop" name="username" placeholder="Username" value="<?= $user['username'] ?>" disabled>
<input type="password" class="text big fullWidth marginTop" name="password_new" placeholder="New password" minlength="6">
<input type="password" class="text big fullWidth marginTop" name="password_new_confirm" placeholder="New password confirmation" minlength="6">
<p id="accountFormError" class="formError justify marginTop"></p>

View File

@ -4,7 +4,7 @@
<h2>Login</h2>
<div class="box">
<form id="loginForm" action="/login" method="post" data-redirect-on-success="<?= $redirectUrl ?>">
<input type="email" class="text big fullWidth" name="email" placeholder="Email address" required autofocus>
<input type="text" class="text big fullWidth" name="email" placeholder="Email address / Username" required autofocus>
<input type="password" class="text big fullWidth marginTop" name="password" placeholder="Password" required minlength="6">
<p id="loginFormError" class="formError justify marginTop"></p>
<div class="right marginTop">