<?php
namespace App\Controller\FrontendBundle;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use App\BackendBundle\Helper\SiteTitleHelper;
use App\BackendBundle\Helper\ValidationHelper;
class SecurityController extends AbstractController {
private $siteTitleHelper;
private $validationHelper;
private $authenticationUtils;
private $tokenStorage;
public function __construct(SiteTitleHelper $sitetitlehelper, ValidationHelper $validationhelper, AuthenticationUtils $authutils,
TokenStorageInterface $tokenstorage) {
$this->siteTitleHelper = $sitetitlehelper;
$this->validationHelper = $validationhelper;
$this->authenticationUtils = $authutils;
$this->tokenStorage = $tokenstorage;
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/login", name="security_login", defaults={"title": "Login", "description": "Loggen Sie sich auf berufsreise.at ein und entdecken Sie die Tiroler Berufsorientierungswelt!"})
*/
public function loginAction(Request $request) {
$this->siteTitleHelper->setTitleDescription($request);
/* @var $authenticationUtils AuthenticationUtils */
//$authenticationUtils = $this->get('security.authentication_utils');
// get the login error if there is one
$error = $this->authenticationUtils->getLastAuthenticationError();
$user = $this->getUser();
if (!empty($user)) {
return $this->redirectToRoute('frontpage_public');
}
// last username entered by the user
$lastUsername = $this->authenticationUtils->getLastUsername();
$validationData = $this->validationHelper->getFormValidationData('login');
return $this->render('@frontend/login/login.html.twig', array(
'validationData' => $validationData,
'last_username' => $lastUsername,
'error' => $error,
));
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/login_redirect", name="security_login_redirect")
*/
public function loginRedirectAction(Request $request, LoggerInterface $logger) {
$logger->info("Handle login redirect action...");
$securityChecker = $this->get('security.authorization_checker');
if ($securityChecker->isGranted('ROLE_COMPANY')) {
return $this->redirectToRoute('company_default_private');
}
if ($securityChecker->isGranted('ROLE_SECONDARY_SCHOOL')) {
return $this->redirectToRoute('school_default_private');
}
if ($securityChecker->isGranted('ROLE_BOPARTNER')) {
return $this->redirectToRoute('bopartner_default_private');
}
if ($securityChecker->isGranted('ROLE_TEACHER')) {
return $this->redirectToRoute('teacher_default_private');
}
if ($securityChecker->isGranted('ROLE_DIRECTOR')) {
return $this->redirectToRoute('director_default_private');
}
if ($securityChecker->isGranted('ROLE_SCHOOL_CLASS')) {
return $this->redirectToRoute('school_class_default_private');
}
return $this->redirectToRoute('frontpage_public');
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/logout", name="security_logout")
*/
public function logoutAction(Request $request) {
//$this->get('security.token_storage')->setToken(null);
$this->tokenStorage->setToken(null);
$request->getSession()->invalidate();
return $this->redirect($this->generateUrl('frontpage_public'));
}
/**
* @return \Symfony\Component\HttpFoundation\Response
* @Route("/login_check", name="security_check")
*/
public function checkAction(Request $request, LoggerInterface $logger) {
$logger->info('Login check action');
}
}