我是Symfony2和FOSUserBundle的新手。我了解捆绑包是什么以及使用它的用途,但我对如何使用捆绑包与已有的视图和控制器相关联有疑问。
我已经设置了捆绑包并使其正常工作,但我对下一个任务感到困惑。
设置:在 FOSUserBundle 的登录页面上,我希望将"管理员用户"路由到某个页面,将"普通用户"路由到另一个页面。我把这个逻辑放在哪里?我目前在我的捆绑包的默认控制器中拥有它,但获取页面:localhost isn't working...localhost redirected you too many times...
我清除了缓存,但结果仍然相同。
默认控制器:
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
}
}
}
现在我的终极目标是一旦用户登录,他们的用户名就会显示在他们被发送到的页面上。我该如何编码?它去哪儿了?
我真的很感激你的帮助,谢谢大家。
Symfony 2.7 : FOSUserBundle 2.0
编辑:安全.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 }
或者你可以这样做:
将其添加到 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
并在服务中创建相应的服务.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>
然后对于 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;
}
}
这就是问题所在。索引操作处的路由是 /
。您在 indexAction 中的条件基本上被解释为:"当前路由(由于从此路由内部调用而始终/
)是否等于 admin_login
?这就是为什么你的条件总是返回 false。
按照这种逻辑,路由/
始终重定向到路由login
。并且路由login
始终重定向到/
,因为登录角色IS_AUTHENTICATED_ANONYMOUSLY
但您已经通过身份验证(这意味着您当前的角色是ROLE_NORMAL
或ROLE_ADMIN
)。
编辑:现在我阅读了您的评论更新,您只需要添加访问控制的路径:
access_control:
- { path: ^/$, role: ROLE_ADMIN }
- { path: ^/product$, role: ROLE_ADMIN }
并对默认值/索引执行以下操作:
function indexAction() {
return $this->redirectToRoute('product');
}
EDIT2:如果您的 indexAction 除了重定向到路由 product
之外没有执行任何其他操作,则可以删除控制器并将以下内容添加到 routeting.yml:
root:
path: /
defaults:
_controller: FrameworkBundle:Redirect:redirect
route: product
permanent: true
源