<?php
/**
* @license Silk Software House Sp. Z O. O.
*/
declare(strict_types=1);
namespace App\Controller\Security;
use App\Entity\User;
use App\Factory\EmailFactory;
use App\Form\Security\ResetPasswordType;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use DateTime;
use Exception;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Class ResetController.
*/
class ResetController extends AbstractController
{
/**
* Email factory.
*
* @var EmailFactory
*/
private $emailFactory;
/**
* Translator.
*
* @var TranslatorInterface
*/
private $translator;
/**
* @var EntityManagerInterface
*/
private $em;
private UserPasswordHasherInterface $hasher;
/**
* ResetController constructor.
*
* @param EmailFactory $emailFactory
* @param TranslatorInterface $translator
* @param EntityManagerInterface $em
* @param UserPasswordHasherInterface $hasher
*/
public function __construct(
EmailFactory $emailFactory,
TranslatorInterface $translator,
EntityManagerInterface $em,
UserPasswordHasherInterface $hasher
) {
$this->emailFactory = $emailFactory;
$this->translator = $translator;
$this->em = $em;
$this->hasher = $hasher;
}
/**
* Trigger password resetting.
*
* @Route("/profile/trigger-password-reset", name="trigger_password_reset", methods={"GET", "POST"})
*
* @Template("panel/security/reset/trigger.html.twig")
*
* @return array|RedirectResponse
*/
public function trigger()
{
$user = $this->getUser();
if ($user) {
$user->startPasswordResettingProcess();
$this->em->persist($user->setHashValidityEndsAt(new DateTime('+7 days')));
$this->em->flush();
return $this->redirectToRoute('reset_password', ['id' => $user->getId(), 'hash' => $user->getHash()]);
}
return ['error' => $this->translator->trans('To change password please contact administrator')];
}
/**
* Trigger account activation.
*
* @Route("/profile/activate/{id}/{hash}", name="activate_account", methods={"GET", "POST"})
*
* @Template("panel/security/reset/form.html.twig")
*
* @param Request $request
* @param User $user
* @param string $hash
*
* @return array|RedirectResponse
*
* @throws Exception
*/
public function activate(Request $request, User $user, string $hash)
{
if ($user->getActivatedAt()) {
$this->addFlash('success', 'Your account has already been activated. You can sign up here.');
return $this->redirectToRoute('login');
}
return $this->change($request, $user->setActivatedAt(new DateTime()), $hash);
}
/**
* Trigger password resetting.
*
* @Route("/profile/reset-password/{id}/{hash}", name="reset_password", methods={"GET", "POST"})
*
* @Template("panel/security/reset/form.html.twig")
*
* @param Request $request
* @param User $user
* @param string $hash
*
* @return array|RedirectResponse
*/
public function change(Request $request, User $user, string $hash)
{
if (!$user->getIsHashCorrect($hash)) {
$this->addFlash('danger', 'This link is outdated.');
return $this->redirectToRoute('trigger_password_reset');
}
$form = $this->createForm(ResetPasswordType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$this->getDoctrine()->getManager()->getRepository(User::class)->upgradePassword(
$user,
$this->hasher->encodePassword($user, $data['password'])
);
$this->addFlash(
'success',
'Password was changed.'
);
return $this->redirectToRoute('panel_devices');
}
return [
'form' => $form->createView(),
];
}
}