登录后symfony fosuserbundle重定向 [英] symfony fosuserbundle redirection after login

查看:71
本文介绍了登录后symfony fosuserbundle重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个登录表单,如果用户具有角色用户尝试登录,他要重定向至页面voiture_new,如果管理员最终具有角色admin,则将其重定向至管理页面 PS:我正在使用easyadminbundle

i have a login form what i want to do that if a user have role user tries to login he's redirected to page voiture_new and if an admin eventually has a role admin he's redirected to the admin page PS : i'm using easyadminbundle

这是我添加到控制器的loginaction中的内容

here's what i've added to the loginaction of my controller

          $authChecker = $this->container-   >get('security.authorization_checker');
$router = $this->container->get('router');

if ($authChecker->isGranted('ROLE_ADMIN')) {
    return new RedirectResponse($router->generate('admin'), 307);
} 

if ($authChecker->isGranted('ROLE_USER')) {
    return new RedirectResponse($router->generate('voiture_new'), 307);
}

这是我的security.yml

and here's my security.yml

    security:
encoders:
    FOS\UserBundle\Model\UserInterface: bcrypt

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email

firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            csrf_token_generator: security.csrf.token_manager
            always_use_default_target_path: false
            default_target_path:  /voiture/new
            check_path: fos_user_security_check

            # if you are using Symfony < 2.8, use the following config instead:
            # csrf_provider: form.csrf_provider

        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: ^/marque/, role: ROLE_ADMIN }
    - { path: ^/modele/, role: ROLE_ADMIN }
    - { path: ^/user/, role: ROLE_ADMIN }
    - { path: ^/voiture/, role: ROLE_USER }
    - { path: ^/profile/, role: ROLE_USER }
    - { path: ^/interventions/, role: ROLE_USER }

但是我总是被重定向到voiture_new,即使用户缺少我的管理员角色?

but always i'mredirected to voiture_new even if the user have a role admin waht i'm missing ?

推荐答案

您需要做的是创建

What you need to do is to create Authenticator Class and then tell symfony that use this while trying to authenticate. Inside this class is a method onAuthenticationSuccess you can then use this to perform all the redirect.

例如,在防火墙下的security.yml内部,在这种情况下称为 main .告诉它您要使用防护,然后提及该示例中称为 app.form_login_authenticator

For example inside security.yml under firewall which is called main in this case. Tell it that you want to use guard and then mention the service which in this example is called app.form_login_authenticator

main:
    pattern: ^/
    http_basic: ~
    anonymous: ~
    logout:
        path: logout
    guard:
        authenticators:
            - app.form_login_authenticator
        # by default, use the start() function from FormLoginAuthenticator
        entry_point: app.form_login_authenticator

在您的services.yml内部,确保已列出此服务

Inside your services.yml make sure this service is listed

app.form_login_authenticator:
        class: AppBundle\Security\FormLoginAuthenticator
        arguments: ["@service_container"]

然后这是课程示例

class FormLoginAuthenticator extends AbstractFormLoginAuthenticator
{
    private $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;
    }

    public function getCredentials(Request $request)
    {
        if ($request->getPathInfo() != '/login_check') {
            return;
        }

        $username = $request->request->get('_username');
        $request->getSession()->set(Security::LAST_USERNAME, $username);
        $password = $request->request->get('_password');

        return array(
            'username' => $username,
            'password' => $password
        );
    }

    public function getUser($credentials, UserProviderInterface $userProvider)
    {
        $username = $credentials['username'];    
        $userRepo = $this->container
            ->get('doctrine')
            ->getManager()
            ->getRepository('AppBundle:User');

        return $userRepo->findOneByUsername($username);
    }

    public function checkCredentials($credentials, UserInterface $user)
    {
        $plainPassword = $credentials['password'];
        $encoder = $this->container->get('security.password_encoder');
        if (!$encoder->isPasswordValid($user, $plainPassword)) {
            return false;
        }

        return true;
    }

    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
    {
        // AJAX! Maybe return some JSON
        if ($request->isXmlHttpRequest()) {
            return new JsonResponse(
            // you could translate the message
                array('message' => $exception->getMessageKey()),
                403
            );
        }

        // for non-AJAX requests, return the normal redirect
        return parent::onAuthenticationFailure($request, $exception);
    }

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        //Perform your redirects here for example


        $response = '';
        if($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')){
            $response = $this->container->get('router')->generate('admin_dashboard');
        }

        if($this->container->get('security.authorization_checker')->isGranted('ROLE_USER')){
            $response = $this->container->get('router')->generate('user_dashboard');
        }

        return $response;
    }

    protected function getLoginUrl()
    {
        return $this->container->get('router')
            ->generate('login');
    }

}

希望这可以使您正确地实现所需的内容,

Hopefully this should put you in right path to implementing what you are looking for,

这篇关于登录后symfony fosuserbundle重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆