<?php 
 
namespace App\Controller; 
 
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\Routing\Annotation\Route; 
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; 
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; 
use App\Repository\UserRepository; 
use App\Entity\User; 
 
class SecurityController extends AbstractController 
{ 
    /** 
     * @Route("/login", name="app_login") 
     */ 
    public function login(AuthenticationUtils $authenticationUtils): Response 
    { 
         if ($this->getUser()) { 
             return $this->redirectToRoute('accueil_back'); 
         } 
 
        // get the login error if there is one 
        $error = $authenticationUtils->getLastAuthenticationError(); 
        // last username entered by the user 
        $lastUsername = $authenticationUtils->getLastUsername(); 
 
        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); 
    } 
 
    /** 
     * @Route("/install", name="user_install", methods={"GET"}) 
     */ 
    public function install(UserRepository $userRepository, UserPasswordEncoderInterface $encoder): Response 
    { 
 
        if(count($userRepository->findAll()) === 0){ 
 
            $user = new User(); 
 
            $user->setFirstName('Admin'); 
            $user->setLastName('Admin'); 
            $user->setEmail('contact@systeo.biz'); 
            $user->setRoles(['ROLE_SUPER_ADMIN']); 
            $user->setRole('ROLE_SUPER_ADMIN'); 
            $user->setActive(true); 
            $user->setPassword($encoder->encodePassword($user, '123456')); 
 
            $em = $this->getDoctrine()->getManager(); 
            $em->persist($user); 
            $em->flush(); 
 
 
            return $this->render('security/install.html.twig', [ 
                'result' => "yes", 
                'user' => $user 
            ]); 
 
        } 
 
        return $this->redirectToRoute('app_login'); 
    } 
    /** 
     * @Route("/logout", name="app_logout") 
     */ 
    public function logout() 
    { 
        throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); 
    } 
}