Files
Snips/src/Controller/UserController.php
2023-12-20 22:51:29 +01:00

61 lines
2.1 KiB
PHP

<?php
namespace App\Controller;
use App\Form\ProfileType;
use App\Form\UserSettingsType;
use App\Service\LastRelease;
use Doctrine\ORM\EntityManagerInterface;
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\Attribute\Route;
#[Route('/user', name: 'user')]
class UserController extends AbstractController
{
public function __construct(
private EntityManagerInterface $em,
)
{
}
#[Route('/profile', name: '_profile')]
public function profile(
Request $request,
UserPasswordHasherInterface $userPasswordHasher,
LastRelease $lastRelease,
): Response
{
$user = $this->getUser();
$form = $this->createForm(ProfileType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if (!empty($form->get('plainPassword')->getData())) {
if ($form->get('plainPassword')->getData() !== $form->get('plainPasswordRepeated')->getData()) {
$this->addFlash('error', 'Password and password repeated must be the same, password not changed');
} else {
$this->addFlash('success', 'Password updated successfully');
$user
->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('plainPassword')->getData()
)
);
}
}
$this->addFlash('success', 'Profile updated successfully');
$this->em->persist($user);
$this->em->flush();
}
return $this->render('user/profile.html.twig', [
'form' => $form->createView(),
'release' => $lastRelease,
]);
}
}