I'm trying to create a controller where I can edit the roles of a user (just that, nothing else) and I'm king of stuck.
I've created a form type:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'roles', 'choice', [
'choices' => ['ROLE_ADMIN', 'ROLE_USER', 'ROLE_CUSTOMER'],
'expanded' => true,
'multiple' => true,
]
)
->add('send', 'submit');
}
First, what would be the best way to retrieve the roles? Is there any way to associate a label to them?
In the controller I have this:
/**
* User role edition
*
* @Route(
* path="/edit-roles",
* name = "backoffice_user_edit_roles",
* requirements = {
* "id_user" = "\d*",
* },
* methods = {"GET"}
* )
*
* @Security("has_role('ROLE_ADMIN')")
*
* @Template
*/
public function editRolesAction($id_user)
{
$user = $this->user_repository->findOneById($id_user);
$form = $this->form_factory->create('dirital_user_roles_form_type', $user);
return [
'form' => $form->createView(),
'user' => $user
];
}
Problems that I have:
Thanks a lot
By default, a symfony application that implements FOSUserBundle, will automatically have access to the Command line tools of this bundle. This tools provide an useful command that allow you to add a role to an user, the fos:user:promote command. This command enables you to add a role to a user or make the user a super administrator: Have fun !
Click the Add New button on the top of the page [Ultimate Member > User Roles] to add a new user role. You'll see a form Add New Role. Configure role settings, then click Create Role. Click the Edit link under the user role title to edit it. You'll see a form Edit Role. Change role settings you need, then click Update Role.
There are a lot of points to handle when you work with user permissions (not mentioned here), in order to make that easier for the user and for the developer, the implementation of Roles is an useful (and basically required) feature of an User system. In this article, you'll learn how to add roles to an user using FOSUserBundle in Symfony 3.
By default, a symfony application that implements FOSUserBundle, will automatically have access to the Command line tools of this bundle. This tools provide an useful command that allow you to add a role to an user, the fos:user:promote command. This command enables you to add a role to a user or make the user a super administrator:
Actually it was easier than I thought – this is the form:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add(
'roles', 'choice', [
'choices' => ['ROLE_ADMIN' => 'ROLE_ADMIN', 'ROLE_USER' => 'ROLE_USER', 'ROLE_CUSTOMER' => 'ROLE_CUSTOMER'],
'expanded' => true,
'multiple' => true,
]
)
->add('save', 'submit', ['label' => 'ui.button.save']);
}
And the controller:
public function editRolesAction(Request $request, $id_user)
{
$user = $this->user_repository->findOneById($id_user);
$form = $this->form_factory->create('dirital_user_roles_form_type', $user);
$form->handleRequest($request);
if($form->isValid())
{
$this->addFlash('success', 'section.backoffice.users.edit_roles.confirmation');
$this->em->persist($user);
$this->em->flush();
$this->redirectToRoute('backoffice_user_edit_roles', ['id_user' => $user->getId()]);
}
return [
'form' => $form->createView(),
'user' => $user
];
}
The only part that remains to do is grabbing the form choices from the config instead of hardcoding them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With