<?php
declare(strict_types=1);
namespace App\Controller\Front\Mypage;
use App\Entity\User;
use App\Form\Type\Front\Mypage\AccountPasswordType;
use App\Form\Type\Front\Mypage\AccountType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
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;
class AccountController extends AbstractController
{
/**
* @IsGranted("ROLE_USER")
* @Route("/mypage/account", name="mypage_account")
*/
public function index(): Response
{
$user = $this->getUser();
assert($user instanceof User);
return $this->render('front/mypage/account/index.html.twig', [
'user' => $user,
]);
}
/**
* @IsGranted("ROLE_USER")
* @Route("/mypage/account/edit", name="mypage_account_edit")
*/
public function edit(Request $request): Response
{
$user = $this->getUser();
assert($user instanceof User);
$form = $this->createForm(AccountType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'メールアドレスを変更しました');
return $this->redirectToRoute('mypage_account');
}
return $this->render('front/mypage/account/edit.html.twig', [
'form' => $form->createView(),
'user' => $user,
]);
}
/**
* @IsGranted("ROLE_USER")
* @Route("/mypage/account/password", name="mypage_account_password")
*/
public function password(Request $request, UserPasswordHasherInterface $passwordHasher): Response
{
$user = $this->getUser();
assert($user instanceof User);
$form = $this->createForm(AccountPasswordType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// パスワード変更処理
$password = $passwordHasher->hashPassword(
$user,
$user->getPlainPassword()
);
$user->setPassword($password);
$user->eraseCredentials();
$this->getDoctrine()->getManager()->flush();
$this->addFlash('success', 'パスワードを変更しました');
return $this->redirectToRoute('mypage_account');
}
return $this->render('front/mypage/account/password.html.twig', [
'form' => $form->createView(),
'user' => $user,
]);
}
}
//$2y$13$qVtf.I0o1QBEUb0CnV53Der6TR3fG7rzcYVNxibRXc6GfFwIvEoxu