Part-DB.Part-DB-server/src/Services/UserSystem/PasswordResetManager.php

131 lines
4.7 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
2022-11-29 22:28:53 +01:00
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
2020-02-22 18:14:36 +01:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2020-01-05 15:46:58 +01:00
declare(strict_types=1);
namespace App\Services\UserSystem;
use App\Entity\UserSystem\User;
2022-08-14 19:32:53 +02:00
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
2021-10-02 20:41:14 +02:00
use Symfony\Component\PasswordHasher\Hasher\PasswordHasherFactoryInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
2022-09-18 22:59:31 +02:00
use Symfony\Component\PasswordHasher\PasswordHasherInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class PasswordResetManager
{
2022-09-18 22:59:31 +02:00
protected PasswordHasherInterface $passwordEncoder;
public function __construct(protected MailerInterface $mailer, protected EntityManagerInterface $em,
protected TranslatorInterface $translator, protected UserPasswordHasherInterface $userPasswordEncoder,
2021-10-02 20:41:14 +02:00
PasswordHasherFactoryInterface $encoderFactory)
{
2021-10-02 20:41:14 +02:00
$this->passwordEncoder = $encoderFactory->getPasswordHasher(User::class);
}
2020-01-04 20:24:09 +01:00
public function request(string $name_or_email): void
{
$repo = $this->em->getRepository(User::class);
//Try to find a user by the given string
$user = $repo->findByEmailOrName($name_or_email);
//Do nothing if no user was found
2023-06-11 14:55:06 +02:00
if (!$user instanceof User) {
return;
}
$unencrypted_token = md5(random_bytes(32));
2022-09-18 23:44:44 +02:00
$user->setPwResetToken($this->passwordEncoder->hash($unencrypted_token));
//Determine the expiration datetime of
2022-08-14 19:32:53 +02:00
$expiration_date = new DateTime();
$expiration_date->add(date_interval_create_from_date_string('1 day'));
$user->setPwResetExpires($expiration_date);
2020-08-21 21:36:22 +02:00
if (!empty($user->getEmail())) {
$address = new Address($user->getEmail(), $user->getFullName());
$mail = new TemplatedEmail();
$mail->to($address);
$mail->subject($this->translator->trans('pw_reset.email.subject'));
2020-01-04 20:24:09 +01:00
$mail->htmlTemplate('mail/pw_reset.html.twig');
$mail->context([
'expiration_date' => $expiration_date,
'token' => $unencrypted_token,
2020-01-04 20:24:09 +01:00
'user' => $user,
]);
//Send email
$this->mailer->send($mail);
}
//Save changes to DB
$this->em->flush();
}
/**
* Sets the new password of the user with the given name, if the token is valid.
2020-01-04 20:24:09 +01:00
*
2020-02-01 19:42:28 +01:00
* @param string $username The name of the user, which password should be reset
2020-01-04 20:24:09 +01:00
* @param string $token The token that should be used to reset the password
* @param string $new_password The new password that should be applied to user
2020-01-04 20:24:09 +01:00
*
* @return bool Returns true, if the new password was applied. False, if either the username is unknown or the
2020-01-04 20:24:09 +01:00
* token is invalid or expired.
*/
2020-02-01 19:42:28 +01:00
public function setNewPassword(string $username, string $token, string $new_password): bool
{
//Try to find the user
$repo = $this->em->getRepository(User::class);
2020-02-01 19:42:28 +01:00
/** @var User|null $user */
$user = $repo->findOneBy(['name' => $username]);
//If no user matching the name, show an error message
2023-06-11 14:55:06 +02:00
if (!$user instanceof User) {
return false;
}
//Check if token is expired yet
2022-08-14 19:32:53 +02:00
if ($user->getPwResetExpires() < new DateTime()) {
return false;
}
//Check if token is valid
2022-09-18 23:44:44 +02:00
if (!$this->passwordEncoder->verify($user->getPwResetToken(), $token)) {
return false;
}
//When everything was valid, apply the new password
2021-10-02 20:41:14 +02:00
$user->setPassword($this->userPasswordEncoder->hashPassword($user, $new_password));
//Remove token
$user->setPwResetToken(null);
2022-08-14 19:32:53 +02:00
$user->setPwResetExpires(new DateTime());
//Save to DB
$this->em->flush();
2020-01-04 20:24:09 +01:00
return true;
}
2020-01-04 20:24:09 +01:00
}