如何根据FOSUserBundle中的角色重定向到不同的路由?

Pal*_*het 2 php symfony fosuserbundle

我是Symfony2的新手,后来是FOSUserBundle.我理解捆绑包是什么以及使用它的内容但是我对如何使用捆绑包与我已经存在的视图和控制器相关联有疑问.

我已经设置了捆绑包并让它工作但我对下一个任务感到困惑.

设置:在FOSUserBundle的登录页面上,我希望将"管理员用户"路由到某个页面,将"普通用户"路由到另一个页面.我在哪里放置这个逻辑?我目前在我的捆绑包的DefaultController中有它但得到页面:localhost isn't working...localhost redirected you too many times...我清除了缓存但仍然是相同的结果.

DefaultController:

namespace Pas\ShopTestBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class DefaultController extends Controller
{
    /**
     * @Route("/")
     * @Template()
     */
    public function indexAction(Request $request) {   

        if ('admin_login' === $request->get('_route')) {
            return $this->redirectToRoute('product'); //just test to product
        } else {
            return $this->redirectToRoute('login'); //just test to login
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

现在,我的ULTIMATE目标是,一旦用户登录,就会在他们发送到的页面上显示他们的用户名.我该如何编码呢?它去哪儿了?

我非常感谢你的帮助,谢谢大家.

Symfony 2.7:FOSUserBundle 2.0

编辑:security.yml

security:

    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:  ROLE_ADMIN
        ROLE_NORMAL: ROLE_NORMAL

    # http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
        in_memory:
            memory: ~

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern: ^/
            form_login:
                login_path: login
                check_path: fos_user_security_check
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
                default_target_path: /

            logout:     true
            anonymous:  true

    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/, role: ROLE_ADMIN }
        # - { path: ^/admin_login, role: ROLE_ADMIN }
Run Code Online (Sandbox Code Playgroud)

小智 5

或者你可以这样做:

将其添加到security.yml中的主防火墙

main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            success_handler: acme_user.login_success_handler


        logout:       true
        anonymous:    true
Run Code Online (Sandbox Code Playgroud)

并在services.xml中创建相应的服务

<services>
    <service id="acme_user.login_success_handler" class="Acme\UserBundle\EventListener\LoginSuccessHandler">
        <argument type="service" id="router" />
        <argument type="service" id="security.context" />
        <tag name="monolog.logger" channel="security"/>
    </service>
</services>
Run Code Online (Sandbox Code Playgroud)

然后为LoginSuccessHandler.php类:

<?php

namespace Acme\UserBundle\EventListener;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{

    protected $router;
    protected $security;

    public function __construct(Router $router, SecurityContext $security)
    {
        $this->router = $router;
        $this->security = $security;
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {

        if ($this->security->isGranted('ROLE_SUPER_ADMIN') || $this->security->isGranted('ROLE_ADMIN')) {
            $response = new RedirectResponse($this->router->generate('admin_route'));
        } else {
            $response = new RedirectResponse($this->router->generate('user_route'));
        }
        return $response;
    }
}
Run Code Online (Sandbox Code Playgroud)