From 67534a22f5425904e44933602df6c23733544654 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Sun, 5 Jul 2020 00:03:56 +0200 Subject: [PATCH] MAPG-141 add password resetter table, model and repository --- .../20200704_1954_password_forgotten.sql | 10 +++ .../Model/UserPasswordResetter.php | 70 +++++++++++++++++++ .../UserPasswordResetterRepository.php | 38 ++++++++++ 3 files changed, 118 insertions(+) create mode 100644 database/migrations/structure/20200704_1954_password_forgotten.sql create mode 100644 src/PersistentData/Model/UserPasswordResetter.php create mode 100644 src/Repository/UserPasswordResetterRepository.php diff --git a/database/migrations/structure/20200704_1954_password_forgotten.sql b/database/migrations/structure/20200704_1954_password_forgotten.sql new file mode 100644 index 0000000..38ba86e --- /dev/null +++ b/database/migrations/structure/20200704_1954_password_forgotten.sql @@ -0,0 +1,10 @@ +CREATE TABLE `user_password_resetters` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `user_id` int(10) unsigned NOT NULL, + `token` varchar(32) CHARACTER SET ascii NOT NULL, + `expires` timestamp NOT NULL, + PRIMARY KEY (`id`), + KEY `user_id` (`user_id`), + KEY `token` (`token`), + CONSTRAINT `user_password_resetters_user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) +) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4; diff --git a/src/PersistentData/Model/UserPasswordResetter.php b/src/PersistentData/Model/UserPasswordResetter.php new file mode 100644 index 0000000..ee28cdf --- /dev/null +++ b/src/PersistentData/Model/UserPasswordResetter.php @@ -0,0 +1,70 @@ + User::class]; + + private ?User $user = null; + + private ?int $userId = null; + + private string $token = ''; + + private DateTime $expires; + + 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 setExpiresDate(DateTime $expires): void + { + $this->expires = $expires; + } + + public function setExpires(string $expires): void + { + $this->expires = new DateTime($expires); + } + + public function getUser(): ?User + { + return $this->user; + } + + public function getUserId(): ?int + { + return $this->userId; + } + + public function getToken(): string + { + return $this->token; + } + + public function getExpiresDate(): DateTime + { + return $this->expires; + } + + public function getExpires(): string + { + return $this->expires->format('Y-m-d H:i:s'); + } +} diff --git a/src/Repository/UserPasswordResetterRepository.php b/src/Repository/UserPasswordResetterRepository.php new file mode 100644 index 0000000..04a82af --- /dev/null +++ b/src/Repository/UserPasswordResetterRepository.php @@ -0,0 +1,38 @@ +pdm = new PersistentDataManager(); + } + + public function getById(int $userConfirmationId): ?UserPasswordResetter + { + return $this->pdm->selectFromDbById($userConfirmationId, UserPasswordResetter::class); + } + + public function getByToken(string $token): ?UserPasswordResetter + { + $select = new Select(\Container::$dbConnection); + $select->where('token', '=', $token); + + return $this->pdm->selectFromDb($select, UserPasswordResetter::class); + } + + public function getByUser(User $user): Generator + { + $select = new Select(\Container::$dbConnection); + $select->where('user_id', '=', $user->getId()); + + yield from $this->pdm->selectMultipleFromDb($select, UserPasswordResetter::class); + } +}