8000 added change password · eXtreme/symfony-demo@9f22a02 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9f22a02

Browse files
committed
added change password
1 parent f8d63f8 commit 9f22a02

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends 'base.html.twig' %}
2+
3+
{% block main %}
4+
{{ form_start(form, { method: 'POST', action: path('profile_change_password') }) }}
5+
{{ form_widget(form) }}
6+
7+
<div class="form-group">
8+
<button class="btn btn-primary pull-right" type="submit">{{ 'action.save'|trans }}</button>
9+
</div>
10+
{{ form_end(form) }}
11+
12+
{% endblock main %}

app/config/security.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
security:
2+
role_hierarchy:
3+
ROLE_ADMIN: ['ROLE_USER']
4+
25
encoders:
36
# Our user class and the algorithm we'll use to encode passwords
47
# http://symfony.com/doc/current/book/security.html#encoding-the-user-s-password
@@ -43,3 +46,4 @@ security:
4346
# this is a catch-all for the admin area
4447
# additional security lives in the controllers
4548
- { path: ^/admin, roles: ROLE_ADMIN }
49+
- { path: ^/profile, roles: ROLE_USER }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace AppBundle\Controller;
4+
5+
use AppBundle\Entity\User;
6+
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
8+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9+
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
10+
use Symfony\Component\HttpFoundation\Request;
11+
12+
/**
13+
* @Route("/profile")
14+
* @Security("has_role('ROLE_USER')")
15+
*/
16+
class ProfileController extends Controller
17+
{
18+
/**
19+
* @Route("/change-password", name="profile_change_password")
20+
* @Method({"GET", "POST"})
21+
*/
22+
public function changePasswordAction(Request $request)
23+
{
24+
/** @var User $user */
25+
$user = $this->getUser();
26+
27+
$form = $this->createForm('AppBundle\Form\UserPasswordType');
28+
29+
$form->handleRequest($request);
30+
31+
if ($form->isSubmitted() && $form->isValid()) {
32+
$encoder = $this->get('security.password_encoder');
33+
$encodedPassword = $encoder->encodePassword($user, $form->get('new_password')->getData());
34+
35+
$user->setPassword($encodedPassword);
36+
37+
$entityManager = $this->getDoctrine()->getManager();
38+
$entityManager->persist($user);
39+
$entityManager->flush();
40+
41+
$this->addFlash('success', 'profile.password_changed');
42+
43+
return $this->redirectToRoute('profile_change_password');
44+
}
45+
46+
return $this->render('profile/change_password.html.twig', array(
47+
'form' => $form->createView(),
48+
));
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace AppBundle\Form;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class UserPasswordType extends AbstractType
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
public function buildForm(FormBuilderInterface $builder, array $options)
16+
{
17+
$builder
18+
->add('new_password', PasswordType::class, array(
19+
'label' => 'label.new_password',
20+
))
21+
;
22+
}
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function configureOptions(OptionsResolver $resolver)
28+
{
29+
}
30+
}

0 commit comments

Comments
 (0)
0