Guest User

Untitled

a guest
Aug 17th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * @Route("/admin/users/edit/{id}", requirements={"id" = "d+"}, name="admin_users_edit")
  5. * @Template("@Core/admin/users_edit.html.twig")
  6. * @Security("has_role('ROLE_ADMIN')")
  7. */
  8. public function EditUserAction($id, Request $request, UserPasswordEncoderInterface $passwordEncoder)
  9. {
  10. $user = $this->getDoctrine()->getRepository('CoreBundle:User')->findOneBy([ 'id'=>$id, 'deleted' => 0 ]);
  11. if ( $user )
  12. {
  13. $old_password = $user->getPassword();
  14. $form = $this->createForm(UserType::class, $user);
  15. $form->handleRequest($request);
  16. if ($form->isSubmitted() && $form->isValid())
  17. {
  18. // If admin changed the user password
  19. if ( $user->getPlainPassword() )
  20. {
  21. $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
  22. $user->setPassword($password);
  23. }
  24. // If admin didn't change the user password, we persist the old one
  25. else
  26. {
  27. $user->setPassword($old_password);
  28. }
  29. $entityManager = $this->getDoctrine()->getManager();
  30. $entityManager->persist($user);
  31. $entityManager->flush();
  32. }
  33. return array('form' => $form->createView());
  34. }
  35. return $this->redirectToRoute('admin_users');
  36. }
Add Comment
Please, Sign In to add comment