如何在Symfony2中的Form Builder类中访问用户角色 [英] How can i access the user Role in Form Builder class in Symfony2

查看:66
本文介绍了如何在Symfony2中的Form Builder类中访问用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我的表单为 UserType  - > add('description')
- > add('createdAt')

现在我想要的是,如果登录的用户有角色(ROLE_SUPERADMIN)然后他可以看到这样的额外的字段

   - > add('description')
if($ user.hasRole(ROLE_SUPERADMIN))
- > add('createdAt')

其实我必须为许多领域做到这一点。有什么办法可以做一些自定义类型,所以如果那个类型是那里只有管理员可以看到像



- > add( 'createdAt',MyCustomType)

解决方案

很简单。只需使您的自定义表单输入服务,具体取决于安全上下文:

 使用Symfony\ Component\Security\Core\SecurityContext; 

class UserType extends AbstractType
{

private $ securityContext;

public function __construct(SecurityContext $ securityContext)
{
$ this-> securityContext = $ securityContext;
}

public function buildForm(FormBuilder $ builder,array $ options)
{
//当前记录的用户
$ user = $ this-> ; securityContext->为gettoken() - >的getUser();

//向构建器添加字段
}

public function getDefaultOptions(array $ options)
{
return array(
'required'=> false,
'data_class'=>'Acme\HelloBundle\Entity\User'
);
}

public function getName()
{
return'user_type';
}
}

然后将该类标记为服务,标签 form.type

 服务:
form.type.user:
class:Acme\HelloBundle\Form\Type\UserType
arguments:[@ security.context]
标签:
- {name:form.type,alias:user_type}

做$ 新的UserType(),从容器中抓取服务:

 $ form = $ this-> createForm($ this-> get('form.type.user'),$ data); 


I have the form as UserType with field like this

->add('description')
  ->add('createdAt')

Now i want that if the Logged in user has Role (ROLE_SUPERADMIN) then he can see extra fields like this

 ->add('description')
if($user.hasRole(ROLE_SUPERADMIN))
->add('createdAt')

Actually i have to do that for many fields . is there any way i can make some custom type so that if that type is there then only admin can see those like

->add('createdAt',"MyCustomType")

解决方案

Pretty simple. Just make your custom form type a service, depending on the security context:

use Symfony\Component\Security\Core\SecurityContext;

class UserType extends AbstractType
{

    private $securityContext;

    public function __construct(SecurityContext $securityContext)
    {
        $this->securityContext = $securityContext;
    }

    public function buildForm(FormBuilder $builder, array $options)
    {
        // Current logged user
        $user = $this->securityContext->getToken()->getUser();

        // Add fields to the builder
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'required'   => false,
            'data_class' => 'Acme\HelloBundle\Entity\User'
        );
    }

    public function getName()
    {
        return 'user_type';
    }
}

Then mark the class as a service, with the special tag form.type:

services:
    form.type.user:
        class: Acme\HelloBundle\Form\Type\UserType
        arguments: ["@security.context"]
        tags:
            - { name: form.type, alias: user_type }

In your controller, instead of doing new UserType(), grap the service from the container:

$form = $this->createForm($this->get('form.type.user'), $data);

这篇关于如何在Symfony2中的Form Builder类中访问用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆