public function edit(Request $request, $id)
    {
        $currentUser = $this->getUser();
        $userEntity = $this->getUserEntity($id);
        if ($userEntity === false) {
            return $this->redirectToRoute('users');
        }
        /** @var Permissions $permissions */
        $permissions = $this->app['permissions'];
        $availableRoles = array_flip(array_map(
            function ($role) {
                return $role['label'];
            },
            $permissions->getDefinedRoles()
        ));
        $mutableRoles = $permissions->getManipulatableRoles($currentUser->toArray());

        $formOptions = [
            'password' => [
                'required' => !$userEntity->getId()
            ],
            'roles'    => [
                'choices' => $availableRoles,
                'mutable' => $mutableRoles,
            ],
        ];

        // Generate the form
        $form = $this->createFormBuilder(FormType\UserEditType::class, $userEntity, $formOptions)
            ->getForm()
            ->handleRequest($request)
        ;

        if ($form->isSubmitted() && $form->isValid()) {
            /** @var \Bolt\Form\FormType\UserData $data */
            $data = $form->getData();
            $roleMutator = new ListMutator($availableRoles, $mutableRoles);
            $data->applyToEntity($userEntity, $roleMutator);

            $saved = $this->getRepository(Entity\Users::class)->save($userEntity);
            if ($saved) {
                $this->flashes()->success(Trans::__('page.edit-users.message.user-saved', ['%user%' => $userEntity->getDisplayname()]));
                $this->app['logger.system']->info(
                    Trans::__('page.edit-users.log.user-updated', ['%user%' => $userEntity->getDisplayname()]),
                    ['event' => 'security']
                );
            } else {
                $this->flashes()->error(Trans::__('page.edit-users.message.saving-user', ['%user%' => $userEntity->getDisplayname()]));
            }

            if ($userEntity !== false && $userEntity->getId() === $currentUser->getId() && $userEntity->getUsername() !== $currentUser->getUsername()) {
                // If the current user changed their own login name, the session
                // is effectively invalidated. If so, we must redirect to the
                // login page with a flash message.
                $this->flashes()->error(Trans::__('page.edit-users.message.change-self'));

                return $this->redirectToRoute('login');
            } elseif ($userEntity !== false) {
                // Return to the 'Edit users' screen.
                return $this->redirectToRoute('users');
            }
        }

        $context = [
            'kind'        => $id ? 'edit' : 'create',
            'form'        => $form->createView(),
            'note'        => '',
            'displayname' => $userEntity->getDisplayname(),
        ];

        return $this->render('@bolt/edituser/edituser.twig', $context);
    }