MAPG-140 refactor user handling to use the PersistentDataManager
This commit is contained in:
parent
d6750777c2
commit
a9eec05288
@ -1,7 +1,7 @@
|
||||
<?php namespace MapGuesser\Cli;
|
||||
|
||||
use MapGuesser\Database\Query\Modify;
|
||||
use MapGuesser\Model\User;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
use MapGuesser\PersistentData\Model\User;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
@ -20,10 +20,8 @@ class AddUserCommand extends Command
|
||||
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$user = new User([
|
||||
'email' => $input->getArgument('email'),
|
||||
]);
|
||||
|
||||
$user = new User();
|
||||
$user->setEmail($input->getArgument('email'));
|
||||
$user->setPlainPassword($input->getArgument('password'));
|
||||
|
||||
if ($input->hasArgument('type')) {
|
||||
@ -31,9 +29,8 @@ class AddUserCommand extends Command
|
||||
}
|
||||
|
||||
try {
|
||||
$modify = new Modify(\Container::$dbConnection, 'users');
|
||||
$modify->fill($user->toArray());
|
||||
$modify->save();
|
||||
$pdm = new PersistentDataManager();
|
||||
$pdm->saveToDb($user);
|
||||
} catch (\Exception $e) {
|
||||
$output->writeln('<error>Adding user failed!</error>');
|
||||
$output->writeln('');
|
||||
|
@ -1,11 +1,9 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\Interfaces\Database\IResultSet;
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\Interfaces\Response\IRedirect;
|
||||
use MapGuesser\Model\User;
|
||||
use MapGuesser\Repository\UserRepository;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Response\Redirect;
|
||||
@ -14,9 +12,12 @@ class LoginController
|
||||
{
|
||||
private IRequest $request;
|
||||
|
||||
private UserRepository $userRepository;
|
||||
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->userRepository = new UserRepository();
|
||||
}
|
||||
|
||||
public function getLoginForm()
|
||||
@ -40,19 +41,13 @@ class LoginController
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
$select = new Select(\Container::$dbConnection, 'users');
|
||||
$select->columns(User::getFields());
|
||||
$select->where('email', '=', $this->request->post('email'));
|
||||
$user = $this->userRepository->getByEmail($this->request->post('email'));
|
||||
|
||||
$userData = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||
|
||||
if ($userData === null) {
|
||||
if ($user === null) {
|
||||
$data = ['error' => 'user_not_found'];
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
$user = new User($userData);
|
||||
|
||||
if (!$user->getActive()) {
|
||||
$data = ['error' => 'user_not_active'];
|
||||
return new JsonContent($data);
|
||||
|
@ -7,7 +7,9 @@ use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\Interfaces\Response\IRedirect;
|
||||
use MapGuesser\Mailing\Mail;
|
||||
use MapGuesser\Model\User;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
use MapGuesser\PersistentData\Model\User;
|
||||
use MapGuesser\Repository\UserRepository;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
use MapGuesser\Response\Redirect;
|
||||
@ -16,9 +18,15 @@ class SignupController
|
||||
{
|
||||
private IRequest $request;
|
||||
|
||||
private PersistentDataManager $pdm;
|
||||
|
||||
private UserRepository $userRepository;
|
||||
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->pdm = new PersistentDataManager();
|
||||
$this->userRepository = new UserRepository();
|
||||
}
|
||||
|
||||
public function getSignupForm()
|
||||
@ -48,15 +56,9 @@ class SignupController
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
$select = new Select(\Container::$dbConnection, 'users');
|
||||
$select->columns(User::getFields());
|
||||
$select->where('email', '=', $this->request->post('email'));
|
||||
|
||||
$userData = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||
|
||||
if ($userData !== null) {
|
||||
$user = new User($userData);
|
||||
$user = $this->userRepository->getByEmail($this->request->post('email'));
|
||||
|
||||
if ($user !== null) {
|
||||
if ($user->getActive()) {
|
||||
$data = ['error' => 'user_found'];
|
||||
} else {
|
||||
@ -75,23 +77,18 @@ class SignupController
|
||||
return new JsonContent($data);
|
||||
}
|
||||
|
||||
$user = new User([
|
||||
'email' => $this->request->post('email'),
|
||||
]);
|
||||
|
||||
$user = new User();
|
||||
$user->setEmail($this->request->post('email'));
|
||||
$user->setPlainPassword($this->request->post('password'));
|
||||
|
||||
\Container::$dbConnection->startTransaction();
|
||||
|
||||
$modify = new Modify(\Container::$dbConnection, 'users');
|
||||
$modify->fill($user->toArray());
|
||||
$modify->save();
|
||||
$userId = $modify->getId();
|
||||
$this->pdm->saveToDb($user);
|
||||
|
||||
$token = hash('sha256', serialize($user) . random_bytes(10) . microtime());
|
||||
|
||||
$modify = new Modify(\Container::$dbConnection, 'user_confirmations');
|
||||
$modify->set('user_id', $userId);
|
||||
$modify->set('user_id', $user->getId());
|
||||
$modify->set('token', $token);
|
||||
$modify->save();
|
||||
|
||||
@ -128,20 +125,13 @@ class SignupController
|
||||
$modify->setId($confirmation['id']);
|
||||
$modify->delete();
|
||||
|
||||
$modify = new Modify(\Container::$dbConnection, 'users');
|
||||
$modify->setId($confirmation['user_id']);
|
||||
$modify->set('active', true);
|
||||
$modify->save();
|
||||
$user = $this->userRepository->getById($confirmation['user_id']);
|
||||
$user->setActive(true);
|
||||
|
||||
$this->pdm->saveToDb($user);
|
||||
|
||||
\Container::$dbConnection->commit();
|
||||
|
||||
$select = new Select(\Container::$dbConnection, 'users');
|
||||
$select->columns(User::getFields());
|
||||
$select->whereId($confirmation['user_id']);
|
||||
|
||||
$userData = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
||||
$user = new User($userData);
|
||||
|
||||
$session->set('user', $user);
|
||||
|
||||
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
|
||||
@ -172,9 +162,9 @@ class SignupController
|
||||
$modify->setId($confirmation['id']);
|
||||
$modify->delete();
|
||||
|
||||
$modify = new Modify(\Container::$dbConnection, 'users');
|
||||
$modify->setId($confirmation['user_id']);
|
||||
$modify->delete();
|
||||
$user = $this->userRepository->getById($confirmation['user_id']);
|
||||
|
||||
$this->pdm->deleteFromDb($user);
|
||||
|
||||
\Container::$dbConnection->commit();
|
||||
|
||||
|
@ -1,9 +1,10 @@
|
||||
<?php namespace MapGuesser\Controller;
|
||||
|
||||
use MapGuesser\Database\Query\Modify;
|
||||
use MapGuesser\Interfaces\Authorization\ISecured;
|
||||
use MapGuesser\Interfaces\Request\IRequest;
|
||||
use MapGuesser\Interfaces\Response\IContent;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
use MapGuesser\PersistentData\Model\User;
|
||||
use MapGuesser\Response\HtmlContent;
|
||||
use MapGuesser\Response\JsonContent;
|
||||
|
||||
@ -11,9 +12,12 @@ class UserController implements ISecured
|
||||
{
|
||||
private IRequest $request;
|
||||
|
||||
private PersistentDataManager $pdm;
|
||||
|
||||
public function __construct(IRequest $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->pdm = new PersistentDataManager();
|
||||
}
|
||||
|
||||
public function authorize(): bool
|
||||
@ -25,6 +29,9 @@ class UserController implements ISecured
|
||||
|
||||
public function getProfile(): IContent
|
||||
{
|
||||
/**
|
||||
* @var User $user
|
||||
*/
|
||||
$user = $this->request->user();
|
||||
|
||||
$data = ['user' => $user->toArray()];
|
||||
@ -33,6 +40,9 @@ class UserController implements ISecured
|
||||
|
||||
public function saveProfile(): IContent
|
||||
{
|
||||
/**
|
||||
* @var User $user
|
||||
*/
|
||||
$user = $this->request->user();
|
||||
|
||||
if (!$user->checkPassword($this->request->post('password'))) {
|
||||
@ -54,9 +64,7 @@ class UserController implements ISecured
|
||||
$user->setPlainPassword($this->request->post('password_new'));
|
||||
}
|
||||
|
||||
$modify = new Modify(\Container::$dbConnection, 'users');
|
||||
$modify->fill($user->toArray());
|
||||
$modify->save();
|
||||
$this->pdm->saveToDb($user);
|
||||
|
||||
$this->request->session()->set('user', $user);
|
||||
|
||||
|
@ -4,15 +4,15 @@ use MapGuesser\Interfaces\Authentication\IUser;
|
||||
|
||||
class User extends Model implements IUser
|
||||
{
|
||||
private static array $types = ['user', 'admin'];
|
||||
|
||||
protected static string $table = 'users';
|
||||
|
||||
protected static array $fields = ['email', 'password', 'type', 'active'];
|
||||
|
||||
private string $email;
|
||||
private static array $types = ['user', 'admin'];
|
||||
|
||||
private string $password;
|
||||
private string $email = '';
|
||||
|
||||
private string $password = '';
|
||||
|
||||
private string $type = 'user';
|
||||
|
||||
|
28
src/Repository/UserRepository.php
Normal file
28
src/Repository/UserRepository.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php namespace MapGuesser\Repository;
|
||||
|
||||
use MapGuesser\Database\Query\Select;
|
||||
use MapGuesser\PersistentData\Model\User;
|
||||
use MapGuesser\PersistentData\PersistentDataManager;
|
||||
|
||||
class UserRepository
|
||||
{
|
||||
private PersistentDataManager $pdm;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->pdm = new PersistentDataManager();
|
||||
}
|
||||
|
||||
public function getById(int $userId): ?User
|
||||
{
|
||||
return $this->pdm->selectFromDbById($userId, User::class);
|
||||
}
|
||||
|
||||
public function getByEmail(string $email): ?User
|
||||
{
|
||||
$select = new Select(\Container::$dbConnection);
|
||||
$select->where('email', '=', $email);
|
||||
|
||||
return $this->pdm->selectFromDb($select, User::class);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user