From f7502b4e9e43c155b134ad9ac0551a44d7b13b20 Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 10 Apr 2018 16:19:35 +0200 Subject: [PATCH] add a page for modifying password for user --- Controller/PasswordController.php | 74 +++++++++++++++++++++ Resources/config/routing.yml | 12 ++-- Resources/views/Password/password.html.twig | 34 ++++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 Controller/PasswordController.php create mode 100644 Resources/views/Password/password.html.twig diff --git a/Controller/PasswordController.php b/Controller/PasswordController.php new file mode 100644 index 000000000..ae85b83a1 --- /dev/null +++ b/Controller/PasswordController.php @@ -0,0 +1,74 @@ +getUser(); + + // create a form for password_encoder + $form = $this->passwordForm($user); + + // process the form + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + + + $password = $form->getData()->getPassword(); + + // logging for prod + $this->get('logger')->info('update password for an user', + array('method' => __METHOD__, 'user' => $user->getUsername())); + + $user->setPassword($this->get('security.password_encoder') + ->encodePassword($user, $password)); + + $em = $this->getDoctrine()->getManager(); + $em->flush(); + + $this->addFlash('success', $this->get('translator')->trans('Password successfully updated!')); + + } + + // render into a template + return $this->render('ChillMainBundle:Password:password.html.twig', array( + 'form' => $form->createView() + )); + + } + + /** + * + * + * @param User $user + * @return \Symfony\Component\Form\Form + */ + private function passwordForm(User $user) + { + return $this->createForm(UserPasswordType::class, $user, array( + 'method' => 'PUT', + + )) + ->add('submit', SubmitType::class, array('label' => 'Change password')) + ; + } + + +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index 2b1562379..6321e9876 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -13,7 +13,7 @@ chill_main_admin_scope: chill_main_admin: resource: "@ChillMainBundle/Resources/config/routing/center.yml" prefix: "{_locale}/admin/center" - + chill_main_exports: resource: "@ChillMainBundle/Resources/config/routing/exports.yml" prefix: "{_locale}/exports" @@ -62,11 +62,11 @@ chill_main_admin_permissions: chill_main_search: path: /{_locale}/search defaults: { _controller: ChillMainBundle:Search:search } - + chill_main_advanced_search: path: /{_locale}/search/advanced/{name} defaults: { _controller: ChillMainBundle:Search:advancedSearch } - + chill_main_advanced_search_list: path: /{_locale}/search/advanced defaults: { _controller: ChillMainBundle:Search:advancedSearchList } @@ -85,4 +85,8 @@ logout: user: order: 10 label: Logout - icon: power-off \ No newline at end of file + icon: power-off + +password: + path: /password + defaults: { _controller: ChillMainBundle:Password:userPassword } diff --git a/Resources/views/Password/password.html.twig b/Resources/views/Password/password.html.twig new file mode 100644 index 000000000..d31d43026 --- /dev/null +++ b/Resources/views/Password/password.html.twig @@ -0,0 +1,34 @@ +{# + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + / + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . +#} + + +{% extends "ChillMainBundle::layout.html.twig" %} + + +{% block title %}{{"Change my password"|trans}}{% endblock %} + +{% block content %} + +

{{ 'Choose a new password'|trans }}

+ + {{ form_start(form) }} + {{ form_row(form.password) }} + {{ form_widget(form.submit, { 'attr': { 'class': 'sc-button orange' } } ) }} + {{ form_end(form) }} + +{% endblock %}