فهرست منبع

Force redirect the user to the admin dashboard connect through the admin login area

Thomas Rabaix 13 سال پیش
والد
کامیت
fdf01f5421
2فایلهای تغییر یافته به همراه10 افزوده شده و 1 حذف شده
  1. 7 0
      Controller/AdminSecurityController.php
  2. 3 1
      Resources/doc/reference/installation.rst

+ 7 - 0
Controller/AdminSecurityController.php

@@ -16,6 +16,7 @@ use FOS\UserBundle\Controller\SecurityController;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\Security\Core\SecurityContext;
 use Symfony\Component\Security\Core\SecurityContext;
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
 use Symfony\Component\Security\Core\Exception\AuthenticationException;
+use Symfony\Component\HttpFoundation\RedirectResponse;
 
 
 class AdminSecurityController extends SecurityController
 class AdminSecurityController extends SecurityController
 {
 {
@@ -43,6 +44,12 @@ class AdminSecurityController extends SecurityController
         // last username entered by the user
         // last username entered by the user
         $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
         $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);
 
 
+        if ($this->container->get('security.context')->isGranted('ROLE_ADMIN')) {
+            $refererUri = $request->server->get('HTTP_REFERER');
+
+            return new RedirectResponse($refererUri && $refererUri != $request->getUri() ? $refererUri : $this->container->get('router')->generate('sonata_admin_dashboard'));
+        }
+
         return $this->container->get('templating')->renderResponse('SonataUserBundle:Admin:Security/login.html.'.$this->container->getParameter('fos_user.template.engine'), array(
         return $this->container->get('templating')->renderResponse('SonataUserBundle:Admin:Security/login.html.'.$this->container->getParameter('fos_user.template.engine'), array(
             'last_username' => $lastUsername,
             'last_username' => $lastUsername,
             'error'         => $error,
             'error'         => $error,

+ 3 - 1
Resources/doc/reference/installation.rst

@@ -147,7 +147,6 @@ Then add a new custom firewall handlers for the admin
                 id: fos_user.user_manager
                 id: fos_user.user_manager
 
 
         firewalls:
         firewalls:
-
             # -> custom firewall for the admin area of the URL
             # -> custom firewall for the admin area of the URL
             admin:
             admin:
                 pattern:      /admin(.*)
                 pattern:      /admin(.*)
@@ -157,8 +156,11 @@ Then add a new custom firewall handlers for the admin
                     use_forward:    false
                     use_forward:    false
                     check_path:     /admin/login_check
                     check_path:     /admin/login_check
                     failure_path:   null
                     failure_path:   null
+                    use_referer:    true
                 logout:
                 logout:
                     path:           /admin/logout
                     path:           /admin/logout
+                    target:         /admin/login
+
                 anonymous:    true
                 anonymous:    true
             # -> end custom configuration
             # -> end custom configuration