src/Fonial/FrontendBundle/Controller/SecurityController.php line 49

Open in your IDE?
  1. <?php
  2. namespace Fonial\FrontendBundle\Controller;
  3. use DateTime;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Fonial\DataBundle\Entity\AccountSetting;
  6. use Fonial\DataBundle\Entity\AccountUserSetting;
  7. use Fonial\DataBundle\Entity\GlobalSetting;
  8. use Fonial\DataBundle\Services\ConfigHelper;
  9. use Fonial\FrontendBundle\Security\LoginFormAuthenticator;
  10. use RuntimeException;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Cookie;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  18. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  19. use User\UserBundle\Entity\User;
  20. /**
  21.  * TODO: make this controller use real di... this maybe needs to get rid of FOS Bundle...
  22.  * @method User getUser()
  23.  */
  24. class SecurityController extends AbstractController
  25. {
  26.     private ConfigHelper $configHelper;
  27.     private SessionInterface $session;
  28.     private EntityManagerInterface $em;
  29.     public function __construct(
  30.         ConfigHelper $configHelper,
  31.         SessionInterface $session,
  32.         EntityManagerInterface $em
  33.     ) {
  34.         $this->configHelper $configHelper;
  35.         $this->session $session;
  36.         $this->em $em;
  37.     }
  38.     /**
  39.      * This method basically just sets the _locale into the session if not available, then calls the original method
  40.      *
  41.      * @param AuthenticationUtils $authenticationUtils
  42.      * @return Response
  43.      */
  44.     public function loginAction(AuthenticationUtils $authenticationUtils): Response
  45.     {
  46.         // REDIRECT TO DASHBOARD IF ALREADY LOGGED IN
  47.         if (!$this->session->get('_locale')) {
  48.             $this->session->set('_locale'$this->configHelper->getConfigParameter('kernel.default_locale'));
  49.         }
  50.         if ($this->getUser() !== null) {
  51.             // REDIRECT TO DASHBOARD IF ALREADY LOGGED IN
  52.             return $this->redirectToRoute('fonial_frontend_dashboard_index');
  53.         }
  54.         /** @var GlobalSetting $loginFooter */
  55.         $loginFooter $this->em->getRepository(GlobalSetting::class)->findOneBy([
  56.              'key' => GlobalSetting::CUSTOM_FOOTER_LOGIN,
  57.          ]);
  58.         $this->session->remove(GlobalSetting::CUSTOM_FOOTER_LOGIN);
  59.         if (null !== $loginFooter) {
  60.             $this->session->set(GlobalSetting::CUSTOM_FOOTER_LOGIN$loginFooter->getValue());
  61.         }
  62.         $error $authenticationUtils->getLastAuthenticationError();
  63.         $lastUsername $authenticationUtils->getLastUsername();
  64.         if (!$error instanceof AuthenticationException) {
  65.             $error null// The value does not come from the security component.
  66.         }
  67.         return $this->renderLogin([
  68.           'last_username' => $lastUsername,
  69.           'error' => $error,
  70.       ]);
  71.     }
  72.     protected function renderLogin(array $data): ?Response
  73.     {
  74.         if ($data['error']) {
  75.             // If we have an error, get more user info to give mor specific info
  76.             /** @var User $user */
  77.             $user $this->em->getRepository(User::class)->findOneBy([
  78.                'username' => $data['last_username']
  79.            ]);
  80.             if ($user) {
  81.                 // Check, if there is a already lockout time or just some failed logins
  82.                 if ($user->getFailedLoginTimeout()) {
  83.                     $data['loginIsTimedOut'] = true;
  84.                     $data['remainingTime'] = $user->getFailedLoginTimeout()->diff(new DateTime())->format('%I:%S');
  85.                 } else {
  86.                     $securitySettings $this->configHelper->getConfigParameter('security');
  87.                     $remainingAttempts = (int)$securitySettings['brute_force_max_logins'] - $user->getFailedLoginCount();
  88.                     $data['passwordIsWrong'] = true;
  89.                     $data['remainingAttempts'] = $remainingAttempts;
  90.                 }
  91.             }
  92.         }
  93.         $data['isBackend'] = $_SERVER['SYMFONY_ENVIRONMENT'] === 'app_backend';
  94.         // TODO trigger warning
  95.         $data['alert'] = array(
  96.             'type' => 'Warning',
  97.             'message' => 'Anbindung an das Telefonnetz derzeit gestört!',
  98.             'info' => 'https://www.fonial.de',
  99.         );
  100.         $signUpServiceConfig $this->configHelper->getConfigParameter('signUpService');
  101.         if (is_array($signUpServiceConfig) && !empty($signUpServiceConfig) && isset($signUpServiceConfig['webservice_url'])) {
  102.             $data['signUpServiceUrl'] = $signUpServiceConfig['webservice_url'];
  103.             if (!str_starts_with($data['signUpServiceUrl'], 'http')) {
  104.                 $data['signUpServiceUrl'] = 'https://' $data['signUpServiceUrl'];
  105.             }
  106.         }
  107.         return $this->render('@FonialFrontend/Security/login.html.twig'$data);
  108.     }
  109.     public function remoteloginAction(
  110.         string $guid,
  111.         string $sessionId,
  112.         LoginFormAuthenticator $loginAuthenticator,
  113.         GuardAuthenticatorHandler $guardHandler,
  114.         Request $request
  115.     ): ?Response
  116.     {
  117.         /** @var ?User $user */
  118.         $user $this->em->getRepository(User::class)->findOneBy(['guid' => $guid]);
  119.         /** @var ?AccountSetting $sessionSetting */
  120.         $sessionSetting $this->em->getRepository(AccountUserSetting::class)->findOneBy([
  121.              'user' => $user,
  122.              'data0' => $sessionId
  123.          ]);
  124.         if (!($sessionSetting instanceof AccountUserSetting)) {
  125.             return $this->redirectToRoute('fonial_frontend_login');
  126.         }
  127.         $this->em->remove($sessionSetting);
  128.         $this->em->flush();
  129.         //Now lets log them in
  130.         $response $guardHandler->authenticateUserAndHandleSuccess(
  131.             user$user,
  132.             request$request,
  133.             authenticator$loginAuthenticator,
  134.             providerKey'main'
  135.         );
  136.         /** @var GlobalSetting $customSidebar */
  137.         $customSidebar $this->em->getRepository(GlobalSetting::class)->findOneBy([
  138.             'key' => GlobalSetting::CUSTOM_SIDEBAR,
  139.         ]);
  140.         $this->session->remove(GlobalSetting::CUSTOM_SIDEBAR);
  141.         if (null !== $customSidebar) {
  142.             $this->session->set(GlobalSetting::CUSTOM_SIDEBAR$customSidebar->getValue());
  143.         }
  144.         $response?->headers->setCookie(new Cookie('adminLogin'1));
  145.         return $response;
  146.     }
  147.     public function logoutAction()
  148.     {
  149.         throw new RuntimeException('You must activate the logout in your security firewall configuration.');
  150.     }
  151. }