<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\Type\RegistrationType;
use Doctrine\ORM\EntityManagerInterface;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use FOS\UserBundle\Form\Factory\FactoryInterface;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManagerInterface;
use FOS\UserBundle\Controller\ProfileController as BaseController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Tellaw\SunshineAdminBundle\Service\EntityService;
/**
* Controller managing the user profile.
*/
class ProfileController extends BaseController
{
/**
* @var FactoryInterface
*/
private $formFactory;
/**
* @var UserManagerInterface
*/
private $userManager;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* ProfileController constructor.
*/
public function __construct(EventDispatcherInterface $eventDispatcher, FactoryInterface $formFactory, UserManagerInterface $userManager)
{
parent::__construct($eventDispatcher, $formFactory, $userManager);
$this->formFactory = $formFactory;
$this->userManager = $userManager;
$this->eventDispatcher = $eventDispatcher;
}
/**
* Show the user.
* @param User $user
* @return Response
* @throws \Exception
*/
public function viewAction(User $user, EntityService $entityService, EntityManagerInterface $em)
{
if (!$user) {
$user = $this->getUser();
}
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
return $this->render('@FOSUser/Profile/show.html.twig', array(
'user' => $user,
'fields' => $entityService->getFormConfiguration('User'),
'userArticles' => $em->getRepository('App:Article')->getArticlesByAuthor($user),
'userFiches' => $em->getRepository('App:PracticalFile')->getFichesByAuthor($user)
));
}
/**
* Edit the user.
*
* @param Request $request
*
* @param User $user
* @return Response
*/
public function editUserAction(Request $request, User $user)
{
if (!$user) {
$user = $this->getUser();
}
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form =$this->createForm(RegistrationType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);
$this->userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('sunshine_page', ['pageId' => 'userGrid']);
$response = new RedirectResponse($url);
}
$this->eventDispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
'user' => $user
));
}
/**
* Mise à jour du mot de passe
*
* @param Request $request
* @return null|RedirectResponse|Response
* @throws AccessDeniedException
*/
public function changePasswordAction(Request $request, User $user)
{
$this->denyAccessUnlessGranted('ICO_EDIT_USER', $user);
$event = new GetResponseUserEvent($user, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_INITIALIZE, $event);
if (null !== $event->getResponse()) {
return $event->getResponse();
}
$form = $this->formFactory->createForm();
$form->remove('current_password');
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$event = new FormEvent($form, $request);
$this->eventDispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_SUCCESS, $event);
$this->userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_profile_edit', ['user' => $user->getId()]);
$response = new RedirectResponse($url);
}
$this->eventDispatcher->dispatch(FOSUserEvents::CHANGE_PASSWORD_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('@FOSUser/ChangePassword/change_password.html.twig', array(
'form' => $form->createView(),
'user' => $user
));
}
}