使用FOS用户捆绑包编辑用户个人资料 [英] Edit user profile using FOS User Bundle
问题描述
我将Symfony 2与FOSUserBundle一起使用,问题是当添加一个用户时,当前登录的用户配置文件将被当前添加的用户替换,然后当我尝试编辑其他用户配置文件时,唯一可编辑的将是当前登录的用户,从FOSUserBundle继承的函数有问题吗?我希望添加用户时,当前登录的用户不会随着添加的内容而改变,并且当我编辑另一个用户配置文件时,我要编辑的用户帐户将被编辑,而不是当前添加的用户帐户用户帐户.
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.
这是我来自FOSUserBundle的代码:
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);
这两行也是:
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
这行:
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
现在,您只是在创建新用户,而无需告知FOSUserBundle您正在注册.
Now you are just creating a new user without telling FOSUserBundle you are in registration.
更新
对于编辑部分,您必须创建一种特定方法来重现editProfile的内容,但要针对给定用户(而非经过身份验证的用户).
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).
尝试使用以下内容:
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 }
这篇关于使用FOS用户捆绑包编辑用户个人资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!