<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\ChangePasswordFormType;
use Doctrine\ORM\EntityManagerInterface;
use Endroid\QrCode\Builder\Builder;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function index(AuthenticationUtils $authenticationUtils, UserInterface $user = null): Response
{
if ($user) {
return $this->redirect('/');
}
$error = $authenticationUtils->getLastAuthenticationError();
$lastusername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastusername,
'error' => $error
]);
}
/**
* @Route("/logout", name="app_logout")
* @return void
*/
public function logout()
{
}
// /**
// * @return Response
// * @Route("/api/login", name="api_login")
// */
// public function apiLogin(UserInterface $user = null): Response
// {
// if (null === $user) {
// return $this->json([
// 'message' => 'Missing credentials'
// ], Response::HTTP_UNAUTHORIZED);
// }
//
// $token = uuid_create();
//
// return $this->json([
// 'user' => $user->getUserIdentifier(),
// 'token' => $token
// ]);
// }
/**
* @Route("/changepassword", name="app_change_password")
* @return Response
*/
public function changePassword(Request $request, UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager)
{
/** @var User $user */
$user = $this->getUser();
// dd($user);
$form = $this->createForm(ChangePasswordFormType::class);
$form->handleRequest($request);
if ($request->isMethod('POST') && $form->isValid()) {
$data = $form->getData();
// TODO ezt át lehetne rakni az entityManagerbe
$user->setPassword($passwordHasher->hashPassword($user, $data['plainPassword']));
$entityManager->flush();
$this->addFlash('success', 'Sikeres jelszó változtatás!');
return $this->redirectToRoute('admin');
}
return $this->render('security/changepassword.html.twig', [
'form' => $form->createView()
]);
}
/**
* @Route("/showqrcode", name="app_2fa_showcode")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function showQrCode(Request $request, TotpAuthenticatorInterface $totpAuthenticator, EntityManagerInterface $entityManager, TokenStorageInterface $tokenStorage, SessionInterface $session)
{
// this can't be happen...
$user = $this->getUser();
if (!$user->isTotpAuthenticationEnabled()) {
$user->setTotpSecret($totpAuthenticator->generateSecret());
$entityManager->flush();
}
if ($request->isMethod('POST')) {
if ($session->has('newTotp')) {
$session->remove('newTotp');
}
$tokenStorage->setToken(null);
return $this->redirect('/');
}
return $this->render('security/show2fa.html.twig');
}
/**
* @param TotpAuthenticatorInterface $totpAuthenticator
* @return Response
* @Route("/showqrcode_png", name="app_2fa_png")
* @IsGranted("IS_AUTHENTICATED_FULLY")
*/
public function authenticatorQrCOde(TotpAuthenticatorInterface $totpAuthenticator)
{
$qrCodeContent = $totpAuthenticator->getQRContent($this->getUser());
$result = Builder::create()
->data($qrCodeContent)
->build();
return new Response($result->getString(), 200, ['Content-Type' => 'image/png']);
}
}