src/Kunstmaan/UserManagementBundle/Controller/UsersController.php
public function editAction(Request $request, $id)
    {
        // The logged in user should be able to change his own password/username/email and not for other users
        if ($id == $this->container->get('security.token_storage')->getToken()->getUser()->getId()) {
            $requiredRole = 'ROLE_ADMIN';
        } else {
            $requiredRole = 'ROLE_SUPER_ADMIN';
        }
        $this->denyAccessUnlessGranted($requiredRole);

        /* @var $em EntityManager */
        $em = $this->getDoctrine()->getManager();

        /** @var UserInterface $user */
        $user = $em->getRepository($this->container->getParameter('fos_user.model.user.class'))->find($id);
        if ($user === null) {
            throw new NotFoundHttpException(sprintf('User with ID %s not found', $id));
        }

        $userEvent = new UserEvent($user, $request);
        $this->container->get('event_dispatcher')->dispatch(UserEvents::USER_EDIT_INITIALIZE, $userEvent);

        $options = array('password_required' => false, 'langs' => $this->container->getParameter('kunstmaan_admin.admin_locales'), 'data_class' => get_class($user));
        $formFqn = $user->getFormTypeClass();
        $formType = new $formFqn();

        if ($formType instanceof RoleDependentUserFormInterface) {
            // to edit groups and enabled the current user should have ROLE_SUPER_ADMIN
            $options['can_edit_all_fields'] = $this->isGranted('ROLE_SUPER_ADMIN');
        }

        $event = new AdaptSimpleFormEvent($request, $formFqn, $user, $options);
        $event = $this->container->get('event_dispatcher')->dispatch(Events::ADAPT_SIMPLE_FORM, $event);
        $tabPane = $event->getTabPane();

        $form = $this->createForm($formFqn, $user, $options);

        if ($request->isMethod('POST')) {
            if ($tabPane) {
                $tabPane->bindRequest($request);
                $form = $tabPane->getForm();
            } else {
                $form->handleRequest($request);
            }

            if ($form->isSubmitted() && $form->isValid()) {
                /* @var UserManager $userManager */
                $userManager = $this->container->get('fos_user.user_manager');
                $userManager->updateUser($user, true);

                $this->addFlash(
                    FlashTypes::SUCCESS,
                    $this->container->get('translator')->trans('kuma_user.users.edit.flash.success.%username%', [
                        '%username%' => $user->getUsername(),
                    ])
                );

                return new RedirectResponse(
                    $this->generateUrl(
                        'KunstmaanUserManagementBundle_settings_users_edit',
                        array('id' => $id)
                    )
                );
            }
        }

        $params = array(
            'form' => $form->createView(),
            'user' => $user,
        );

        if ($tabPane) {
            $params = array_merge($params, array('tabPane' => $tabPane));
        }

        return $params;
    }
public function editAction(Request $request, $id)
    {
        // The logged in user should be able to change his own password/username/email and not for other users
        if ($id == $this->container->get('security.token_storage')->getToken()->getUser()->getId()) {
            $requiredRole = 'ROLE_ADMIN';
        } else {
            $requiredRole = 'ROLE_SUPER_ADMIN';
        }
        $this->denyAccessUnlessGranted($requiredRole);

        /* @var $em EntityManager */
        $em = $this->getDoctrine()->getManager();

        /** @var UserInterface $user */
        $user = $em->getRepository($this->container->getParameter('fos_user.model.user.class'))->find($id);
        if ($user === null) {
            throw new NotFoundHttpException(sprintf('User with ID %s not found', $id));
        }

        $userEvent = new UserEvent($user, $request);
        $this->container->get('event_dispatcher')->dispatch(UserEvents::USER_EDIT_INITIALIZE, $userEvent);

        $options = array('password_required' => false, 'langs' => $this->container->getParameter('kunstmaan_admin.admin_locales'), 'data_class' => get_class($user));
        $formFqn = $user->getFormTypeClass();
        $formType = new $formFqn();

        if ($formType instanceof RoleDependentUserFormInterface) {
            // to edit groups and enabled the current user should have ROLE_SUPER_ADMIN
            $options['can_edit_all_fields'] = $this->isGranted('ROLE_SUPER_ADMIN');
        }

        $event = new AdaptSimpleFormEvent($request, $formFqn, $user, $options);
        $event = $this->container->get('event_dispatcher')->dispatch(Events::ADAPT_SIMPLE_FORM, $event);
        $tabPane = $event->getTabPane();

        $form = $this->createForm($formFqn, $user, $options);

        if ($request->isMethod('POST')) {
            if ($tabPane) {
                $tabPane->bindRequest($request);
                $form = $tabPane->getForm();
            } else {
                $form->handleRequest($request);
            }

            if ($form->isSubmitted() && $form->isValid()) {
                /* @var UserManager $userManager */
                $userManager = $this->container->get('fos_user.user_manager');
                $userManager->updateUser($user, true);

                $this->addFlash(
                    FlashTypes::SUCCESS,
                    $this->container->get('translator')->trans('kuma_user.users.edit.flash.success.%username%', [
                        '%username%' => $user->getUsername(),
                    ])
                );

                return new RedirectResponse(
                    $this->generateUrl(
                        'KunstmaanUserManagementBundle_settings_users_edit',
                        array('id' => $id)
                    )
                );
            }
        }

        $params = array(
            'form' => $form->createView(),
            'user' => $user,
        );

        if ($tabPane) {
            $params = array_merge($params, array('tabPane' => $tabPane));
        }

        return $params;
    }
public function editAction ( Request $ request , $ id ) { // The logged in user should be able to change his own password/username/email and not for other users if ( $ id == $ this -> container -> get ( 'security.token_storage' ) -> getToken ( ) -> getUser ( ) -> getId ( ) ) { $ requiredRole = 'ROLE_ADMIN' ; } else { $ requiredRole = 'ROLE_SUPER_ADMIN' ; } $ this -> denyAccessUnlessGranted ( $ requiredRole ) ; /* @var $em EntityManager */ $ em = $ this -> getDoctrine ( ) -> getManager ( ) ; /** @var UserInterface $user */ $ user = $ em -> getRepository ( $ this -> container -> getParameter ( 'fos_user.model.user.class' ) ) -> find ( $ id ) ; if ( $ user === null ) { throw new NotFoundHttpException ( sprintf ( 'User with ID %s not found' , $ id ) ) ; } $ userEvent = new UserEvent ( $ user , $ request ) ; $ this -> container -> get ( 'event_dispatcher' ) -> dispatch ( UserEvents :: USER_EDIT_INITIALIZE , $ userEvent ) ; $ options = array ( 'password_required' => false , 'langs' => $ this -> container -> getParameter ( 'kunstmaan_admin.admin_locales' ) , 'data_class' => get_class ( $ user ) ) ; $ formFqn = $ user -> getFormTypeClass ( ) ; $ formType = new $ formFqn ( ) ; if ( $ formType instanceof RoleDependentUserFormInterface ) { // to edit groups and enabled the current user should have ROLE_SUPER_ADMIN $ options [ 'can_edit_all_fields' ] = $ this -> isGranted ( 'ROLE_SUPER_ADMIN' ) ; } $ event = new AdaptSimpleFormEvent ( $ request , $ formFqn , $ user , $ options ) ; $ event = $ this -> container -> get ( 'event_dispatcher' ) -> dispatch ( Events :: ADAPT_SIMPLE_FORM , $ event ) ; $ tabPane = $ event -> getTabPane ( ) ; $ form = $ this -> createForm ( $ formFqn , $ user , $ options ) ; if ( $ request -> isMethod ( 'POST' ) ) { if ( $ tabPane ) { $ tabPane -> bindRequest ( $ request ) ; $ form = $ tabPane -> getForm ( ) ; } else { $ form -> handleRequest ( $ request ) ; } if ( $ form -> isSubmitted ( ) && $ form -> isValid ( ) ) { /* @var UserManager $userManager */ $ userManager = $ this -> container -> get ( 'fos_user.user_manager' ) ; $ userManager -> updateUser ( $ user , true ) ; $ this -> addFlash ( FlashTypes :: SUCCESS , $ this -> container -> get ( 'translator' ) -> trans ( 'kuma_user.users.edit.flash.success.%username%' , [ '%username%' => $ user -> getUsername ( ) , ] ) ) ; return new RedirectResponse ( $ this -> generateUrl ( 'KunstmaanUserManagementBundle_settings_users_edit' , array ( 'id' => $ id ) ) ) ; } } $ params = array ( 'form' => $ form -> createView ( ) , 'user' => $ user , ) ; if ( $ tabPane ) { $ params = array_merge ( $ params , array ( 'tabPane' => $ tabPane ) ) ; } return $ params ; }
Edit a user

@param int $id

@Route("/{id}/edit", requirements={"id" = "\d+"}, name="KunstmaanUserManagementBundle_settings_users_edit", methods={"GET", "POST"})
@Template("@KunstmaanUserManagement/Users/edit.html.twig")

@throws AccessDeniedException

@return array
Edit a user