From 0bf6e900feb3c85f2f67dc6dc8b50b9379806b77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sat, 20 Jun 2020 00:41:31 +0200 Subject: [PATCH] MAPG-140 add UserConfirmation model and repository and adapt SignupController --- src/Controller/SignupController.php | 41 +++++++---------- src/PersistentData/Model/UserConfirmation.php | 44 +++++++++++++++++++ src/Repository/UserConfirmationRepository.php | 28 ++++++++++++ 3 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 src/PersistentData/Model/UserConfirmation.php create mode 100644 src/Repository/UserConfirmationRepository.php diff --git a/src/Controller/SignupController.php b/src/Controller/SignupController.php index 32a54d1..77b3197 100644 --- a/src/Controller/SignupController.php +++ b/src/Controller/SignupController.php @@ -1,14 +1,13 @@ request = $request; $this->pdm = new PersistentDataManager(); $this->userRepository = new UserRepository(); + $this->userConfirmationRepository = new UserConfirmationRepository(); } public function getSignupForm() @@ -83,10 +85,11 @@ class SignupController $token = hash('sha256', serialize($user) . random_bytes(10) . microtime()); - $modify = new Modify(\Container::$dbConnection, 'user_confirmations'); - $modify->set('user_id', $user->getId()); - $modify->set('token', $token); - $modify->save(); + $confirmation = new UserConfirmation(); + $confirmation->setUser($user); + $confirmation->setToken($token); + + $this->pdm->saveToDb($confirmation); \Container::$dbConnection->commit(); @@ -102,11 +105,7 @@ class SignupController return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY); } - $select = new Select(\Container::$dbConnection, 'user_confirmations'); - $select->columns(['id', 'user_id']); - $select->where('token', '=', $this->request->query('token')); - - $confirmation = $select->execute()->fetch(IResultSet::FETCH_ASSOC); + $confirmation = $this->userConfirmationRepository->getByToken($this->request->query('token')); if ($confirmation === null) { $data = []; @@ -115,11 +114,9 @@ class SignupController \Container::$dbConnection->startTransaction(); - $modify = new Modify(\Container::$dbConnection, 'user_confirmations'); - $modify->setId($confirmation['id']); - $modify->delete(); + $this->pdm->deleteFromDb($confirmation); - $user = $this->userRepository->getById($confirmation['user_id']); + $user = $this->userRepository->getById($confirmation->getUserId()); $user->setActive(true); $this->pdm->saveToDb($user); @@ -137,11 +134,7 @@ class SignupController return new Redirect([\Container::$routeCollection->getRoute('index'), []], IRedirect::TEMPORARY); } - $select = new Select(\Container::$dbConnection, 'user_confirmations'); - $select->columns(['id', 'user_id']); - $select->where('token', '=', $this->request->query('token')); - - $confirmation = $select->execute()->fetch(IResultSet::FETCH_ASSOC); + $confirmation = $this->userConfirmationRepository->getByToken($this->request->query('token')); if ($confirmation === null) { $data = ['success' => false]; @@ -150,11 +143,9 @@ class SignupController \Container::$dbConnection->startTransaction(); - $modify = new Modify(\Container::$dbConnection, 'user_confirmations'); - $modify->setId($confirmation['id']); - $modify->delete(); + $this->pdm->deleteFromDb($confirmation); - $user = $this->userRepository->getById($confirmation['user_id']); + $user = $this->userRepository->getById($confirmation->getUserId()); $this->pdm->deleteFromDb($user); diff --git a/src/PersistentData/Model/UserConfirmation.php b/src/PersistentData/Model/UserConfirmation.php new file mode 100644 index 0000000..70a76d6 --- /dev/null +++ b/src/PersistentData/Model/UserConfirmation.php @@ -0,0 +1,44 @@ +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; + } +} diff --git a/src/Repository/UserConfirmationRepository.php b/src/Repository/UserConfirmationRepository.php new file mode 100644 index 0000000..62df3ae --- /dev/null +++ b/src/Repository/UserConfirmationRepository.php @@ -0,0 +1,28 @@ +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); + } +}