注册事件在您的方法中触发,这就是新创建的用户自动登录的原因。
要避免这种行为并使当前用户保持登录状态(不对新创建的用户进行身份验证),请删除以下行:
$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 您正在注册。
Update
对于编辑部分,您必须创建一个特定的方法来重现 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 }