<?php
namespace App\Controller\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use FOS\UserBundle\Controller\SecurityController as BaseController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use App\Service\User\ExpertUserManager;
class SecurityController extends BaseController
{
private $tokenManager;
public function __construct(CsrfTokenManagerInterface $tokenManager = null)
{
parent::__construct($tokenManager);
$this->tokenManager = $tokenManager;
}
/**
* @Route("/load-me", name="app_load_me", methods={"GET"} , options={"expose"=true})
*/
public function loadme(ExpertUserManager $manager)
{
$user = $manager->getConnectedUser();
dump($user);
$data = $this->get('serializer')->serialize($user, 'json');
$response = new \Symfony\Component\HttpFoundation\Response($data);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* @Route("/reset-password", name="app_reset_password")
*/
public function resetAction()
{
return $this->render('agropastoral/login/reset-password.html.twig');
}
/**
* @Route("/change-password", name="app_change_password")
*/
public function changeAction()
{
return $this->render('agropastoral/login/change-password.html.twig', [
'firstconnexion' => false
]);
}
/**
*
* @param Request $request
* @return RedirectResponse
* @Route("/login", name="app_login")
*/
public function loginAction(Request $request)
{
$authChecker = $this->get('security.authorization_checker');
$router = $this->get('router');
if ($authChecker->isGranted('ROLE_ADMIN') or $authChecker->isGranted('ROLE_SUPERT_ADMIN') OR $authChecker->isGranted('ROLE_COMMERCIAL')) {
return new RedirectResponse($router->generate('web_home'), 307);
}
// if ($authChecker->isGranted('ROLE_USER')) {
// return new RedirectResponse($router->generate('agro_expert_home'), 307);
// }
/** @var $session \Symfony\Component\HttpFoundation\Session\Session */
$session = $request->getSession();
$authErrorKey = Security::AUTHENTICATION_ERROR;
$lastUsernameKey = Security::LAST_USERNAME;
// get the 4error if any (works with forward and redirect -- see below)
if ($request->attributes->has($authErrorKey)) {
$error = $request->attributes->get($authErrorKey);
} elseif (null !== $session && $session->has($authErrorKey)) {
$error = $session->get($authErrorKey);
$session->remove($authErrorKey);
} else {
$error = null;
}
if (!$error instanceof AuthenticationException) {
$error = null; // The value does not come from the security component.
}
if ($error) {
// TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
$error = $error->getMessage();
}
// last username entered by the user
$lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
$csrfToken = $this->tokenManager ? $this->tokenManager->getToken('authenticate')->getValue() : null;
return $this->renderLogin(array(
'last_username' => $lastUsername,
'error' => $error,
'csrf_token' => $csrfToken,
'request' => $request
));
}
protected function renderLogin(array $data)
{
$request = $data['request'];
unset($data['request']);
$data['rq'] = $request;
$template = sprintf('web/login/index.html.twig');
return $this->container->get('templating')->renderResponse($template, $data);
}
/**
* @Route("/login_check")
* @throws \RuntimeException
*/
public function checkAction()
{
throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
}
/**
* @Route("/logout",name="app_logout",options={"expose"=true})
* @throws \RuntimeException
*/
public function logoutAction()
{
throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
}
}