src/Controller/SecurityController.php line 19

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\SessionHistory;
  4. use App\Repository\SessionHistoryRepository;
  5. use App\Repository\SettingsRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13.     #[Route(path'/login'name'login')]
  14.     public function login(AuthenticationUtils $authenticationUtils,SettingsRepository $settingsRepository): Response
  15.     {
  16.         $error $authenticationUtils->getLastAuthenticationError();
  17.         $lastUsername $authenticationUtils->getLastUsername();
  18.         $settingsMain $settingsRepository->findOneBy(["code"=>"main"]);
  19.         return $this->render('@EasyAdmin/page/login.html.twig', [
  20.             // parameters usually defined in Symfony login forms
  21.             'error' => $error,
  22.             'last_username' => $lastUsername,
  23.             // OPTIONAL parameters to customize the login form:
  24.             // the translation_domain to use (define this option only if you are
  25.             // rendering the login template in a regular Symfony controller; when
  26.             // rendering it from an EasyAdmin Dashboard this is automatically set to
  27.             // the same domain as the rest of the Dashboard)
  28.             'translation_domain' => 'admin',
  29.             // by default EasyAdmin displays a black square as its default favicon;
  30.             // use this method to display a custom favicon: the given path is passed
  31.             // "as is" to the Twig asset() function:
  32.             // <link rel="shortcut icon" href="{{ asset('...') }}">
  33.              'background_body'=>"url('/themes/".strtolower($settingsMain->getAssetFolderName())."/admin/images/bg-body.jpg')",
  34.             'favicon_path' => "/themes/".strtolower($settingsMain->getAssetFolderName())."/admin/images/favicon.png",
  35.             // the title visible above the login form (define this option only if you are
  36.             // rendering the login template in a regular Symfony controller; when rendering
  37.             // it from an EasyAdmin Dashboard this is automatically set as the Dashboard title)
  38.             'page_title' => '<img width="100" class="mb-3 mt-5" src="/themes/'.strtolower($settingsMain->getAssetFolderName()).'/admin/images/logo.png"  />',
  39.             // the string used to generate the CSRF token. If you don't define
  40.             // this parameter, the login form won't include a CSRF token
  41.             'csrf_token_intention' => 'authenticate',
  42.             // the URL users are redirected to after the login (default: '/admin')
  43.             //'target_path' => $this->generateUrl('admin'),
  44.             // the label displayed for the username form field (the |trans filter is applied to it)
  45.             'username_label' => 'Nom d\'utilisateur',
  46.             // the label displayed for the password form field (the |trans filter is applied to it)
  47.             'password_label' => 'Mot de passe',
  48.             // the label displayed for the Sign In form button (the |trans filter is applied to it)
  49.             'sign_in_label' => 'Se connecter',
  50.             // the 'name' HTML attribute of the <input> used for the username field (default: '_username')
  51.             'username_parameter' => 'username',
  52.             // the 'name' HTML attribute of the <input> used for the password field (default: '_password')
  53.             'password_parameter' => 'password',
  54.             // whether to enable or not the "forgot password?" link (default: false)
  55.             'forgot_password_enabled' => false,
  56.             // the path (i.e. a relative or absolute URL) to visit when clicking the "forgot password?" link (default: '#')
  57.             //'forgot_password_path' => $this->generateUrl('...', ['...' => '...']),
  58.             // the label displayed for the "forgot password?" link (the |trans filter is applied to it)
  59.             'forgot_password_label' => 'Forgot your password?',
  60.             // whether to enable or not the "remember me" checkbox (default: false)
  61.             'remember_me_enabled' => true,
  62.             // remember me name form field (default: '_remember_me')
  63.             'remember_me_parameter' => 'remember_me',
  64.             // whether to check by default the "remember me" checkbox (default: false)
  65.             'remember_me_checked' => false,
  66.             // the label displayed for the remember me checkbox (the |trans filter is applied to it)
  67.             'remember_me_label' => 'Se souvenir de moi',
  68.         ]);
  69.     }
  70.     #[Route(path'/logout'name'app_logout')]
  71.     public function logout(): never
  72.     {
  73.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  74.     }
  75.     #[Route(path'/toggle-session'name'toggleSession')]
  76.     public function toggleSession(ManagerRegistry $doctrine,SessionHistoryRepository $sessionHistoryRepository)
  77.     {
  78.         $session $sessionHistoryRepository->findCurrentSession($this->getUser());
  79.         $currentDateTime = new \DateTime();
  80.         
  81.         if($session){
  82.             
  83.             $session->setEndAt($currentDateTime);
  84.             $doctrine->getManager()->persist($session);
  85.             $doctrine->getManager()->flush();
  86.             return $this->redirectToRoute("app_logout");
  87.         }
  88.         
  89.         if($this->getUser()){
  90.             $session = new SessionHistory();
  91.         $session->setStartAt($currentDateTime);
  92.         $session->setUser($this->getUser());
  93.         $doctrine->getManager()->persist($session);
  94.         $doctrine->getManager()->flush();
  95.         return $this->redirectToRoute("admin"); 
  96.         }else{
  97.             return $this->redirectToRoute("app_logout");
  98.         }
  99.                
  100.             
  101.             
  102.         
  103.         
  104.     }
  105. }