vendor/friendsofsymfony/user-bundle/Controller/ProfileController.php line 49

Open in your IDE?
  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. class ProfileController extends Controller
  30. {
  31.     private $eventDispatcher;
  32.     private $formFactory;
  33.     private $userManager;
  34.     public function __construct(EventDispatcherInterface $eventDispatcherFactoryInterface $formFactoryUserManagerInterface $userManager)
  35.     {
  36.         $this->eventDispatcher $eventDispatcher;
  37.         $this->formFactory $formFactory;
  38.         $this->userManager $userManager;
  39.     }
  40.     /**
  41.      * Show the user.
  42.      */
  43.     public function showAction()
  44.     {
  45.         $user $this->getUser();
  46.         if (!is_object($user) || !$user instanceof UserInterface) {
  47.             throw new AccessDeniedException('This user does not have access to this section.');
  48.         }
  49.         return $this->render('@FOSUser/Profile/show.html.twig', array(
  50.             'user' => $user,
  51.         ));
  52.     }
  53.     /**
  54.      * Edit the user.
  55.      *
  56.      * @param Request $request
  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', array(
  86.             'form' => $form->createView(),
  87.         ));
  88.     }
  89. }