使用FOS用户包编辑用户配置文件

时间:2016-02-09 07:52:37

标签: php symfony fosuserbundle

我正在使用带有FOSUserBundle的symfony 2,问题是当添加用户时,登录的当前用户配置文件将被当前添加的用户替换,然后当我尝试编辑其他用户配置文件时,唯一可编辑的将是目前登录用户,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()

        ));

    }

2 个答案:

答案 0 :(得分:5)

在您的方法中触发注册事件,这就是新创建的用户自动登录的原因。

要避免此合并并保持当前用户已登录(请勿对新创建的用户进行身份验证),请删除以下行:

$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。

<强>更新

对于编辑部分,您必须创建一个特定的方法来重现editProfile的编译,但是对于给定的用户(不是经过身份验证的用户)。

尝试使用以下内容:

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 }

答案 1 :(得分:0)

从RegisterAction中删除:

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