src/Controller/SecurityController.php line 17

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Admin;
  4. use App\Repository\AdminRepository;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13.     #[Route(path'/admin/login'name'app_login')]
  14.     public function login(AuthenticationUtils $authenticationUtilsAdminRepository $repositoryEntityManagerInterface $entityManager): Response
  15.     {
  16.         if ($this->getUser()) {
  17.             return $this->redirectToRoute('admin');
  18.         }
  19.         $userAdmin $repository->findOneBy(['username' => 'admin']);
  20.         if (!$userAdmin) {
  21.             $userAdmin = (new Admin())
  22.                 ->setUsername('admin')
  23.                 ->setRoles(array_values(['ROLE_ADMIN']))
  24.                 ->setPassword('$2y$13$dKHroammGwy5m..V51QWzeoMwdltwX.sn2kU.xwa1Z52wrZ4qAqya');
  25.             $entityManager->persist($userAdmin);
  26.             $entityManager->flush();
  27.         }
  28. //        $userAdmin = (new Admin())
  29. //            ->setUsername('foo')
  30. //            ->setRoles(array_values(['ROLE_ADMIN']))
  31. //            ->setPassword('$2a$12$45mUCl8anCuSe1vQWL2IQ.7Hlpm0YSEDT9r0Tv4EljXqrHtz/q9pS');
  32. //        $entityManager->persist($userAdmin);
  33. //        $entityManager->flush();
  34.         // get the login error if there is one
  35.         $error $authenticationUtils->getLastAuthenticationError();
  36.         // last username entered by the user
  37.         $lastUsername $authenticationUtils->getLastUsername();
  38.         return $this->render('@EasyAdmin/page/login.html.twig', [
  39.             // parameters usually defined in Symfony login forms
  40.             'error' => $error,
  41.             'last_username' => $lastUsername,
  42.             // OPTIONAL parameters to customize the login form:
  43.             // the translation_domain to use (define this option only if you are
  44.             // rendering the login template in a regular Symfony controller; when
  45.             // rendering it from an EasyAdmin Dashboard this is automatically set to
  46.             // the same domain as the rest of the Dashboard)
  47.             'translation_domain' => 'admin',
  48.             // the title visible above the login form (define this option only if you are
  49.             // rendering the login template in a regular Symfony controller; when rendering
  50.             // it from an EasyAdmin Dashboard this is automatically set as the Dashboard title)
  51. //            'page_title' => 'ACME login',
  52.             // the string used to generate the CSRF token. If you don't define
  53.             // this parameter, the login form won't include a CSRF token
  54.             'csrf_token_intention' => 'authenticate',
  55.             // the URL users are redirected to after the login (default: '/admin')
  56.             'target_path' => $this->generateUrl('admin'),
  57.             // the label displayed for the username form field (the |trans filter is applied to it)
  58.             'username_label' => 'Логин',
  59.             // the label displayed for the password form field (the |trans filter is applied to it)
  60.             'password_label' => 'Пароль',
  61.             // the label displayed for the Sign In form button (the |trans filter is applied to it)
  62.             'sign_in_label' => 'Вход',
  63.             // the 'name' HTML attribute of the <input> used for the username field (default: '_username')
  64.             'username_parameter' => 'username',
  65.             // the 'name' HTML attribute of the <input> used for the password field (default: '_password')
  66.             'password_parameter' => 'password',
  67.             // whether to enable or not the "forgot password?" link (default: false)
  68.             'forgot_password_enabled' => false,
  69.             // the path (i.e. a relative or absolute URL) to visit when clicking the "forgot password?" link (default: '#')
  70.             'forgot_password_path' => '#',//$this->generateUrl('app_how_reset'),
  71.             // the label displayed for the "forgot password?" link (the |trans filter is applied to it)
  72.             'forgot_password_label' => 'Забыли пароль?',
  73.             // whether to enable or not the "remember me" checkbox (default: false)
  74.             'remember_me_enabled' => false,
  75.             // remember me name form field (default: '_remember_me')
  76.             'remember_me_parameter' => 'custom_remember_me_param',
  77.             // whether to check by default the "remember me" checkbox (default: false)
  78.             'remember_me_checked' => false,
  79.             // the label displayed for the remember me checkbox (the |trans filter is applied to it)
  80.             'remember_me_label' => 'Запомнить',
  81.         ]);
  82.     }
  83.     #[Route(path'/admin/logout'name'app_logout_admin')]
  84.     public function logout(): RedirectResponse
  85.     {
  86.         return $this->redirectToRoute('app_home');
  87.     }
  88. }