路由检查用户角色Symfony2

时间:2015-12-04 17:36:07

标签: symfony

我有两个捆绑包,我希望只有用户具有已定义的角色才能访问其中一个捆绑包中的路由。

路由器匹配器的逻辑应该是:

如果用户具有角色

| name             | path  | success |
|------------------|-------|---------|
| bundle_1_route_1 | /     | false   |
| bundle_1_route_2 | /test | true    |

如果用户没有角色

| name             | path  | success |
|------------------|-------|---------|
| bundle_1_route_1 | /     | false   |
| bundle_1_route_2 | /test | false   |
| bundle_2_route_1 | /aaa  | false   |
| bundle_2_route_2 | /test | true    |

问题是我无法使用安全性,因为路径是相同的

我尝试使用@security注释http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/security.html 但是我在bundle_1_route_2没有角色时拒绝访问,其他网址没有被检查。

如果角色不匹配,我想继续检查所有可用的网址

我找到了另一个解决方案,但这不是很干净,如果会话不存在会产生错误

bundle_1:
    resource: "@Bundle1/Controller/"
    type:     annotation
    prefix:   /
    condition: "'ROLE_FILTER' in request.getSession().get('bundle1.user').getRoles()"

有没有办法在路由上创建完全自定义的条件?

0 个答案:

没有答案
相关问题