我正在尝试为管理员用户制作一个面板来管理用户个人资料。我的意思是,我有一个用户表,管理员可以编辑他们的信息chossing用户。 问题是,当我尝试编辑某些用户配置文件时,我无法保存更改,只允许我更改已登录用户的配置文件。
/**
* Edit the user.
*
* @param Request $request
*
* @return Response
*/
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 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 FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $userManager 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');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Edit the user.
*
* @param Request $request
*
* @return Response
*/
public function editUserAction($id,Request $request)
{
$user= $this->getDoctrine()->getRepository('BackEndBundle:User')->findOneById($id);
if (!is_object($user) || !$user instanceof UserInterface) {
throw new AccessDeniedException('This user does not have access to this section.');
}
/** @var $dispatcher 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 FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $userManager 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');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
'title' => 'Edit User'
));
}
/**
* Edit the user.
*
* @param Request $request
*
* @return Response
*/
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 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 FactoryInterface */
$formFactory = $this->get('fos_user.profile.form.factory');
$form = $formFactory->createForm();
$form->setData($user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var $userManager 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');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
return $this->render('@FOSUser/Profile/edit.html.twig', array(
'form' => $form->createView(),
));
}
路由:
编辑自己的个人资料(记录用户)
exchangeit_back_end_usuari_edit:
path: /edit/{id}
defaults: { _controller: FOSUserBundle:Profile:edit }
TABLE ADMIN
exchangeit_back_end_usuari_edit_taula:
path: /edit-tabla/{id}
defaults: { _controller: FOSUserBundle:Profile:editUser }
编辑:
解决了创造我自己职能的问题。
public function editUserAction($id,Request $request){
$usuari = $this->getDoctrine()->getRepository('BackEndBundle:User')->findOneById($id);
$form = $this->createFormBuilder($usuari)
->add('image', TextType::class, array('label' => 'Imatge','attr' => array(
'class' => 'form-control'),
'label_attr'=> array('class' => 'label_text spaceTop')))
->add('dni', TextType::class, array('label' => 'Dni','attr' => array(
'class' => 'form-control'),
'label_attr'=> array('class' => 'label_text spaceTop')))
->add('lastname', TextType::class, array('label' => 'Cognom','attr' => array(
'class' => 'form-control'),
'label_attr'=> array('class' => 'label_text spaceTop')))
->add('username', TextType::class, array('label' => 'Nom d\'usuari','attr' => array(
'class' => 'form-control'),
'label_attr'=> array('class' => 'label_text spaceTop')))
->add('email', EmailType::class, array('label' => 'Email','attr' => array(
'class' => 'form-control'),
'label_attr'=> array('class' => 'label_text spaceTop')))
->add('password', PasswordType::class, array('label' => 'Password','attr' => array(
'class' => 'form-control'),
'label_attr'=> array('class' => 'label_text spaceTop')))
->add('roles', ChoiceType::class, array('label' => 'Rol',
'attr' => ['class' => 'selectRol'],
'required' => true, 'choices' => array("Traductor" => 'ROLE_TRANS',"Administrador" => 'ROLE_ADMIN', "Usuari" => 'ROLE_USER'), 'multiple' => true))
->add('save', SubmitType::class, array('label' => 'Editar Usuari',
'attr' => array(
'class' => 'btn btn-warning mt')))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$password = $usuari->getPassword();
$encoder = $this->container->get('security.password_encoder');
$passwordEncrypt = $encoder->encodePassword($usuari, $password);
$usuari->setPassword($passwordEncrypt);
$em->persist($usuari);
$em->flush();
$this->get('session')->getFlashBag()->add(
'notice',array(
'type' => 'success',
'msg' => 'S\'ha editat l\'usuari'
));
return $this->redirect($this->generateurl('exchangeit_back_end_llistaUsuaris'));
};
return $this->render('BackEndBundle:Default:form.html.twig', array(
'title' => 'Editar Usuari',
'form' => $form->createView()
));
}