Skip to content
Snippets Groups Projects
Select Git revision
  • 58c1c49ab72de9991b20377dc4f4f870dd72ab31
  • main default
  • 35-cgu
  • 34-peertube-support
  • 27-add-autoplay-to-iframe
  • 33-bug-on-youtube-embed-urls
  • RC-Rekall-v1.1-fix_lpo
  • tuleap-140-go-back-to-my-capsules-page-when-i-m-on-capsule-preview-page
  • RC-Rekall-v1.2-fix10
  • RC-Rekall-v1.2-fix9
  • RC-Rekall-v1.2-fix8
  • RC-Rekall-v1.2-fix7
  • RC-Rekall-v1.2-fix6
  • RC-Rekall-v1.2-fix5
  • RC-Rekall-v1.2-fix4
  • RC-Rekall-v1.2-fix3
  • RC-Rekall-v1.2-fix2
  • RC-Rekall-v1.2-fix1
  • RC-Rekall-v1.1-fix-3
  • RC-Rekall-v1.1-fix-2
  • RC-Rekall-v1.1-fix-1
  • RC-Rekall-v1.1-delivered
  • preprod20220209-1535
23 results

UserController.php

Blame
  • 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
            ]);
        }
    }