ProfileController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Sonata\UserBundle\Controller;
  11. use Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use FOS\UserBundle\Model\UserInterface;
  16. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  17. /**
  18. * This class is inspirated from the FOS Profile Controller, except :
  19. * - only twig is supported
  20. * - separation of the user authentication form with the profile form
  21. *
  22. */
  23. class ProfileController extends Controller
  24. {
  25. /**
  26. * @return Response
  27. *
  28. * @throws AccessDeniedException
  29. */
  30. public function showAction()
  31. {
  32. $user = $this->container->get('security.context')->getToken()->getUser();
  33. if (!is_object($user) || !$user instanceof UserInterface) {
  34. throw new AccessDeniedException('This user does not have access to this section.');
  35. }
  36. return $this->render('SonataUserBundle:Profile:show.html.twig', array(
  37. 'user' => $user
  38. ));
  39. }
  40. /**
  41. * @return Response
  42. *
  43. * @throws AccessDeniedException
  44. */
  45. public function editAuthenticationAction()
  46. {
  47. $user = $this->container->get('security.context')->getToken()->getUser();
  48. if (!is_object($user) || !$user instanceof UserInterface) {
  49. throw new AccessDeniedException('This user does not have access to this section.');
  50. }
  51. $form = $this->container->get('sonata_user_authentication_form');
  52. $formHandler = $this->container->get('sonata_user_authentication_form_handler');
  53. $process = $formHandler->process($user);
  54. if ($process) {
  55. $this->setFlash('fos_user_success', 'profile.flash.updated');
  56. return new RedirectResponse($this->generateUrl('sonata_user_profile_show'));
  57. }
  58. return $this->render('SonataUserBundle:Profile:edit_authentication.html.twig', array(
  59. 'form' => $form->createView(),
  60. 'theme' => $this->container->getParameter('fos_user.template.theme')
  61. ));
  62. }
  63. /**
  64. * @return Response
  65. *
  66. * @throws AccessDeniedException
  67. */
  68. public function editProfileAction()
  69. {
  70. $user = $this->container->get('security.context')->getToken()->getUser();
  71. if (!is_object($user) || !$user instanceof UserInterface) {
  72. throw new AccessDeniedException('This user does not have access to this section.');
  73. }
  74. $form = $this->container->get('sonata.user.profile.form');
  75. $formHandler = $this->container->get('sonata.user.profile.form.handler');
  76. $process = $formHandler->process($user);
  77. if ($process) {
  78. $this->setFlash('fos_user_success', 'profile.flash.updated');
  79. return new RedirectResponse($this->generateUrl('sonata_user_profile_show'));
  80. }
  81. return $this->render('SonataUserBundle:Profile:edit_profile.html.twig', array(
  82. 'form' => $form->createView(),
  83. 'theme' => $this->container->getParameter('fos_user.template.theme')
  84. ));
  85. }
  86. /**
  87. * @param string $action
  88. * @param string $value
  89. */
  90. protected function setFlash($action, $value)
  91. {
  92. $this->container->get('session')->setFlash($action, $value);
  93. }
  94. }