Here is my problem, im working with FOSUserBundle and Symfony 3, i've created a UserBundle that inherits FOSUserBundle so i can override some of it's parts when needed, for project's needs i've created two firewalls, one for the backend and the second for the frontend part, everything worked fine until i tried to edit the current logged user's profile(username and email), when i use var_dump($this->getUser()) while loading the editing form i get all of the current user's data but after submiting i get var_dump($this->getUser()) = null and get redirected to login form.
This is the security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
admin:
pattern: /admin(.*)
form_login:
provider: fos_userbundle
login_path: /admin/login
check_path: /admin/login_check
csrf_token_generator: security.csrf.token_manager
default_target_path: /admin/
always_use_default_target_path: true
logout:
path: /admin/logout
target: /admin/login
anonymous: true
main:
pattern: ^/
form_login:
provider: fos_userbundle
login_path: /login
check_path: /login_check
csrf_token_generator: security.csrf.token_manager
default_target_path: /
always_use_default_target_path: true
# if you are using Symfony < 2.8, use the following config instead:
# csrf_provider: form.csrf_provider
logout:
path: /logout
target: /login
anonymous: true
access_control:
- { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
The routes for the admin(backend) part in routing.yml:
admin_login:
path: /admin/login
defaults: { _controller: FOSUserBundle:Security:login }
admin_check:
path: /admin/login_check
defaults: { _controller: FOSUserBundle:Security:check }
admin_logout:
path: /admin/logout
defaults: { _controller: FOSUserBundle:Security:logout }
admin_profile_show:
path: /admin/profile/show
defaults: { _controller: FOSUserBundle:Profile:show }
admin_profile_edit:
path: /admin/profile/edit
defaults: { _controller: FOSUserBundle:Profile:edit }
admin_profile_change_password:
path: /admin/profile/changePassword
defaults: { _controller: FOSUserBundle:ChangePassword:changePassword }
And this is the editAction in my ProfileController.php that can be accessed via the route admin_profile_edit
in the routing file above, that action overrides the one in FOSUserBundle
// UserBundle/Controller/ProfileController.php
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');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::PROFILE_EDIT_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
$requestAttributes = $this->container->get('request_stack')->getCurrentrequest()->attributes;
if ($requestAttributes->get('_route') == 'admin_profile_edit') {
$template = sprintf('UserBundle:Profile:user_admin_edit.html.twig');
} else {
$template = sprintf('FOSUserBundle:Profile:edit.html.twig');
}
return $this->render($template, array(
'form' => $form->createView()
));
}
Now when loading the editing form everything works fine and the current user data show perfectly if i use var_dump($this->getUser()), but after submiting the form the same var_dump is null and im redirected to the login form.
I don't know if im missing somthing or doing it wrong, because i've already did the same thing with another project in Symfony 2.8 and it worked perfectly without any problems.
I hope that i've provided the max informations for my problem.
Thank you for your help.
Extend base Action(login), and add custom logic with redirect
;
See Link :
This redirects using a 301 return code to the client:
$this->redirect(
$this->generateUrl(
'person_in_need_view',
array('id' => $id)
)
);
This redirects internally:
$this->forward(
'person_in_need_view',
array('id' => $id)
)
解决方案是将表单操作从fos_user_profile_edit
为admin_profile_edit
,加载编辑表单的路由是后端用户的 admin_profile_edit
,但我忘了在表单操作中更改它,这就是为什么在提交用户后不再重新调整因为它被重定向到应该在前端部分工作的fos_user_profile_edit
editAction。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.