好的,我正在尝试检查用户是否有特定角色,我做了this
然而,当我这样做时:
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('nombre',null,array('label' => 'Usuario'))
->add('email')
->add('password', 'repeated', array(
'type' => 'password',
'invalid_message' => 'Los campos deben coincidir',
'first_name' => 'password',
'second_name' => 'confirmar password',
'options' => array('required' => false)
))
->add('cliente', 'entity', array(
'class' => 'ClientesBundle:Cliente',
'empty_value' => 'Company',
'required' => false,
'empty_data' => null)
**)**
$user = $this->securityContext->getToken()->getUser();
**if ($user->getRol() == 'ROLE_SUPER_ADMIN'){**
->add('rol')
**}**
;
}
也尝试了这个:
**if ($this->securityContext->getToken()->getUser()->getRol() === 'ROLE_SUPER_ADMIN'){**
->add('rol')
**}**
粗体线(带**的那些)有一条红色的小线表示错误,如果...... 我该如何解决这个问题?
答案 0 :(得分:15)
从控制器,您必须将用户对象传递给表单构建器
$form = $this->createForm(
new YourType(),
$data,
array('user' => $this->getUser())
);
然后在表单构建器中,您可以从$options
:
public function buildForm(FormBuilder $builder, array $options)
{
$user = $options['user']
}
不要忘记使用setDefaultOptions()
索引扩展user
:
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
...
'user' => null
));
}
答案 1 :(得分:15)
如果您将表单类型声明为服务,则可以在您的类中注入令牌存储。
所以你在services.yml
中声明这样的服务:
my_form:
class: AppBundle\Services\MyFormType
public: true
arguments: ['@security.token_storage']
表单类如下:
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
class MyFormType extends AbstractType
{
protected $tokenStorage;
public function __construct(TokenStorage $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$user = $this->tokenStorage->getToken()->getUser();
// Use the user object to build the form
}
}
答案 2 :(得分:5)
我知道这是一个古老的问题,但是我想提出一种更好的方法来检查表单类型内的角色。
使用TokenInterface和User对象的问题是它不检查继承。例如,考虑以下{
"id": "5c26321bd8f4113d43b91141",
"idMemberCreator": "5b203bc7e47d817a8138bc37",
"data.list.name": "Sorji for QA",
"data.list.id": "5b0a2543b89acdbdb85f7b42",
"data.board.shortLink": "iyCzZ5jx",
"data.board.name": "FlicksIO",
"data.board.id": "5b0a251f68a9e74b8ec3b3ac",
"data.card.shortLink": "vOt2vO7v",
"data.card.idShort": 92,
"data.card.name": "New column in main for Storefront provider correlation.",
"data.card.id": "5b9c0023533f7c26424ea4ed",
"data.card.closed": true,
"data.old.closed": false,
"type": "updateCard",
"date": "2018-12-28T14:24:27.455Z",
"limits": {},
"memberCreator.id": "5b203bc7e47d817a8138bc37",
"memberCreator.avatarHash": "73bfa48c76c3c92615fe89ff79a6c5ae",
"memberCreator.avatarUrl": "https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae",
"memberCreator.fullName": "Marie Bond",
"memberCreator.idMemberReferrer": null,
"memberCreator.initials": "MB",
"memberCreator.username": "mb"
}
:
security.yml
如果您的用户已添加security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
而不是ROLE_SUPER_ADMIN
,则如果您使用ROLE_ADMIN
,则上述解决方案将失败,就像用户一样没有明确为其用户分配$user->hasRole('ROLE_ADMIN')
,并且ROLE_ADMIN
不检查层次结构。
使用hasRole()
来访问AuthorizationCheckerInterface
函数。
isGranted()
这将尊重use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class MyFormType extends AbstractType {
protected $auth;
public function __construct(AuthorizationCheckerInterface $auth) {
$this->auth = $auth;
}
public function buildForm(FormBuilderInterface $builder, array $options) {
// ...
if($this->auth->isGranted('ROLE_ADMIN')) {
// Do the thing here
}
}
}
中定义的任何层次结构。如果我们使用与上述相同的yml文件,则如果用户的个人资料分配了security.yml
而不是$auth->isGranted('ROLE_ADMIN')
,则ROLE_SUPER_ADMIN
将返回 true 。
答案 3 :(得分:1)
我成功完成了此操作,而没有传递给symfony 3.4中的服务。我知道我的方法不是最“专业”的,但是它很简单并且有效。
首先,从您的控制器发送您的formType用户
$form = $this->get('form.factory')->create(addPlanExpectedType::class, $business,
array('user' => $this->getUser())
);
第二,恢复角色,并验证此$ roles数组中是否存在“ ROLE_AMIN”
public function buildForm(FormBuilderInterface $builder, array $options)
{
$businessId = $options['data']->getId();
$user = $options['user'];
$roles = $user->getRoles();
$boolAdmin = in_array('ROLE_ADMIN', $roles);