2017-03-22 140 views
0

我有一个FormType,我需要访问当前的Useres ROLE,因为我想确定哪些字段正在显示。 是否有可能获得访问security.authorization_checker例如,这样我可以做一个if从句:Symfony:如何获取FormType或security.authorization_checker中的当前用户角色

if (!$this->get('security.authorization_checker')->isGranted('IS_ADMIN')) { .... 
+0

你就可以说FormType作为一个服务,然后注入容器,你可以像上面。 –

回答

2

你可以注册形式的服务,然后通过security.authorization_checker作为参数,检查下面的示例代码。

form.service.id: 
    class: YourFormClass 
    arguments: ['@security.authorization_checker'] 
    tags: 
     - { name: form.type } 

然后在你的窗体类创建__construct(AuthorizationChecker $authorizationChecker)方法,然后用AuthorizationChecker检查ROLE

+0

不,在Symfony 3中不允许在类型 – Zwen2012

+1

中有构造函数不,我正在使用symfony 3.2,它允许我在form中添加__construct(),在formType中发现symfony3 *不允许__construct? 事件你可以在symfony3.2文档中找到__construct,检查http://symfony.com/doc/current/form/use_empty_data.html –

+1

@MaulikSavaliya完全同意你,我们可以在任何版本的symfony表单中使用构造方法。 –

相关问题