Symfony:如何在Formtype或security.authorization\u检查器中获取当前用户角色
我有一个Formtype,我需要访问当前用户角色,因为我想决定显示哪些字段。 例如,是否可以访问Symfony:如何在Formtype或security.authorization\u检查器中获取当前用户角色,symfony,security,authentication,symfony-2.3,symfony-3.1,Symfony,Security,Authentication,Symfony 2.3,Symfony 3.1,我有一个Formtype,我需要访问当前用户角色,因为我想决定显示哪些字段。 例如,是否可以访问安全性.授权\u检查程序,以便我可以创建一个if子句: if (!$this->get('security.authorization_checker')->isGranted('IS_ADMIN')) { .... 您可以将表单注册为服务,然后将security.authorization\u checker作为参数传递,请检查下面的示例代码 form.service.id:
安全性.授权\u检查程序
,以便我可以创建一个if子句:
if (!$this->get('security.authorization_checker')->isGranted('IS_ADMIN')) { ....
您可以将表单注册为服务,然后将
security.authorization\u checker
作为参数传递,请检查下面的示例代码
form.service.id:
class: YourFormClass
arguments: ['@security.authorization_checker']
tags:
- { name: form.type }
然后在表单类中创建
\u构造(AuthorizationChecker$AuthorizationChecker)
方法,然后使用AuthorizationChecker
检查角色您可以将表单注册为服务,然后将安全性传递给用户。authorization\u checker
作为参数,检查下面的示例代码
form.service.id:
class: YourFormClass
arguments: ['@security.authorization_checker']
tags:
- { name: form.type }
然后在表单类中创建
\uu构造(AuthorizationChecker$AuthorizationChecker)
方法,然后使用AuthorizationChecker
检查角色如上所述,您可以基于我使用的代码片段创建onw表单,以便在任何非管理员用户处呈现字段:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Validator\Constraints\IsTrue as TrueConstraint;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
class RegistrationType extends AbstractType
{
/**
* @var AuthorizationChecker
*/
private $authorizationChecker=null;
public function __construct(AuthorizationChecker $authorizationChecker)
{
$this->authorizationChecker=$authorizationChecker;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle']);
if(!$this->authorizationChecker->isGranted('ROLE_ADMIN'))
{
$builder->add('accept_terms',CheckboxType::class,["label"=>"register.acceptTerms","required"=>true,'translation_domain' => 'FOSUserBundle',
'mapped' => false,'constraints' => new TrueConstraint(array('message' => 'Your Confirmation Message','groups' => 'Registration'))]);
}
}
// Extra stuff ommited for convenience
}
正如您所看到的,如果用户是管理员,我会通过$this->authorizationChecker->isgrated('ROLE\u admin')
一段代码来使用它
因此,您只需将
'@security.authorization\u checker'
作为服务参数。如上所述,您可以基于我使用的这段代码创建onw表单,以便在任何非管理员用户处呈现字段:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Validator\Constraints\IsTrue as TrueConstraint;
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
class RegistrationType extends AbstractType
{
/**
* @var AuthorizationChecker
*/
private $authorizationChecker=null;
public function __construct(AuthorizationChecker $authorizationChecker)
{
$this->authorizationChecker=$authorizationChecker;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle']);
if(!$this->authorizationChecker->isGranted('ROLE_ADMIN'))
{
$builder->add('accept_terms',CheckboxType::class,["label"=>"register.acceptTerms","required"=>true,'translation_domain' => 'FOSUserBundle',
'mapped' => false,'constraints' => new TrueConstraint(array('message' => 'Your Confirmation Message','groups' => 'Registration'))]);
}
}
// Extra stuff ommited for convenience
}
正如您所看到的,如果用户是管理员,我会通过$this->authorizationChecker->isgrated('ROLE\u admin')
一段代码来使用它
因此,您只需将
“@security.authorization\u checker”
作为服务参数。您可以将该FormType作为服务,然后注入容器,您可以如上所述。您可以将该FormType作为服务,然后注入容器,您可以如上所述。不,在Symfony 3中,不允许在fromtypeNo,我使用的是symfony 3.2,它允许我在表单中添加_构造(),您会在表单类型中发现symfony 3*不允许_构造吗?如果您可以在symfony3.2文档中找到构造,请检查@MaulikSavaliya完全同意您的意见,我们可以使用任何版本的symfony形式的构造方法。AuthorizationChecker
的命名空间是什么?symfony\Component\Security\Core\Authorization\AuthorizationChecker;不,在Symfony 3中,不允许在fromtype中有构造函数不,我使用的是Symfony 3.2,它允许我在表单中添加_construct(),在表单类型中您会发现Symfony 3*不允许_构造?如果您可以在symfony3.2文档中找到构造,请检查@MaulikSavaliya完全同意您的意见,我们可以使用任何版本的symfony形式的构造方法。AuthorizationChecker
的命名空间是什么?symfony\Component\Security\Core\Authorization\AuthorizationChecker;