MAPG-140 add UserConfirmation model and repository and adapt SignupController
This commit is contained in:
parent
bd46809d3a
commit
0bf6e900fe
@ -1,14 +1,13 @@
|
|||||||
<?php namespace MapGuesser\Controller;
|
<?php namespace MapGuesser\Controller;
|
||||||
|
|
||||||
use MapGuesser\Database\Query\Modify;
|
|
||||||
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\Mailing\Mail;
|
use MapGuesser\Mailing\Mail;
|
||||||
use MapGuesser\PersistentData\PersistentDataManager;
|
use MapGuesser\PersistentData\PersistentDataManager;
|
||||||
use MapGuesser\PersistentData\Model\User;
|
use MapGuesser\PersistentData\Model\User;
|
||||||
|
use MapGuesser\PersistentData\Model\UserConfirmation;
|
||||||
|
use MapGuesser\Repository\UserConfirmationRepository;
|
||||||
use MapGuesser\Repository\UserRepository;
|
use MapGuesser\Repository\UserRepository;
|
||||||
use MapGuesser\Response\HtmlContent;
|
use MapGuesser\Response\HtmlContent;
|
||||||
use MapGuesser\Response\JsonContent;
|
use MapGuesser\Response\JsonContent;
|
||||||
@ -22,11 +21,14 @@ class SignupController
|
|||||||
|
|
||||||
private UserRepository $userRepository;
|
private UserRepository $userRepository;
|
||||||
|
|
||||||
|
private UserConfirmationRepository $userConfirmationRepository;
|
||||||
|
|
||||||
public function __construct(IRequest $request)
|
public function __construct(IRequest $request)
|
||||||
{
|
{
|
||||||
$this->request = $request;
|
$this->request = $request;
|
||||||
$this->pdm = new PersistentDataManager();
|
$this->pdm = new PersistentDataManager();
|
||||||
$this->userRepository = new UserRepository();
|
$this->userRepository = new UserRepository();
|
||||||
|
$this->userConfirmationRepository = new UserConfirmationRepository();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSignupForm()
|
public function getSignupForm()
|
||||||
@ -83,10 +85,11 @@ class SignupController
|
|||||||
|
|
||||||
$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');
|
$confirmation = new UserConfirmation();
|
||||||
$modify->set('user_id', $user->getId());
|
$confirmation->setUser($user);
|
||||||
$modify->set('token', $token);
|
$confirmation->setToken($token);
|
||||||
$modify->save();
|
|
||||||
|
$this->pdm->saveToDb($confirmation);
|
||||||
|
|
||||||
\Container::$dbConnection->commit();
|
\Container::$dbConnection->commit();
|
||||||
|
|
||||||
@ -102,11 +105,7 @@ class SignupController
|
|||||||
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
|
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
$select = new Select(\Container::$dbConnection, 'user_confirmations');
|
$confirmation = $this->userConfirmationRepository->getByToken($this->request->query('token'));
|
||||||
$select->columns(['id', 'user_id']);
|
|
||||||
$select->where('token', '=', $this->request->query('token'));
|
|
||||||
|
|
||||||
$confirmation = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
|
||||||
|
|
||||||
if ($confirmation === null) {
|
if ($confirmation === null) {
|
||||||
$data = [];
|
$data = [];
|
||||||
@ -115,11 +114,9 @@ class SignupController
|
|||||||
|
|
||||||
\Container::$dbConnection->startTransaction();
|
\Container::$dbConnection->startTransaction();
|
||||||
|
|
||||||
$modify = new Modify(\Container::$dbConnection, 'user_confirmations');
|
$this->pdm->deleteFromDb($confirmation);
|
||||||
$modify->setId($confirmation['id']);
|
|
||||||
$modify->delete();
|
|
||||||
|
|
||||||
$user = $this->userRepository->getById($confirmation['user_id']);
|
$user = $this->userRepository->getById($confirmation->getUserId());
|
||||||
$user->setActive(true);
|
$user->setActive(true);
|
||||||
|
|
||||||
$this->pdm->saveToDb($user);
|
$this->pdm->saveToDb($user);
|
||||||
@ -137,11 +134,7 @@ class SignupController
|
|||||||
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
|
return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY);
|
||||||
}
|
}
|
||||||
|
|
||||||
$select = new Select(\Container::$dbConnection, 'user_confirmations');
|
$confirmation = $this->userConfirmationRepository->getByToken($this->request->query('token'));
|
||||||
$select->columns(['id', 'user_id']);
|
|
||||||
$select->where('token', '=', $this->request->query('token'));
|
|
||||||
|
|
||||||
$confirmation = $select->execute()->fetch(IResultSet::FETCH_ASSOC);
|
|
||||||
|
|
||||||
if ($confirmation === null) {
|
if ($confirmation === null) {
|
||||||
$data = ['success' => false];
|
$data = ['success' => false];
|
||||||
@ -150,11 +143,9 @@ class SignupController
|
|||||||
|
|
||||||
\Container::$dbConnection->startTransaction();
|
\Container::$dbConnection->startTransaction();
|
||||||
|
|
||||||
$modify = new Modify(\Container::$dbConnection, 'user_confirmations');
|
$this->pdm->deleteFromDb($confirmation);
|
||||||
$modify->setId($confirmation['id']);
|
|
||||||
$modify->delete();
|
|
||||||
|
|
||||||
$user = $this->userRepository->getById($confirmation['user_id']);
|
$user = $this->userRepository->getById($confirmation->getUserId());
|
||||||
|
|
||||||
$this->pdm->deleteFromDb($user);
|
$this->pdm->deleteFromDb($user);
|
||||||
|
|
||||||
|
44
src/PersistentData/Model/UserConfirmation.php
Normal file
44
src/PersistentData/Model/UserConfirmation.php
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
<?php namespace MapGuesser\PersistentData\Model;
|
||||||
|
|
||||||
|
class UserConfirmation extends Model
|
||||||
|
{
|
||||||
|
protected static string $table = 'user_confirmations';
|
||||||
|
|
||||||
|
protected static array $fields = ['user_id', 'token'];
|
||||||
|
|
||||||
|
private ?User $user = null;
|
||||||
|
|
||||||
|
private ?int $userId = null;
|
||||||
|
|
||||||
|
private string $token = '';
|
||||||
|
|
||||||
|
public function setUser(User $user): void
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUserId(int $userId): void
|
||||||
|
{
|
||||||
|
$this->userId = $userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setToken(string $token): void
|
||||||
|
{
|
||||||
|
$this->token = $token;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUser(): ?User
|
||||||
|
{
|
||||||
|
return $this->user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUserId(): ?int
|
||||||
|
{
|
||||||
|
return $this->userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getToken(): string
|
||||||
|
{
|
||||||
|
return $this->token;
|
||||||
|
}
|
||||||
|
}
|
28
src/Repository/UserConfirmationRepository.php
Normal file
28
src/Repository/UserConfirmationRepository.php
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?php namespace MapGuesser\Repository;
|
||||||
|
|
||||||
|
use MapGuesser\Database\Query\Select;
|
||||||
|
use MapGuesser\PersistentData\Model\UserConfirmation;
|
||||||
|
use MapGuesser\PersistentData\PersistentDataManager;
|
||||||
|
|
||||||
|
class UserConfirmationRepository
|
||||||
|
{
|
||||||
|
private PersistentDataManager $pdm;
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->pdm = new PersistentDataManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getById(int $userConfirmationId): ?UserConfirmation
|
||||||
|
{
|
||||||
|
return $this->pdm->selectFromDbById($userConfirmationId, UserConfirmation::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getByToken(string $token): ?UserConfirmation
|
||||||
|
{
|
||||||
|
$select = new Select(\Container::$dbConnection);
|
||||||
|
$select->where('token', '=', $token);
|
||||||
|
|
||||||
|
return $this->pdm->selectFromDb($select, UserConfirmation::class);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user