Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a form to edit user roles with FriendsOfSymfony UserBundle

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:

  • The form doesn't get populate with the current user roles, how should I do that?
  • When receiving the form, how can I update the user?

Thanks a lot

like image 364
petekaner Avatar asked Nov 04 '15 14:11

petekaner


People also ask

How do I add a role to a user in Symfony?

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 !

How do I add or edit a user role?

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.

Why do we need roles in Symfony 3?

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.

How to add a role to a user in fosuserbundle?

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:


1 Answers

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.

like image 107
petekaner Avatar answered Nov 15 '22 09:11

petekaner