src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Usuario;
  4. use App\Security\LoginAuthenticator;
  5. use App\Service\Microsoft;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  13. use Symfony\Component\Security\Http\Authentication\UserAuthenticatorInterface;
  14. class SecurityController extends AbstractController
  15. {
  16.     #[Route('/login'name'app_login')]
  17.     public function login(AuthenticationUtils $authenticationUtils): Response
  18.     {
  19.         // if ($this->getUser()) {
  20.         //     return $this->redirectToRoute('target_path');
  21.         // }
  22.         $tenant "conalepmexedu.onmicrosoft.com";
  23.         $client_id "211ac272-40bc-4a7d-b0f9-222d60baf6c0";
  24.         $response_type "token+id_token";
  25.         $redirect_uri "https%3A%2F%2Fti.conalepmex.edu.mx%2Fsigmec%2FprocessLogin";
  26.         $response_mode "form_post";
  27.         $scope "user.read+openid+profile+email";
  28.         $state "12345";
  29.         $nonce "678910";
  30.         $link "https://login.microsoftonline.com/$tenant/oauth2/v2.0/authorize?client_id=$client_id&response_type=$response_type&redirect_uri=$redirect_uri&response_mode=$response_mode&scope=$scope&state=$state&nonce=$nonce";
  31.         // get the login error if there is one
  32.         $error $authenticationUtils->getLastAuthenticationError();
  33.         // last username entered by the user
  34.         $lastUsername $authenticationUtils->getLastUsername();
  35.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error'link' => $link]);
  36.     }
  37.     #[Route('/logout'name'app_logout')]
  38.     public function logout(): void
  39.     {
  40.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  41.     }
  42.     #[Route('/processLogin'name'app_microsoft'methods: ['POST'])]
  43.     public function loginMicrosoft(Request $requestMicrosoft $msEntityManagerInterface $emUserAuthenticatorInterface $userAuthenticatorLoginAuthenticator $authenticator): Response
  44.     {
  45.         $token $request->request->get('access_token');
  46.         if (empty($token))
  47.             throw new CustomUserMessageAuthenticationException('Username could not be found.');
  48.         $info $ms->OIDCUserInfo($token);
  49.         $user $em->getRepository(Usuario::class)->findOneBy(['correo' => $info['email']]);
  50.         if (!$user) {
  51.             $this->addFlash('danger''Usuario no encontrado. Solicitar acceso al sistema.');
  52.             throw new CustomUserMessageAuthenticationException('Username could not be found.');
  53.         }
  54.         if ($user->getEstatus() == "Pendiente") {
  55.             $this->addFlash('danger''La solicitud de acceso no ha sido evaluada.');
  56.             throw new CustomUserMessageAuthenticationException('Access request pending evaluation.');
  57.         }
  58.         if ($user->getEstatus() == "Rechazado") {
  59.             $this->addFlash('danger''La solicitud de acceso fue rechazada.');
  60.             throw new CustomUserMessageAuthenticationException('Access request has been rejected.');
  61.         }
  62.         if ($user->getEstatus() == "Suspendido") {
  63.             $this->addFlash('danger''Las credenciales de acceso han sido suspendidas.');
  64.             throw new CustomUserMessageAuthenticationException('Access credentials suspended.');
  65.         }
  66.         $userAuthenticator->authenticateUser($user$authenticator$request);
  67.         return $this->redirectToRoute('homepage');
  68.     }
  69. }