根据用户角色重定向Symfony登录表单。

问题描述 投票:0回答:1

我使用maker bundle创建了一个标准的登录表单,当用户成功登录后,它会调用函数onAuthenticationSuccess重定向到新的页面。当用户成功登录后,它会调用函数onAuthenticationSuccess来重定向到新页面。

public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {         
        return new RedirectResponse($this->urlGenerator->generate('app_homepage'));      
    }

然而,我想根据用户的角色重定向到不同的页面。我想做这样的事情。

if ($this->security->isGranted('ROLE_STANDARD_USER')) {
    return new RedirectResponse($this->urlGenerator->generate('app_homepage')); 
}

if ($this->security->isGranted('ROLE_SYS_ADMIN')) {
    return new RedirectResponse($this->urlGenerator->generate('app_ADMINpage')); 
}

但我得到的错误是未定义的属性: AppSecurity/LoginFormAuthenticator::$security(未定义属性)

非常感谢大家的帮助。

symfony security authentication user-roles
1个回答
0
投票

我没有太多东西可供参考,但似乎可以看到 LoginFormAuthenticator的默认实现。 不要求 授权检查器.

你可以通过使用构造函数向你的类中注入一个AuthorizationChecker来解决这个问题。依赖注入的工作原理示例

既然你已经使用了制造者捆绑包,那么可以肯定你已经使用了 开启自动布线 的服务,这意味着Symfony内核将自动完成所有剩下的工作。

© www.soinside.com 2019 - 2024. All rights reserved.