src/Controller/User/SecurityController.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Controller\User;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use FOS\UserBundle\Controller\SecurityController as BaseController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Security\Core\Security;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use App\Service\User\ExpertUserManager;
  10. class SecurityController extends BaseController
  11. {
  12.     private $tokenManager;
  13.     public function __construct(CsrfTokenManagerInterface $tokenManager null)
  14.     {
  15.         parent::__construct($tokenManager);
  16.         $this->tokenManager $tokenManager;
  17.     }
  18.     /**
  19.      * @Route("/load-me", name="app_load_me", methods={"GET"} ,  options={"expose"=true})
  20.      */
  21.     public function loadme(ExpertUserManager $manager)
  22.     {
  23.         $user $manager->getConnectedUser();
  24.         dump($user);
  25.         $data $this->get('serializer')->serialize($user'json');
  26.         $response = new \Symfony\Component\HttpFoundation\Response($data);
  27.         $response->headers->set('Content-Type''application/json');
  28.         return $response;
  29.     }
  30.     /**
  31.      * @Route("/reset-password", name="app_reset_password")
  32.      */
  33.     public function resetAction()
  34.     {
  35.         return $this->render('agropastoral/login/reset-password.html.twig');
  36.     }
  37.     /**
  38.      * @Route("/change-password", name="app_change_password")
  39.      */
  40.     public function changeAction()
  41.     {
  42.         return $this->render('agropastoral/login/change-password.html.twig', [
  43.                     'firstconnexion' => false
  44.         ]);
  45.     }
  46.     /**
  47.      *
  48.      * @param Request $request
  49.      * @return RedirectResponse
  50.      * @Route("/login", name="app_login")
  51.      */
  52.     public function loginAction(Request $request)
  53.     {
  54.         $authChecker $this->get('security.authorization_checker');
  55.         $router $this->get('router');
  56.         if ($authChecker->isGranted('ROLE_ADMIN') or $authChecker->isGranted('ROLE_SUPERT_ADMIN') OR $authChecker->isGranted('ROLE_COMMERCIAL')) {
  57.             return new RedirectResponse($router->generate('web_home'), 307);
  58.         }
  59. //        if ($authChecker->isGranted('ROLE_USER')) {
  60. //            return new RedirectResponse($router->generate('agro_expert_home'), 307);
  61. //        }
  62.         /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  63.         $session $request->getSession();
  64.         $authErrorKey Security::AUTHENTICATION_ERROR;
  65.         $lastUsernameKey Security::LAST_USERNAME;
  66.         // get the 4error if any (works with forward and redirect -- see below)
  67.         if ($request->attributes->has($authErrorKey)) {
  68.             $error $request->attributes->get($authErrorKey);
  69.         } elseif (null !== $session && $session->has($authErrorKey)) {
  70.             $error $session->get($authErrorKey);
  71.             $session->remove($authErrorKey);
  72.         } else {
  73.             $error null;
  74.         }
  75.         if (!$error instanceof AuthenticationException) {
  76.             $error null// The value does not come from the security component.
  77.         }
  78.         if ($error) {
  79.             // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
  80.             $error $error->getMessage();
  81.         }
  82.         // last username entered by the user
  83.         $lastUsername = (null === $session) ? '' $session->get($lastUsernameKey);
  84.         $csrfToken $this->tokenManager $this->tokenManager->getToken('authenticate')->getValue() : null;
  85.         return $this->renderLogin(array(
  86.                     'last_username' => $lastUsername,
  87.                     'error' => $error,
  88.                     'csrf_token' => $csrfToken,
  89.                     'request' => $request
  90.         ));
  91.     }
  92.     protected function renderLogin(array $data)
  93.     {
  94.         $request $data['request'];
  95.         unset($data['request']);
  96.         $data['rq'] = $request;
  97.         $template sprintf('web/login/index.html.twig');
  98.         return $this->container->get('templating')->renderResponse($template$data);
  99.     }
  100.     /**
  101.      * @Route("/login_check")
  102.      * @throws \RuntimeException
  103.      */
  104.     public function checkAction()
  105.     {
  106.         throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  107.     }
  108.     /**
  109.      * @Route("/logout",name="app_logout",options={"expose"=true})
  110.      * @throws \RuntimeException
  111.      */
  112.     public function logoutAction()
  113.     {
  114.         throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  115.     }
  116. }