vendor/sonata-project/user-bundle/src/Controller/AdminSecurityController.php line 74

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Sonata\UserBundle\Controller;
  12. use Sonata\UserBundle\Model\UserInterface;
  13. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  18. use Symfony\Component\Security\Core\Security;
  19. class AdminSecurityController extends Controller
  20. {
  21.     /**
  22.      * @param Request $request
  23.      *
  24.      * @return Response|RedirectResponse
  25.      */
  26.     public function loginAction(Request $request)
  27.     {
  28.         if ($this->getUser() instanceof UserInterface) {
  29.             $this->addFlash('sonata_user_error''sonata_user_already_authenticated');
  30.             $url $this->generateUrl('sonata_admin_dashboard');
  31.             return $this->redirect($url);
  32.         }
  33.         $session $request->getSession();
  34.         $authErrorKey Security::AUTHENTICATION_ERROR;
  35.         // get the error if any (works with forward and redirect -- see below)
  36.         if ($request->attributes->has($authErrorKey)) {
  37.             $error $request->attributes->get($authErrorKey);
  38.         } elseif (null !== $session && $session->has($authErrorKey)) {
  39.             $error $session->get($authErrorKey);
  40.             $session->remove($authErrorKey);
  41.         } else {
  42.             $error null;
  43.         }
  44.         if (!$error instanceof AuthenticationException) {
  45.             $error null// The value does not come from the security component.
  46.         }
  47.         if ($this->isGranted('ROLE_ADMIN')) {
  48.             $refererUri $request->server->get('HTTP_REFERER');
  49.             return $this->redirect($refererUri && $refererUri != $request->getUri() ? $refererUri $this->generateUrl('sonata_admin_dashboard'));
  50.         }
  51.         $csrfToken $this->has('security.csrf.token_manager')
  52.             ? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
  53.             : null;
  54.         return $this->render('@SonataUser/Admin/Security/login.html.twig', [
  55.             'admin_pool' => $this->get('sonata.admin.pool'),
  56.             'base_template' => $this->get('sonata.admin.pool')->getTemplate('layout'),
  57.             'csrf_token' => $csrfToken,
  58.             'error' => $error,
  59.             'last_username' => (null === $session) ? '' $session->get(Security::LAST_USERNAME),
  60.             'reset_route' => $this->generateUrl('sonata_user_admin_resetting_request'),
  61.         ]);
  62.     }
  63.     public function checkAction(): void
  64.     {
  65.         throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  66.     }
  67.     public function logoutAction(): void
  68.     {
  69.         throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  70.     }
  71. }