Select Git revision
UserController.php

Camille Simiand authored
UserController.php 4.59 KiB
<?php
namespace App\Controller;
use App\Entity\PendingEmailAddress;
use App\Entity\User;
use App\Form\EditPasswordFormType;
use App\Form\EditUserProfileFormType;
use App\Security\EmailVerifier;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserController extends AbstractController
{
public function __construct(
private EntityManagerInterface $entity_manager,
private TranslatorInterface $translator
) {
}
#[Route('/profile', name:'show_profile')]
public function showProfile(): Response
{
$current_user = $this->getUser();
if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout');
}
return $this->render('user/profile.html.twig', [
'user' => $current_user
]);
}
#[Route('/edit_profile', name:'edit_profile')]
public function editProfile(Request $request, EmailVerifier $email_verifier): Response
{
$current_user = $this->getUser();
if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout');
}
$form = $this->createForm(
EditUserProfileFormType::class,
$current_user,
['current_email_address' => $current_user->getEmail()]
);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->entity_manager->persist($current_user);
$this->entity_manager->flush();
if ($current_user->getEmail() !== $form->get('email')->getData()) {
$pending_email_address = new PendingEmailAddress();
$pending_email_address->setEmail($form->get('email')->getData());
$pending_email_address->setUser($current_user);
$this->entity_manager->persist($pending_email_address);
$this->entity_manager->flush();
$email_verifier->sendEmailConfirmation(
'app_verify_email',
$pending_email_address->getUser()->getId(),
$pending_email_address->getEmail(),
(new TemplatedEmail())
->to($pending_email_address->getEmail())
->subject('Please Confirm your Email')
->htmlTemplate('user/update_email.html.twig')
);
$this->addFlash(
'warning',
$this->translator->trans('user.profile.updated.warning', [
'new_email_address' => $form->get('email')->getData()
])
);
return $this->render('user/edit_email_address.html.twig', [
'new_email_address' => $form->get('email')->getData()
]);
}
$this->addFlash(
'success',
$this->translator->trans('user.profile.updated.success')
);
return $this->redirectToRoute('show_profile');
}
return $this->renderForm('user/edit_profile.html.twig', [
'editUserProfileForm' => $form
]);
}
#[Route('/edit_password', name:'edit_password')]
public function editPassword(
Request $request,
UserPasswordHasherInterface $user_password_hasher,
): Response {
$form = $this->createForm(EditPasswordFormType::class);
$form->handleRequest($request);
$current_user = $this->getUser();
if (! $current_user instanceof User) {
return $this->redirectToRoute('app_logout');
}
if ($form->isSubmitted() && $form->isValid()) {
$encodedPassword = $user_password_hasher->hashPassword(
$current_user,
$form->get('plainPassword')->getData()
);
$current_user->setPassword($encodedPassword);
$this->entity_manager->flush();
$this->addFlash(
'profile_updated_success',
$this->translator->trans('user.password.updated_success')
);
return $this->redirectToRoute('show_profile');
}
return $this->renderForm('user/edit_password.html.twig', [
'editPasswordForm' => $form
]);
}
}