Skip to content
Snippets Groups Projects
Commit 0d52d214 authored by Camille Simiand's avatar Camille Simiand
Browse files

Add an edit password page

parent 10dc1cee
No related branches found
No related tags found
1 merge request!36tuleap-90-access-my-account-preferences
Pipeline #673 passed
......@@ -3,11 +3,13 @@
namespace App\Controller;
use App\Entity\User;
use App\Form\EditPasswordFormType;
use App\Form\EditUserProfileFormType;
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\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
......@@ -65,21 +67,40 @@ class UserController extends AbstractController
]);
}
// /**
// * @Route("/edit_password", name="edit_password")
// */
// public function editPassword(Request $request): Response
// {
// $form = $this->createForm(EditPasswordFormType::class);
// $form->handleRequest($request);
// $current_user = $this->getUser();
//
// if (! $current_user instanceof User) {
// return $this->redirectToRoute('app_logout');
// }
//
// return $this->render('user/edit_password.html.twig', [
// 'editPasswordForm' => $form->createView()
// ]);
// }
/**
* @Route("/edit_password", name="edit_password")
*/
public function editPassword(
Request $request,
UserPasswordHasherInterface $userPasswordHasher,
EntityManagerInterface $entity_manager,
TranslatorInterface $translator
): 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 = $userPasswordHasher->hashPassword(
$current_user,
$form->get('plainPassword')->getData()
);
$current_user->setPassword($encodedPassword);
$entity_manager->flush();
$this->addFlash(
'profile_updated_success',
$translator->trans('user.password.updated_success')
);
return $this->redirectToRoute('show_profile');
}
return $this->render('user/edit_password.html.twig', [
'editPasswordForm' => $form->createView()
]);
}
}
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class EditPasswordFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('current_password', PasswordType::class, [
'mapped' => false,
'required' => true,
'constraints' => [
new NotBlank(['message' => 'password.not_blank']),
new UserPassword(['message' => "password.invalid"])
],
'label' => 'user.password.current'
])
->add('plainPassword', RepeatedType::class, [
'type' => PasswordType::class,
'first_options' => [
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'password.not_blank',
]),
new Length([
'min' => 6,
'minMessage' => 'password.min_characters',
'max' => 4096,
]),
],
'label' => 'reset_password.new_password',
],
'second_options' => [
'attr' => ['autocomplete' => 'new-password'],
'label' => 'reset_password.repeat_password',
],
'invalid_message' => 'password.match',
'mapped' => false,
])
->add(
'change_password',
SubmitType::class,
['label' => 'user.password.change_password']
)
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([]);
}
}
{% extends 'layout.html.twig' %}
{% block title %}
{{ 'user.edit_password'|trans }}
{% endblock %}
{% block body %}
<div class="row gx-0">
<div class="row-title-box">
<h3 class="row-title">
{{ 'user.edit_password'|trans }}
</h3>
</div>
</div>
<div>
{{ form_start(editPasswordForm, {'attr': {novalidate: 'novalidate', 'class': 'd-flex flex-column justify-content-center'}}) }}
{{ form_row(editPasswordForm.current_password, {'row_attr': {'class' : 'form-group m-auto mb-4 col-6'}}) }}
{{ form_row(editPasswordForm.plainPassword.first, {'row_attr': {'class' : 'form-group m-auto mb-4 col-6'}}) }}
{{ form_row(editPasswordForm.plainPassword.second, {'row_attr': {'class' : 'form-group m-auto mb-4 col-6'}}) }}
{{ form_row(editPasswordForm.change_password, {'row_attr': {'class' : 'm-auto mb-4 col-2'}}) }}
{{ form_end(editPasswordForm) }}
</div>
{% endblock %}
\ No newline at end of file
{% extends 'layout.html.twig' %}
{% block title %}
{{ 'user.profile.title'|trans }}
{{ 'user.edit_profile'|trans }}
{% endblock %}
{% block body %}
......
......@@ -24,7 +24,7 @@ registration:
reset_password:
title: Reset your password
new_password: New password
repeat_password: Repeat password
repeat_password: Repeat new password
message: Enter your email address and we will send you a link to reset your password
button: Reset password
......@@ -69,4 +69,7 @@ user:
password:
edit: Edit password
current: Current password
change_password: Change password
updated_success: The password has been updated
edit_profile: Edit my profile
edit_password: Edit my password
\ No newline at end of file
......@@ -23,7 +23,7 @@ registration:
reset_password:
title: Réinitialiser mon mot de passe
new_password: Nouveau mot de passe
repeat_password: Répéter le mot de passe
repeat_password: Répéter le nouveau mot de passe
message: Entrez votre courriel et nous vous enverrons un lien pour réinitialiser votre mot de passe
button: Réinitialiser mon mot de passe
......@@ -68,4 +68,7 @@ user:
password:
edit: Modifier mon mot de passe
current: Mot de passe actuel
edit_profile: Edit my profile
\ No newline at end of file
change_password: Changer mon mot de passe
updated_success: Votre mot de passe a bien été modifié
edit_profile: Modifier mon mot de passe
edit_password: Modifier mon mot de passe
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment