1
所以,我已经通过documentation了解了这个包,但没有任何描述如何列出现有角色或为特定用户分配/更改卷。我需要能够:FOSUserBundle - 如何处理角色?
- 将默认角色分配给新注册的用户。
- 让管理员和超级管理员升级和降级用户。
- 这就要求我能够请参阅我可以选择哪些角色。
那么,这是怎么完成的?
所以,我已经通过documentation了解了这个包,但没有任何描述如何列出现有角色或为特定用户分配/更改卷。我需要能够:FOSUserBundle - 如何处理角色?
那么,这是怎么完成的?
你可以做这样的事情:
$roleHierarchy = $container->getParameter('security.role_hierarchy.roles');
$roles = array_keys($roleHierarchy);
$form = $this->createForm(new UserFormType($roles, $user->getRoles()), $user);
在你UserFormType您可以添加的角色领域是这样的:
protected $roles;
protected $userRoles;
public function __construct($roles, $userRoles)
{
foreach ($roles as $role) {
$theRoles[$role] = $role;
}
$this->roles = $theRoles;
$this->userRoles = $userRoles;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('roles', 'choice', array(
'choices' => $this->roles,
'data' => $this->userRoles,
'expanded' => true,
'multiple' => true,
));
}
所以做这项工作吗? – 2013-04-29 11:47:56