src/Controller/SecurityController.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  7. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  8. use App\Repository\UserRepository;
  9. use App\Entity\User;
  10. class SecurityController extends AbstractController
  11. {
  12.     /**
  13.      * @Route("/login", name="app_login")
  14.      */
  15.     public function login(AuthenticationUtils $authenticationUtils): Response
  16.     {
  17.          if ($this->getUser()) {
  18.              return $this->redirectToRoute('accueil_back');
  19.          }
  20.         // get the login error if there is one
  21.         $error $authenticationUtils->getLastAuthenticationError();
  22.         // last username entered by the user
  23.         $lastUsername $authenticationUtils->getLastUsername();
  24.         return $this->render('security/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  25.     }
  26.     /**
  27.      * @Route("/install", name="user_install", methods={"GET"})
  28.      */
  29.     public function install(UserRepository $userRepositoryUserPasswordEncoderInterface $encoder): Response
  30.     {
  31.         if(count($userRepository->findAll()) === 0){
  32.             $user = new User();
  33.             $user->setFirstName('Admin');
  34.             $user->setLastName('Admin');
  35.             $user->setEmail('contact@systeo.biz');
  36.             $user->setRoles(['ROLE_SUPER_ADMIN']);
  37.             $user->setRole('ROLE_SUPER_ADMIN');
  38.             $user->setActive(true);
  39.             $user->setPassword($encoder->encodePassword($user'123456'));
  40.             $em $this->getDoctrine()->getManager();
  41.             $em->persist($user);
  42.             $em->flush();
  43.             return $this->render('security/install.html.twig', [
  44.                 'result' => "yes",
  45.                 'user' => $user
  46.             ]);
  47.         }
  48.         return $this->redirectToRoute('app_login');
  49.     }
  50.     /**
  51.      * @Route("/logout", name="app_logout")
  52.      */
  53.     public function logout()
  54.     {
  55.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  56.     }
  57. }