-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add ability to edit user's informations and password #834
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Controller; | ||
|
||
use App\Form\Type\ChangePasswordType; | ||
use App\Form\UserType; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | ||
|
||
/** | ||
* Controller used to manage current user. | ||
* | ||
* @Route("/profile") | ||
* @Security("has_role('ROLE_USER')") | ||
* | ||
* @author Romain Monteil <[email protected]> | ||
*/ | ||
class UserController extends AbstractController | ||
{ | ||
/** | ||
* @Route("/edit", methods={"GET", "POST"}, name="user_edit") | ||
*/ | ||
public function edit(Request $request): Response | ||
{ | ||
$user = $this->getUser(); | ||
|
||
$form = $this->createForm(UserType::class, $user); | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$this->getDoctrine()->getManager()->flush(); | ||
|
||
$this->addFlash('success', 'user.updated_successfully'); | ||
|
||
return $this->redirectToRoute('user_edit'); | ||
} | ||
|
||
return $this->render('user/edit.html.twig', [ | ||
'user' => $user, | ||
'form' => $form->createView(), | ||
]); | ||
} | ||
|
||
/** | ||
* @Route("/change-password", methods={"GET", "POST"}, name="user_change_password") | ||
*/ | ||
public function changePassword(Request $request, UserPasswordEncoderInterface $encoder): Response | ||
{ | ||
$user = $this->getUser(); | ||
|
||
$form = $this->createForm(ChangePasswordType::class); | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$user->setPassword($encoder->encodePassword($user, $form->get('newPassword')->getData())); | ||
|
||
$this->getDoctrine()->getManager()->flush(); | ||
|
||
return $this->redirectToRoute('security_logout'); | ||
} | ||
|
||
return $this->render('user/change_password.html.twig', [ | ||
'form' => $form->createView(), | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Form\Type; | ||
|
||
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\FormBuilderInterface; | ||
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder; | ||
use Symfony\Component\Security\Core\Validator\Constraints\UserPassword; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\NotBlank; | ||
|
||
/** | ||
* Defines the custom form field type used to change user's password. | ||
* | ||
* @author Romain Monteil <[email protected]> | ||
*/ | ||
class ChangePasswordType extends AbstractType | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
$builder | ||
->add('currentPassword', PasswordType::class, [ | ||
'constraints' => [ | ||
new UserPassword(), | ||
], | ||
'label' => 'label.current_password', | ||
'attr' => [ | ||
'autocomplete' => 'off', | ||
], | ||
]) | ||
->add('newPassword', RepeatedType::class, [ | ||
'type' => PasswordType::class, | ||
'constraints' => [ | ||
new NotBlank(), | ||
new Length([ | ||
'min' => 5, | ||
'max' => BCryptPasswordEncoder::MAX_PASSWORD_LENGTH, | ||
]), | ||
], | ||
'first_options' => [ | ||
'label' => 'label.new_password', | ||
], | ||
'second_options' => [ | ||
'label' => 'label.new_password_confirm', | ||
], | ||
]) | ||
; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace App\Form; | ||
|
||
use App\Entity\User; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\EmailType; | ||
use Symfony\Component\Form\Extension\Core\Type\TextType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* Defines the form used to edit an user. | ||
* | ||
* @author Romain Monteil <[email protected]> | ||
*/ | ||
class UserType extends AbstractType | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options): void | ||
{ | ||
// For the full reference of options defined by each form field type | ||
// see https://symfony.com/doc/current/reference/forms/types.html | ||
|
||
// By default, form fields include the 'required' attribute, which enables | ||
// the client-side form validation. This means that you can't test the | ||
// server-side validation errors from the browser. To temporarily disable | ||
// this validation, set the 'required' attribute to 'false': | ||
// $builder->add('title', null, ['required' => false, ...]); | ||
|
||
$builder | ||
->add('username', TextType::class, [ | ||
'label' => 'label.username', | ||
'disabled' => true, | ||
]) | ||
->add('fullName', TextType::class, [ | ||
'label' => 'label.fullname', | ||
]) | ||
->add('email', EmailType::class, [ | ||
'label' => 'label.email', | ||
]) | ||
; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver): void | ||
{ | ||
$resolver->setDefaults([ | ||
'data_class' => User::class, | ||
]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block body_id 'user_password' %} | ||
|
||
{% block main %} | ||
<h1>{{ 'title.change_password'|trans }}</h1> | ||
|
||
<div class="alert alert-info" role="alert">{{ 'info.change_password'|trans }}</div> | ||
|
||
{{ form_start(form) }} | ||
{{ form_widget(form) }} | ||
|
||
<button type="submit" class="btn btn-primary"> | ||
<i class="fa fa-save" aria-hidden="true"></i> {{ 'action.save'|trans }} | ||
</button> | ||
{{ form_end(form) }} | ||
{% endblock %} | ||
|
||
{% block sidebar %} | ||
<div class="section"> | ||
<a href="{{ path('user_edit') }}" class="btn btn-lg btn-block btn-danger"> | ||
<i class="fa fa-edit" aria-hidden="true"></i> {{ 'action.edit_user'|trans }} | ||
</a> | ||
</div> | ||
|
||
{{ parent() }} | ||
|
||
{{ show_source_code(_self) }} | ||
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{% extends 'base.html.twig' %} | ||
|
||
{% block body_id 'user_edit' %} | ||
|
||
{% block main %} | ||
<h1>{{ 'title.edit_user'|trans }}</h1> | ||
|
||
{{ form_start(form) }} | ||
{{ form_widget(form) }} | ||
|
||
<button type="submit" class="btn btn-primary"> | ||
<i class="fa fa-save" aria-hidden="true"></i> {{ 'action.save'|trans }} | ||
</button> | ||
{{ form_end(form) }} | ||
{% endblock %} | ||
|
||
{% block sidebar %} | ||
<div class="section"> | ||
<a href="{{ path('user_change_password') }}" class="btn btn-lg btn-block btn-danger"> | ||
<i class="fa fa-lock" aria-hidden="true"></i> {{ 'action.change_password'|trans }} | ||
</a> | ||
</div> | ||
|
||
{{ parent() }} | ||
|
||
{{ show_source_code(_self) }} | ||
{% endblock %} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the intention is not to modify the username, how about removing this field and print
{{ user.username }}
directly in the template?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is a demo application, maybe we can keep the disabled option to show people how to disable an input in a form ?