2020-06-14 20:15:53 +02:00
|
|
|
<?php namespace MapGuesser\Controller;
|
|
|
|
|
2020-06-25 20:26:33 +02:00
|
|
|
use MapGuesser\Database\Query\Select;
|
2020-06-14 21:04:20 +02:00
|
|
|
use MapGuesser\Interfaces\Authorization\ISecured;
|
2020-06-25 20:26:33 +02:00
|
|
|
use MapGuesser\Interfaces\Database\IResultSet;
|
2020-06-14 20:15:53 +02:00
|
|
|
use MapGuesser\Interfaces\Request\IRequest;
|
|
|
|
use MapGuesser\Interfaces\Response\IContent;
|
2020-06-18 00:21:18 +02:00
|
|
|
use MapGuesser\PersistentData\PersistentDataManager;
|
|
|
|
use MapGuesser\PersistentData\Model\User;
|
2020-06-25 20:26:33 +02:00
|
|
|
use MapGuesser\PersistentData\Model\UserConfirmation;
|
|
|
|
use MapGuesser\Repository\UserConfirmationRepository;
|
2020-06-14 20:15:53 +02:00
|
|
|
use MapGuesser\Response\HtmlContent;
|
|
|
|
use MapGuesser\Response\JsonContent;
|
|
|
|
|
2020-06-14 21:04:20 +02:00
|
|
|
class UserController implements ISecured
|
2020-06-14 20:15:53 +02:00
|
|
|
{
|
|
|
|
private IRequest $request;
|
|
|
|
|
2020-06-18 00:21:18 +02:00
|
|
|
private PersistentDataManager $pdm;
|
|
|
|
|
2020-06-25 20:26:33 +02:00
|
|
|
private UserConfirmationRepository $userConfirmationRepository;
|
|
|
|
|
2020-06-14 20:15:53 +02:00
|
|
|
public function __construct(IRequest $request)
|
|
|
|
{
|
|
|
|
$this->request = $request;
|
2020-06-18 00:21:18 +02:00
|
|
|
$this->pdm = new PersistentDataManager();
|
2020-06-25 20:26:33 +02:00
|
|
|
$this->userConfirmationRepository = new UserConfirmationRepository();
|
2020-06-14 20:15:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 21:04:20 +02:00
|
|
|
public function authorize(): bool
|
|
|
|
{
|
|
|
|
$user = $this->request->user();
|
|
|
|
|
|
|
|
return $user !== null;
|
|
|
|
}
|
|
|
|
|
2020-06-25 16:44:34 +02:00
|
|
|
public function getAccount(): IContent
|
2020-06-14 20:15:53 +02:00
|
|
|
{
|
2020-06-18 00:21:18 +02:00
|
|
|
/**
|
|
|
|
* @var User $user
|
|
|
|
*/
|
2020-06-14 20:15:53 +02:00
|
|
|
$user = $this->request->user();
|
|
|
|
|
|
|
|
$data = ['user' => $user->toArray()];
|
2020-06-25 20:26:33 +02:00
|
|
|
return new HtmlContent('account/account', $data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDeleteAccount(): IContent
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var User $user
|
|
|
|
*/
|
|
|
|
$user = $this->request->user();
|
|
|
|
|
|
|
|
$data = ['user' => $user->toArray()];
|
|
|
|
return new HtmlContent('account/delete', $data);
|
2020-06-14 20:15:53 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 16:44:34 +02:00
|
|
|
public function saveAccount(): IContent
|
2020-06-14 20:15:53 +02:00
|
|
|
{
|
2020-06-18 00:21:18 +02:00
|
|
|
/**
|
|
|
|
* @var User $user
|
|
|
|
*/
|
2020-06-14 20:15:53 +02:00
|
|
|
$user = $this->request->user();
|
|
|
|
|
|
|
|
if (!$user->checkPassword($this->request->post('password'))) {
|
2020-06-25 20:26:33 +02:00
|
|
|
$data = ['error' => ['errorText' => 'The given current password is wrong.']];
|
2020-06-14 20:15:53 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($this->request->post('password_new')) > 0) {
|
|
|
|
if (strlen($this->request->post('password_new')) < 6) {
|
2020-06-25 20:26:33 +02:00
|
|
|
$data = ['error' => ['errorText' => 'The given new password is too short. Please choose a password that is at least 6 characters long!']];
|
2020-06-14 20:15:53 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->request->post('password_new') !== $this->request->post('password_new_confirm')) {
|
2020-06-25 20:26:33 +02:00
|
|
|
$data = ['error' => ['errorText' => 'The given new passwords do not match.']];
|
2020-06-14 20:15:53 +02:00
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->setPlainPassword($this->request->post('password_new'));
|
|
|
|
}
|
|
|
|
|
2020-06-18 00:21:18 +02:00
|
|
|
$this->pdm->saveToDb($user);
|
2020-06-14 20:15:53 +02:00
|
|
|
|
|
|
|
$data = ['success' => true];
|
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
2020-06-25 20:26:33 +02:00
|
|
|
|
|
|
|
public function deleteAccount(): IContent
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var User $user
|
|
|
|
*/
|
|
|
|
$user = $this->request->user();
|
|
|
|
|
|
|
|
if (!$user->checkPassword($this->request->post('password'))) {
|
|
|
|
$data = ['error' => ['errorText' => 'The given current password is wrong.']];
|
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
\Container::$dbConnection->startTransaction();
|
|
|
|
|
|
|
|
foreach ($this->userConfirmationRepository->getByUser($user) as $userConfirmation) {
|
|
|
|
$this->pdm->deleteFromDb($userConfirmation);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->pdm->deleteFromDb($user);
|
|
|
|
|
|
|
|
\Container::$dbConnection->commit();
|
|
|
|
|
|
|
|
$data = ['success' => true];
|
|
|
|
return new JsonContent($data);
|
|
|
|
}
|
2020-06-14 20:15:53 +02:00
|
|
|
}
|