mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
75 lines
1.9 KiB
PHP
75 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Chill\MainBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Chill\MainBundle\Form\UserPasswordType;
|
|
use Chill\MainBundle\Entity\User;
|
|
|
|
class PasswordController extends Controller
|
|
{
|
|
/**
|
|
*
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function UserPasswordAction(Request $request)
|
|
{
|
|
|
|
// get authentified user
|
|
$user = $this->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'))
|
|
;
|
|
}
|
|
|
|
|
|
}
|