src/Controller/SecurityController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Form\ChangePasswordFormType;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Endroid\QrCode\Builder\Builder;
  7. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  18. class SecurityController extends AbstractController
  19. {
  20.     /**
  21.      * @Route("/login", name="app_login")
  22.      */
  23.     public function index(AuthenticationUtils $authenticationUtilsUserInterface $user null): Response
  24.     {
  25.         if ($user) {
  26.             return $this->redirect('/');
  27.         }
  28.         $error $authenticationUtils->getLastAuthenticationError();
  29.         $lastusername $authenticationUtils->getLastUsername();
  30.         return $this->render('security/login.html.twig', [
  31.             'last_username' => $lastusername,
  32.             'error' => $error
  33.         ]);
  34.     }
  35.     /**
  36.      * @Route("/logout", name="app_logout")
  37.      * @return void
  38.      */
  39.     public function logout()
  40.     {
  41.     }
  42. //    /**
  43. //     * @return Response
  44. //     * @Route("/api/login", name="api_login")
  45. //     */
  46. //    public function apiLogin(UserInterface $user = null): Response
  47. //    {
  48. //        if (null === $user) {
  49. //            return $this->json([
  50. //                'message' => 'Missing credentials'
  51. //            ], Response::HTTP_UNAUTHORIZED);
  52. //        }
  53. //
  54. //        $token = uuid_create();
  55. //
  56. //        return $this->json([
  57. //            'user' => $user->getUserIdentifier(),
  58. //            'token' => $token
  59. //        ]);
  60. //    }
  61.     /**
  62.      * @Route("/changepassword", name="app_change_password")
  63.      * @return Response
  64.      */
  65.     public function changePassword(Request $requestUserPasswordHasherInterface $passwordHasherEntityManagerInterface $entityManager)
  66.     {
  67.         /** @var User $user */
  68.         $user $this->getUser();
  69. //        dd($user);
  70.         $form $this->createForm(ChangePasswordFormType::class);
  71.         $form->handleRequest($request);
  72.         if ($request->isMethod('POST') && $form->isValid()) {
  73.             $data $form->getData();
  74.             // TODO ezt át lehetne rakni az entityManagerbe
  75.             $user->setPassword($passwordHasher->hashPassword($user$data['plainPassword']));
  76.             $entityManager->flush();
  77.             $this->addFlash('success''Sikeres jelszó változtatás!');
  78.             return $this->redirectToRoute('admin');
  79.         }
  80.         return $this->render('security/changepassword.html.twig', [
  81.             'form' => $form->createView()
  82.         ]);
  83.     }
  84.     /**
  85.      * @Route("/showqrcode", name="app_2fa_showcode")
  86.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  87.      */
  88.     public function showQrCode(Request $requestTotpAuthenticatorInterface $totpAuthenticatorEntityManagerInterface $entityManagerTokenStorageInterface $tokenStorageSessionInterface $session)
  89.     {
  90.         // this can't be happen...
  91.         $user $this->getUser();
  92.         if (!$user->isTotpAuthenticationEnabled()) {
  93.             $user->setTotpSecret($totpAuthenticator->generateSecret());
  94.             $entityManager->flush();
  95.         }
  96.         if ($request->isMethod('POST')) {
  97.             if ($session->has('newTotp')) {
  98.                 $session->remove('newTotp');
  99.             }
  100.             $tokenStorage->setToken(null);
  101.             return $this->redirect('/');
  102.         }
  103.         return $this->render('security/show2fa.html.twig');
  104.     }
  105.     /**
  106.      * @param TotpAuthenticatorInterface $totpAuthenticator
  107.      * @return Response
  108.      * @Route("/showqrcode_png", name="app_2fa_png")
  109.      * @IsGranted("IS_AUTHENTICATED_FULLY")
  110.      */
  111.     public function authenticatorQrCOde(TotpAuthenticatorInterface $totpAuthenticator)
  112.     {
  113.         $qrCodeContent $totpAuthenticator->getQRContent($this->getUser());
  114.         $result Builder::create()
  115.             ->data($qrCodeContent)
  116.             ->build();
  117.         return new Response($result->getString(), 200, ['Content-Type' => 'image/png']);
  118.     }
  119. }