src/Controller/SecurityController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Repository\SchoolRepository;
  4. use App\Repository\UserRepository;
  5. use App\Security\SecurityAuthenticator;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  12. use Symfony\Contracts\Translation\TranslatorInterface;
  13. class SecurityController extends AbstractController
  14. {
  15.     #[Route(path'/login'name'app_login')]
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         // if ($this->getUser()) {
  19.         //     return $this->redirectToRoute('target_path');
  20.         // }
  21.         // get the login error if there is one
  22.         $error $authenticationUtils->getLastAuthenticationError();
  23.         // last username entered by the user
  24.         $lastUsername $authenticationUtils->getLastUsername();
  25.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  26.     }
  27.     #[Route(path'/logout'name'app_logout')]
  28.     public function logout(): void
  29.     {
  30.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  31.     }
  32.     #[Route(path'super-admin/switch-to-admin-school/{school}/'name'app_supper-admin_to_admin')]
  33.     public function supperAdminSwitchToAdmin(
  34.         $school,SchoolRepository $schoolRepository,
  35.         UserAuthenticatorInterface $userAuthenticator,
  36.         SecurityAuthenticator $authenticator,
  37.         UserRepository $userRepository,
  38.         TranslatorInterface $translator,
  39.         Request $request,
  40.     ):Response
  41.     {
  42.         $_school $schoolRepository->find($school);
  43.         $admin $userRepository->findOneBy(['school'=>$_school,'userType'=>'admin']);
  44.         if ($admin != null){
  45.             $message $translator->trans('vous etes connecté en tant que admin de :') .$_school->getName();
  46.             $this->addFlash('success',$message);
  47.             return $userAuthenticator->authenticateUser(
  48.                 $admin,
  49.                 $authenticator,
  50.                 $request
  51.             );
  52.         }else{
  53.            $message $translator->trans('échec de connexion');
  54.            $this->addFlash('danger',$message);
  55.         }
  56.         return $this->redirectToRoute('app_school_index');
  57.     }
  58. }