src/Controller/SecurityController.php line 38

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use App\Security\User;
  12. use App\Util\AccountTools;
  13. use App\Util\Api;
  14. use App\Util\Reporter;
  15. use App\Util\TwoFactor;
  16. use App\Util\AppLogger;
  17. class SecurityController extends AbstractController
  18. {
  19.     private $translator;
  20.     private $logger;
  21.     private $params;
  22.     private $session;
  23.     
  24.     public function __construct(TranslatorInterface $translatorParameterBagInterface $paramsRequestStack $requestStackAppLogger $logger)
  25.     {
  26.         $this->translator $translator;
  27.         $this->params $params;
  28.         $this->session $requestStack->getSession();
  29.         $this->logger $logger;
  30.     }
  31.     
  32.     /**
  33.      * @Route("/login/{forceLoginTarget}", name="app_login", defaults={"forceLoginTarget"=""}, requirements={"forceLoginTarget"="|panel|webmail"})
  34.      */
  35.     public function login(AuthenticationUtils $authenticationUtilsRequest $request$forceLoginTarget): Response
  36.     {
  37.         // if set by URL - get it from there
  38.         if($request->get('_locale')) {
  39.             $locale $request->get('_locale');
  40.         }
  41.         else {
  42.             // if we have locale stored in cookie get it from there
  43.             $locale $request->cookies->get(User::LOCALE_COOKIE_NAME);
  44.         }
  45.         
  46.         if($locale == '') {
  47.             // no URL or cookie set locale, use default
  48.             // $locale = $this->translator->getLocale();
  49.             if(isset($_SERVER['APP_DEFAULT_LANGUAGE'])) {
  50.                 $locale $_SERVER['APP_DEFAULT_LANGUAGE'];
  51.             }
  52.             else {
  53.                 $locale 'pl';
  54.             }            
  55.         }
  56.         
  57.         $this->translator->setLocale($locale);
  58.         
  59.         // maintenance mode?
  60.         
  61.         $maintenanceMode false;
  62.         
  63.         if(isset($_SERVER['MAINTENANCE_MODE'])) {
  64.             if($_SERVER['MAINTENANCE_MODE'] == 'full') {
  65.                 return $this->render('security/maintenance.html.twig');
  66.             }
  67.             
  68.             if(($_SERVER['MAINTENANCE_MODE'] == 'panel') || ($_SERVER['MAINTENANCE_MODE'] == 'webmail')) {
  69.                 $forceLoginTarget $_SERVER['MAINTENANCE_MODE'];
  70.                 $maintenanceMode true;
  71.             }
  72.         }
  73.                 
  74.         // get the login error if there is one
  75.         $error $authenticationUtils->getLastAuthenticationError();
  76.         // last username entered by the user
  77.         $lastUsername $authenticationUtils->getLastUsername();
  78.         
  79.         $apiHealth Api::checkSystemHealth();
  80.         $reporter = new Reporter($this->params);
  81.         $licenseStatus = -1;
  82.         
  83.         if($apiHealth == 1) {
  84.             $licenseStatus Api::checkSystemHealth('license');
  85.             
  86.             if(Reporter::shouldSendReport()) {
  87.                 if($reporter->sendPanelInfo()) {
  88.                     $reporter->setAsSent();
  89.                 }
  90.                 else {
  91.                     $reporter->setAsTriedNotSent();
  92.                 }
  93.             }
  94.         }
  95.         
  96.         $showForgotPasswordUrl '';
  97.         
  98.         // should we show the link to "forgot password" path? 
  99.         if(isset($_SERVER['APP_SHOW_FORGOT_PASS'])) {
  100.             $showForgotPassword true;
  101.             $showForgotPasswordUrl $_SERVER['APP_SHOW_FORGOT_PASS']; // URL or keyword 'internal'
  102.         }
  103.         else {
  104.             $showForgotPassword false;
  105.         }
  106.         
  107.         $showLoginLanguage true;
  108.         
  109.         if(isset($_SERVER['APP_LOGIN_LANGUAGE_SHOW']) && ($_SERVER['APP_LOGIN_LANGUAGE_SHOW'] == 'false')) {
  110.             $showLoginLanguage false;
  111.         }
  112.         
  113.         $recaptchaAllowed false;
  114.         $recaptchaSiteKey '';
  115.         
  116.         if(isset($_SERVER['APP_RECAPTCHA_ALLOWED'])) {
  117.             if($_SERVER['APP_RECAPTCHA_ALLOWED'] == 'true') {
  118.                 $recaptchaAllowed true;
  119.                 if(isset($_SERVER['APP_RECAPTCHA_SITE_KEY'])) {
  120.                     $recaptchaSiteKey $_SERVER['APP_RECAPTCHA_SITE_KEY'];
  121.                 }
  122.             }
  123.         }
  124.         
  125.         // determine template name based on APP_CLIENT_NAME
  126.         $loginTemplate 'security/login.html.twig';
  127.         if(isset($_SERVER['APP_CLIENT_NAME']) && $_SERVER['APP_CLIENT_NAME'] != '') {
  128.             $loginTemplate 'security/login.' $_SERVER['APP_CLIENT_NAME'] . '.html.twig';
  129.         }
  130.         
  131.         return $this->render($loginTemplate, [
  132.             'last_username' => $lastUsername,
  133.             'error' => $error,
  134.             'language' => $locale,
  135.             'apiHealth' => $apiHealth,
  136.             'licenseStatus' => $licenseStatus,
  137.             'show_forgot_password' => $showForgotPassword,
  138.             'show_forgot_password_url' => $showForgotPasswordUrl,
  139.             'show_login_language' => $showLoginLanguage,
  140.             'force_login_target' => $forceLoginTarget,
  141.             'show_login_target' => isset($_SERVER['APP_LOGIN_TARGET']) ? false true,
  142.             'default_signin_program' => isset($_SERVER['APP_DEFAULT_SIGNIN_PROGRAM']) ? $_SERVER['APP_DEFAULT_SIGNIN_PROGRAM'] : 'panel',
  143.             'maintenance_mode' => $maintenanceMode,
  144.             'recaptcha_allowed' => $recaptchaAllowed,
  145.             'recaptcha_site_key' => $recaptchaSiteKey,
  146.         ]);
  147.     }
  148.     
  149.     /**
  150.      * @Route("/logout", name="app_logout")
  151.      */
  152.     public function logout()
  153.     {
  154.         // it will never be executed. Needed only for route
  155.     }
  156. }