<?php
namespace App\Controller;
use App\Entity\User;
use App\Type\Form\AdminLoginFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class AppController extends AbstractController
{
private EntityManagerInterface $em;
public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
#[Route('/', name: 'admin_login')]
public function login(AuthenticationUtils $utils): Response
{
$user = new User();
$loginForm = $this->createForm(AdminLoginFormType::class, $user);
$error = $utils->getLastAuthenticationError();
$lastUsername = $utils->getLastUsername();
if ($this->isGranted('ROLE_ADMIN')) {
return $this->redirectToRoute('admin-index');
}
return $this->render(
'admin/login.html.twig',
[
'login_form' => $loginForm->createView(),
'error' => $error,
'last_username' => $lastUsername
]
);
}
#[Route('/create-admin', name: 'create_admin')]
public function createAdminUser(UserPasswordHasherInterface $passwordHasher): Response
{
$user = new User();
$hashedPassword = $passwordHasher->hashPassword(
$user,
'moselle57@Gde'
);
$user
->setEmail('emmanuel.grosdemange@thedots.lu')
->setRoles(['ROLE_ADMIN'])
->setPassword($hashedPassword);
$this->em->persist($user);
$this->em->flush($user);
return $this->redirectToRoute('admin-login');
}
#[Route('/logout', name: 'logout')]
public function logout(): void
{
}
#[Route('/exit', name: 'exit')]
public function exit(): Response
{
return $this->redirectToRoute('admin-login');
}
}