src/Controller/Front/Mypage/AccountController.php line 23

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\Front\Mypage;
  4. use App\Entity\User;
  5. use App\Form\Type\Front\Mypage\AccountPasswordType;
  6. use App\Form\Type\Front\Mypage\AccountType;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. class AccountController extends AbstractController
  14. {
  15.     /**
  16.      * @IsGranted("ROLE_USER")
  17.      * @Route("/mypage/account", name="mypage_account")
  18.      */
  19.     public function index(): Response
  20.     {
  21.         $user $this->getUser();
  22.         assert($user instanceof User);
  23.         return $this->render('front/mypage/account/index.html.twig', [
  24.             'user' => $user,
  25.         ]);
  26.     }
  27.     /**
  28.      * @IsGranted("ROLE_USER")
  29.      * @Route("/mypage/account/edit", name="mypage_account_edit")
  30.      */
  31.     public function edit(Request $request): Response
  32.     {
  33.         $user $this->getUser();
  34.         assert($user instanceof User);
  35.         $form $this->createForm(AccountType::class, $user);
  36.         $form->handleRequest($request);
  37.         if ($form->isSubmitted() && $form->isValid()) {
  38.             $this->getDoctrine()->getManager()->flush();
  39.             $this->addFlash('success''メールアドレスを変更しました');
  40.             return $this->redirectToRoute('mypage_account');
  41.         }
  42.         return $this->render('front/mypage/account/edit.html.twig', [
  43.             'form' => $form->createView(),
  44.             'user' => $user,
  45.         ]);
  46.     }
  47.     /**
  48.      * @IsGranted("ROLE_USER")
  49.      * @Route("/mypage/account/password", name="mypage_account_password")
  50.      */
  51.     public function password(Request $requestUserPasswordHasherInterface $passwordHasher): Response
  52.     {
  53.         $user $this->getUser();
  54.         assert($user instanceof User);
  55.         $form $this->createForm(AccountPasswordType::class, $user);
  56.         $form->handleRequest($request);
  57.         if ($form->isSubmitted() && $form->isValid()) {
  58.             // パスワード変更処理
  59.             $password $passwordHasher->hashPassword(
  60.                 $user,
  61.                 $user->getPlainPassword()
  62.             );
  63.             $user->setPassword($password);
  64.             $user->eraseCredentials();
  65.             $this->getDoctrine()->getManager()->flush();
  66.             $this->addFlash('success''パスワードを変更しました');
  67.             return $this->redirectToRoute('mypage_account');
  68.         }
  69.         return $this->render('front/mypage/account/password.html.twig', [
  70.             'form' => $form->createView(),
  71.             'user' => $user,
  72.         ]);
  73.     }
  74. }
  75. //$2y$13$qVtf.I0o1QBEUb0CnV53Der6TR3fG7rzcYVNxibRXc6GfFwIvEoxu