Change password feature (maybe...)

This commit is contained in:
Nicolò P. 2024-10-29 18:55:17 +01:00
parent 1d5278eb06
commit 96904693ca
2 changed files with 37 additions and 6 deletions

View File

@ -4,7 +4,10 @@ namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Doctrine\ORM\EntityManagerInterface;
class ProfileController extends AbstractController
{
@ -17,9 +20,30 @@ class ProfileController extends AbstractController
}
#[Route('/changepasswd', name: 'app_change_passwd')]
public function changePassword(): Response
public function changePassword(Request $request, UserPasswordHasherInterface $hasher, EntityManagerInterface $entityManager): Response
{
$this->addFlash('notice', 'Password successfully updated');
$current = $request->getPayload()->get('_current_pass');
$new = $request->getPayload()->get('_new_pass');
$confirm = $request->getPayload()->get('_confirm_pass');
$user = $this->getUser();
if(! $hasher->isPasswordValid($user, $current)) {
$this->addFlash('error', 'The current password is incorrect');
return $this->redirectToRoute('app_profile');
}
if ($new !== $confirm) {
$this->addFlash('error', 'The new password and the confirmation password don\'t match');
return $this->redirectToRoute('app_profile');
}
$hashed = $hasher->hashPassword($user, $new);
$user->setPassword($hashed);
$entityManager->persist($user);
$entityManager->flush();
$this->addFlash('notice', 'Password updated successfully');
return $this->redirectToRoute('app_profile');
}

View File

@ -3,10 +3,11 @@
{% block title %}Profile | ArCOA{% endblock %}
{% block rightpanel %}
<h1 class="is-size-1 mb-2 has-text-centered">User profile</h1>
<div class="container" style="max-width: 50vw">
<h1 class="is-size-1 mt-0 has-text-centered">User profile</h1>
<div class="ml-6 pl-6 container">
<div class="card ml-6">
<div class="container mt-6">
<div class="card">
<div class="card-content">
<div class="media">
<div class="media-left">
@ -42,7 +43,12 @@
</div>
</div>
<div class="card ml-6 content">
<div class="card content">
{% for message in app.flashes('error') %}
<article class="message is-danger">
<div class="message-body">{{ message }}</div>
</article>
{% endfor %}
{% for message in app.flashes('notice') %}
<article class="message is-success">
<div class="message-body">{{ message }}</div>
@ -81,5 +87,6 @@
</form>
</div>
</div>
</div>
{% endblock %}