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