feature/user-data #11

Merged
bence merged 8 commits from feature/user-data into master 2023-04-09 02:59:36 +02:00
7 changed files with 44 additions and 6 deletions
Showing only changes of commit 151112bd2a - Show all commits

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 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 = new User();
$user->setEmail($input->getArgument('email')); $user->setEmail($input->getArgument('email'));
$user->setPlainPassword($input->getArgument('password')); $user->setPlainPassword($input->getArgument('password'));

View File

@ -109,11 +109,11 @@ class LoginController
return new JsonContent(['success' => true]); 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'))) { if ($user === null || !$user->checkPassword($this->request->post('password'))) {
return new JsonContent([ return new JsonContent([
'error' => [ '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>!' 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) { if ($user === null) {
return new JsonContent([ return new JsonContent([
'error' => [ '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 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 static array $types = ['user', 'admin'];
private string $email = ''; private string $email = '';
private ?string $username = null;
private ?string $password = null; private ?string $password = null;
private string $type = 'user'; private string $type = 'user';
@ -27,6 +29,11 @@ class User extends Model implements IUser
$this->email = $email; $this->email = $email;
} }
public function setUsername(?string $username): void
{
$this->username = $username;
}
public function setPassword(?string $hashedPassword): void public function setPassword(?string $hashedPassword): void
{ {
$this->password = $hashedPassword; $this->password = $hashedPassword;
@ -64,6 +71,11 @@ class User extends Model implements IUser
return $this->email; return $this->email;
} }
public function getUsername(): ?string
{
return $this->username;
}
public function getPassword(): ?string public function getPassword(): ?string
{ {
return $this->password; return $this->password;

View File

@ -27,6 +27,23 @@ class UserRepository implements IUserRepository
return $this->pdm->selectFromDb($select, User::class); 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 public function getByGoogleSub(string $sub): ?User
{ {
$select = new Select(\Container::$dbConnection); $select = new Select(\Container::$dbConnection);

View File

@ -25,6 +25,7 @@
<hr> <hr>
<?php /* TODO: disabled for the time being, email modification should be implemented */ ?> <?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="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" placeholder="New password" minlength="6">
<input type="password" class="text big fullWidth marginTop" name="password_new_confirm" placeholder="New password confirmation" 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> <p id="accountFormError" class="formError justify marginTop"></p>

View File

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