开发者

howto integrate routes and security access control

I need to hide some links in template, if route or path is not allowed for logged user. Something like this:

{% if allowedroute 'fos_user_security_logout' %}    
<a href="{{ path('fos_user_security_logout') }}" class="action-logout">
    {{ 'layout.logout'|trans({}, 'FOSUserBundle') }}
    </a>
{% endif %}

How to de开发者_开发问答termine is it allowed in security access_control for logged user? May be there is some functionality like this:

$securityContext->isRouteGranted('routeName');
or
$securityContext->isUriGranted('uri/path');


As far as I know, there is no way to check access to a route from twig. However, since you have to set up security.yml access_control routes with user roles, you CAN check role-based authentication in your template:

{% if is_granted('IS_AUTHENTICATED_REMEMBERED') %}
// logout link here
{% endif %}

It's not as dynamic, but unless your routes change a lot, it shouldn't be a problem.


If you want item hiding for KnpMenuBundle - you need to extend menu renderer and menu item classes to support permissions. You can also try to look at SecurityExtraBundle for putting permissions onto controller actions and play with it to bind security permissions to routes - as far as I know, Sf2 does not have it at the moment. You could try inside your controller the following annotated example:

/**
 * @Route("/create", name="create_group")
 * @Secure(roles="ROLE_CREATE_GROUP")
 * @Template()
 */
public function createGroupAction(Request $request) {
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜