/Controller/ProfileController.php

http://github.com/FriendsOfSymfony/FOSUserBundle · PHP · 107 lines · 66 code · 18 blank · 23 comment · 8 complexity · 3aff4285d5f2a0fea7191a0eb52270d0 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the FOSUserBundle package.
  4. *
  5. * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace FOS\UserBundle\Controller;
  11. use FOS\UserBundle\Event\FilterUserResponseEvent;
  12. use FOS\UserBundle\Event\FormEvent;
  13. use FOS\UserBundle\Event\GetResponseUserEvent;
  14. use FOS\UserBundle\Form\Factory\FactoryInterface;
  15. use FOS\UserBundle\FOSUserEvents;
  16. use FOS\UserBundle\Model\UserInterface;
  17. use FOS\UserBundle\Model\UserManagerInterface;
  18. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\HttpFoundation\RedirectResponse;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  24. /**
  25. * Controller managing the user profile.
  26. *
  27. * @author Christophe Coevoet <stof@notk.org>
  28. *
  29. * @final
  30. */
  31. class ProfileController extends Controller
  32. {
  33. private $eventDispatcher;
  34. private $formFactory;
  35. private $userManager;
  36. public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager)
  37. {
  38. $this->eventDispatcher = $eventDispatcher;
  39. $this->formFactory = $formFactory;
  40. $this->userManager = $userManager;
  41. }
  42. /**
  43. * Show the user.
  44. */
  45. public function showAction()
  46. {
  47. $user = $this->getUser();
  48. if (!is_object($user) || !$user instanceof UserInterface) {
  49. throw new AccessDeniedException('This user does not have access to this section.');
  50. }
  51. return $this->render('@FOSUser/Profile/show.html.twig', [
  52. 'user' => $user,
  53. ]);
  54. }
  55. /**
  56. * Edit the user.
  57. *
  58. * @return Response
  59. */
  60. public function editAction(Request $request)
  61. {
  62. $user = $this->getUser();
  63. if (!is_object($user) || !$user instanceof UserInterface) {
  64. throw new AccessDeniedException('This user does not have access to this section.');
  65. }
  66. $event = new GetResponseUserEvent($user, $request);
  67. $this->eventDispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
  68. if (null !== $event->getResponse()) {
  69. return $event->getResponse();
  70. }
  71. $form = $this->formFactory->createForm();
  72. $form->setData($user);
  73. $form->handleRequest($request);
  74. if ($form->isSubmitted() && $form->isValid()) {
  75. $event = new FormEvent($form, $request);
  76. $this->eventDispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
  77. $this->userManager->updateUser($user);
  78. if (null === $response = $event->getResponse()) {
  79. $url = $this->generateUrl('fos_user_profile_show');
  80. $response = new RedirectResponse($url);
  81. }
  82. $this->eventDispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
  83. return $response;
  84. }
  85. return $this->render('@FOSUser/Profile/edit.html.twig', [
  86. 'form' => $form->createView(),
  87. ]);
  88. }
  89. }