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