使用FOS用户包编辑用户资料[英] Edit user profile using FOS User Bundle

本文是小编为大家收集整理的关于使用FOS用户包编辑用户资料的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在将Symfony 2与FosuserBundle一起使用,问题是添加用户时,当前添加的当前用户配置文件将被当前添加的用户替换,然后当我尝试编辑其他用户配置文件时,唯一的可编辑将是当前登录用户,FosuserBundle的继承功能是否有问题?我希望在添加用户时,当前登录的用户不会随着所添加的内容而更改,而且当我编辑另一个用户配置文件时,我要编辑的用户帐户将被编辑,而不是当前添加的用户帐户用户帐户.

这是我来自fosuserbundle的代码:

注册控制器: //添加用户

public function registerAction(Request $request)

    {

        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */

        $formFactory = $this->get('fos_user.registration.form.factory');

        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */

        $userManager = $this->get('fos_user.user_manager');

        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */

        $dispatcher = $this->get('event_dispatcher');



        $user = $userManager->createUser();

        $user->setEnabled(true);



        $event = new GetResponseUserEvent($user, $request);

        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);



        if (null !== $event->getResponse()) {

            return $event->getResponse();

        }



        $form = $formFactory->createForm();

        $form->setData($user);



        $form->handleRequest($request);



        if ($form->isValid()) {

            $event = new FormEvent($form, $request);

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);



            $userManager->updateUser($user);



            if (null === $response = $event->getResponse()) {   
                $session = $this->getRequest()->getSession();
                $session->getFlashBag()->add('message', 'Sucessfully Added');
                $url = $this->generateUrl('matrix_edi_viewUser');

                $response = new RedirectResponse($url);

            }



            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));



            return $response;

        }



        return $this->render('FOSUserBundle:Registration:register.html.twig', array(

            'form' => $form->createView(),

        ));

    }

配置文件控制器: //用于编辑用户配置文件

public function editAction(Request $request)

    {

        $user = $this->getUser();

        if (!is_object($user) || !$user instanceof UserInterface) {

            throw new AccessDeniedException('This user does not have access to this section.');

        }



        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */

        $dispatcher = $this->get('event_dispatcher');



        $event = new GetResponseUserEvent($user, $request);

        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);



        if (null !== $event->getResponse()) {

            return $event->getResponse();

        }



        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */

        $formFactory = $this->get('fos_user.profile.form.factory');



        $form = $formFactory->createForm();

        $form->setData($user);



        $form->handleRequest($request);



        if ($form->isValid()) {

            /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */

            $userManager = $this->get('fos_user.user_manager');



            $event = new FormEvent($form, $request);

            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);



            $userManager->updateUser($user);



            if (null === $response = $event->getResponse()) {

                //$url = $this->generateUrl('fos_user_profile_show');
                $session = $this->getRequest()->getSession();
                $session->getFlashBag()->add('message', 'Successfully updated');
                $url = $this->generateUrl('matrix_edi_viewUser');
                $response = new RedirectResponse($url);

            }



            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));



            return $response;

        }



        return $this->render('FOSUserBundle:Profile:edit.html.twig', array(

            'form' => $form->createView()

        ));

    }

推荐答案

注册事件是在您的方法中启动的,这就是为什么新创建的用户自动登录的原因.

要避免使用此计数,并将当前用户保留为登录(不要对新创建的用户进行身份验证),请删除以下行:

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

这两行也:

$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

和这一行:

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

现在您只是在创建一个新用户而不告诉FosuserBundle您正在注册中.

更新

对于编辑部件,您必须创建一种特定方法来重现编辑器封面的计数,但对于给定的用户(不是经过身份验证的用户).

尝试使用以下内容:

public function editUserAction($id)
{
    $user = $em->getRepository('YourBunde:User')->find($id);

    if (!is_object($user)) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->get('fos_user.profile.form.factory');

    $form = $formFactory->createForm();
    $form->setData($user);
    $form->handleRequest($request);

    if ($form->isValid()) {
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        $userManager->updateUser($user);

        $session = $this->getRequest()->getSession();
        $session->getFlashBag()->add('message', 'Successfully updated');
        $url = $this->generateUrl('matrix_edi_viewUser');
        $response = new RedirectResponse($url);

    }

    return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
        'form' => $form->createView()
    ));
}

和路线:

security_edit_profile:
    path:     /users/{id}/edit
    defaults: { _controller: YourBundle:Security:editUser }

其他推荐答案

从寄存器中删除:

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

本文地址:https://www.itbaoku.cn/post/2090768.html

问题描述

I'm using symfony 2 with FOSUserBundle, the problem is when adding a user the current user profile logged in will be replaced by the currently added user, then when I tried to edit other user profile, the only editable would be the currently logged in user, is there something wrong with the inherited function from FOSUserBundle? I want that when adding a user, the currently logged in user would not be change with what is being added, and also when I edit another user profile, the user account that I'm trying to edit would be edited, not the currently added user account.

Here are my codes from FOSUserBundle :

Registration Controller : // adding user

public function registerAction(Request $request)

    {

        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */

        $formFactory = $this->get('fos_user.registration.form.factory');

        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */

        $userManager = $this->get('fos_user.user_manager');

        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */

        $dispatcher = $this->get('event_dispatcher');



        $user = $userManager->createUser();

        $user->setEnabled(true);



        $event = new GetResponseUserEvent($user, $request);

        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);



        if (null !== $event->getResponse()) {

            return $event->getResponse();

        }



        $form = $formFactory->createForm();

        $form->setData($user);



        $form->handleRequest($request);



        if ($form->isValid()) {

            $event = new FormEvent($form, $request);

            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);



            $userManager->updateUser($user);



            if (null === $response = $event->getResponse()) {   
                $session = $this->getRequest()->getSession();
                $session->getFlashBag()->add('message', 'Sucessfully Added');
                $url = $this->generateUrl('matrix_edi_viewUser');

                $response = new RedirectResponse($url);

            }



            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));



            return $response;

        }



        return $this->render('FOSUserBundle:Registration:register.html.twig', array(

            'form' => $form->createView(),

        ));

    }

Profile Controller : //used for editing user profile

public function editAction(Request $request)

    {

        $user = $this->getUser();

        if (!is_object($user) || !$user instanceof UserInterface) {

            throw new AccessDeniedException('This user does not have access to this section.');

        }



        /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */

        $dispatcher = $this->get('event_dispatcher');



        $event = new GetResponseUserEvent($user, $request);

        $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_INITIALIZE, $event);



        if (null !== $event->getResponse()) {

            return $event->getResponse();

        }



        /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */

        $formFactory = $this->get('fos_user.profile.form.factory');



        $form = $formFactory->createForm();

        $form->setData($user);



        $form->handleRequest($request);



        if ($form->isValid()) {

            /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */

            $userManager = $this->get('fos_user.user_manager');



            $event = new FormEvent($form, $request);

            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_SUCCESS, $event);



            $userManager->updateUser($user);



            if (null === $response = $event->getResponse()) {

                //$url = $this->generateUrl('fos_user_profile_show');
                $session = $this->getRequest()->getSession();
                $session->getFlashBag()->add('message', 'Successfully updated');
                $url = $this->generateUrl('matrix_edi_viewUser');
                $response = new RedirectResponse($url);

            }



            $dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));



            return $response;

        }



        return $this->render('FOSUserBundle:Profile:edit.html.twig', array(

            'form' => $form->createView()

        ));

    }

推荐答案

The registration event is fired in your method, this is why the newly created user is logged in automatically.

To avoid this comportment and keep the current user as logged in (don't authenticate the newly created user), remove the following line :

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

This two lines too :

$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

And this line :

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

Now you are just creating a new user without telling FOSUserBundle you are in registration.

Update

For the edit part, you have to create a specific method to reproduces the comportment of the editProfile, but for a given user (not the authenticated user).

Try to use the following :

public function editUserAction($id)
{
    $user = $em->getRepository('YourBunde:User')->find($id);

    if (!is_object($user)) {
        throw new AccessDeniedException('This user does not have access to this section.');
    }

    /** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
    $formFactory = $this->get('fos_user.profile.form.factory');

    $form = $formFactory->createForm();
    $form->setData($user);
    $form->handleRequest($request);

    if ($form->isValid()) {
        /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
        $userManager = $this->get('fos_user.user_manager');
        $userManager->updateUser($user);

        $session = $this->getRequest()->getSession();
        $session->getFlashBag()->add('message', 'Successfully updated');
        $url = $this->generateUrl('matrix_edi_viewUser');
        $response = new RedirectResponse($url);

    }

    return $this->render('FOSUserBundle:Profile:edit.html.twig', array(
        'form' => $form->createView()
    ));
}

And the route :

security_edit_profile:
    path:     /users/{id}/edit
    defaults: { _controller: YourBundle:Security:editUser }

其他推荐答案

Remove from the RegisterAction:

$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));