src/Controller/FrontendBundle/SecurityController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller\FrontendBundle;
  3. use Psr\Log\LoggerInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  8. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Session\Session;
  11. use App\BackendBundle\Helper\SecurityHelper;
  12. use App\BackendBundle\Helper\SiteTitleHelper;
  13. use App\BackendBundle\Helper\ValidationHelper;
  14. class SecurityController extends AbstractController {
  15.     private SiteTitleHelper $siteTitleHelper;
  16.     private AuthenticationUtils $authenticationUtils;
  17.     private SecurityHelper $securityHelper;
  18.     private TokenStorageInterface $tokenStorage;
  19.     private ValidationHelper $validationHelper;
  20.     public function __construct(SiteTitleHelper $sitetitlehelperValidationHelper $validationhelperAuthenticationUtils $authutils,
  21.             SecurityHelper $securityHelperTokenStorageInterface $tokenstorage) {
  22.         $this->siteTitleHelper $sitetitlehelper;
  23.         $this->validationHelper $validationhelper;
  24.         $this->authenticationUtils $authutils;
  25.         $this->securityHelper $securityHelper;
  26.         $this->tokenStorage $tokenstorage;
  27.     }
  28.     /**
  29.      * @return \Symfony\Component\HttpFoundation\Response
  30.      * @Route("/login", name="security_login", defaults={"title": "Login", "description": "Loggen Sie sich auf berufsreise.at ein und entdecken Sie die Tiroler Berufsorientierungswelt!"}) 
  31.      */
  32.     public function loginAction(Request $request) {
  33.         $this->siteTitleHelper->setTitleDescription($request);
  34.         /* @var $authException AuthenticationException */
  35.         $authException $this->authenticationUtils->getLastAuthenticationError();
  36.         $error $this->securityHelper->getLoginErrorText($authException);
  37.         $user $this->getUser();
  38.         if (!empty($user)) {
  39.             return $this->redirectToRoute('frontpage_public');
  40.         }
  41.         // last username entered by the user
  42.         $lastUsername $this->authenticationUtils->getLastUsername();
  43.         $validationData $this->validationHelper->getFormValidationData('login');
  44.         return $this->render('@frontend/login/login.html.twig', array(
  45.                     'validationData' => $validationData,
  46.                     'last_username' => $lastUsername,
  47.                     'error' => $error,
  48.         ));
  49.     }
  50.     /**
  51.      * @return \Symfony\Component\HttpFoundation\Response
  52.      * @Route("/login_redirect", name="security_login_redirect") 
  53.      */
  54.     public function loginRedirectAction(Request $requestLoggerInterface $logger) {
  55.         $logger->info("Handle login redirect action...");
  56.         $securityChecker $this->get('security.authorization_checker');
  57.         if ($securityChecker->isGranted('ROLE_COMPANY')) {
  58.             return $this->redirectToRoute('company_default_private');
  59.         }
  60.         if ($securityChecker->isGranted('ROLE_SECONDARY_SCHOOL')) {
  61.             return $this->redirectToRoute('school_default_private');
  62.         }
  63.         if ($securityChecker->isGranted('ROLE_BOPARTNER')) {
  64.             return $this->redirectToRoute('bopartner_default_private');
  65.         }
  66.         if ($securityChecker->isGranted('ROLE_TEACHER')) {
  67.             return $this->redirectToRoute('teacher_default_private');
  68.         }
  69.         if ($securityChecker->isGranted('ROLE_DIRECTOR')) {
  70.             return $this->redirectToRoute('director_default_private');
  71.         }
  72.         if ($securityChecker->isGranted('ROLE_SCHOOL_CLASS')) {
  73.             return $this->redirectToRoute('school_class_default_private');
  74.         }
  75.         return $this->redirectToRoute('frontpage_public');
  76.     }
  77.     /**
  78.      * @return \Symfony\Component\HttpFoundation\Response
  79.      * @Route("/logout", name="security_logout") 
  80.      */
  81.     public function logoutAction(Request $request) {
  82.         //$this->get('security.token_storage')->setToken(null);
  83.         $this->tokenStorage->setToken(null);
  84.         $request->getSession()->invalidate();
  85.         return $this->redirect($this->generateUrl('frontpage_public'));
  86.     }
  87.     /**
  88.      * @return \Symfony\Component\HttpFoundation\Response
  89.      * @Route("/login_check", name="security_check") 
  90.      */
  91.     public function checkAction(Request $requestLoggerInterface $logger) {
  92.         $logger->info('Login check action');
  93.     }
  94. }