そのため、バンドルのドキュメントを読みましたが、既存のロールを一覧表示したり、特定のユーザーのロールを割り当て/変更したりする方法については何も説明されていません。次のことができる必要があります。
- 新しく登録されたユーザーにデフォルトの役割を割り当てます。
- 管理者とスーパー管理者がユーザーを昇格および降格させます。
- これには、選択できる役割を確認できる必要があります。
それで、これはどのように行われますか?
そのため、バンドルのドキュメントを読みましたが、既存のロールを一覧表示したり、特定のユーザーのロールを割り当て/変更したりする方法については何も説明されていません。次のことができる必要があります。
それで、これはどのように行われますか?
次のようなことができます。
$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,
));
}