ProfileController.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 Symfony\Component\DependencyInjection\ContainerAware;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  14. use FOS\UserBundle\Model\UserInterface;
  15. /**
  16. * Controller managing the user profile
  17. *
  18. * @author Christophe Coevoet <stof@notk.org>
  19. */
  20. class ProfileController extends ContainerAware
  21. {
  22. /**
  23. * Show the user
  24. */
  25. public function showAction()
  26. {
  27. $user = $this->container->get('security.context')->getToken()->getUser();
  28. if (!is_object($user) || !$user instanceof UserInterface) {
  29. throw new AccessDeniedException('This user does not have access to this section.');
  30. }
  31. return $this->container->get('templating')->renderResponse('FOSUserBundle:Profile:show.html.'.$this->container->getParameter('fos_user.template.engine'), array('user' => $user));
  32. }
  33. /**
  34. * Edit the user
  35. */
  36. public function editAction()
  37. {
  38. $user = $this->container->get('security.context')->getToken()->getUser();
  39. if (!is_object($user) || !$user instanceof UserInterface) {
  40. throw new AccessDeniedException('This user does not have access to this section.');
  41. }
  42. $form = $this->container->get('fos_user.profile.form');
  43. $formHandler = $this->container->get('fos_user.profile.form.handler');
  44. $process = $formHandler->process($user);
  45. if ($process) {
  46. $this->setFlash('fos_user_success', 'profile.flash.updated');
  47. return new RedirectResponse($this->getRedirectionUrl($user));
  48. }
  49. return $this->container->get('templating')->renderResponse(
  50. 'FOSUserBundle:Profile:edit.html.'.$this->container->getParameter('fos_user.template.engine'),
  51. array('form' => $form->createView())
  52. );
  53. }
  54. /**
  55. * Generate the redirection url when editing is completed.
  56. *
  57. * @param \FOS\UserBundle\Model\UserInterface $user
  58. *
  59. * @return string
  60. */
  61. protected function getRedirectionUrl(UserInterface $user)
  62. {
  63. return $this->container->get('router')->generate('fos_user_profile_show');
  64. }
  65. /**
  66. * @param string $action
  67. * @param string $value
  68. */
  69. protected function setFlash($action, $value)
  70. {
  71. $this->container->get('session')->getFlashBag()->set($action, $value);
  72. }
  73. }