简体   繁体   English

Symfony 3 FOSUserBundle在提交编辑配置文件后重定向到登录

[英]Symfony 3 FOSUserBundle redirect to login after submiting edit profile

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. 这是我的问题,我正在使用FOSUserBundle和Symfony 3,我创建了一个继承FOSUserBundle的UserBundle,因此我可以在需要时覆盖它的一些部分,因为项目的需要我创建了两个防火墙,一个用于后端,第二个用于后端对于前端部分,一切正常,直到我尝试编辑当前登录用户的配置文件(用户名和电子邮件),当我在加载编辑表单时使用var_dump($ this-> getUser())我得到所有当前用户的数据但提交后我得到var_dump($ this-> getUser())= null并重定向到登录表单。

This is the security.yml 这是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: routing.yml中admin(后端)部分的路由:

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 这是我的ProfileController.php中的editAction,可以通过上面的路由文件中的路由admin_profile_edit访问,该操作会覆盖admin_profile_edit中的路径

// 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. 现在加载编辑表单时一切正常,如果我使用var_dump($ this-> getUser()),当前用户数据显示完好,但在提交表单后,相同的var_dump为null并重定向到登录表单。

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. 我不知道我是否遗漏了某些东西或做错了,因为我已经在Symfony 2.8中的另一个项目做了同样的事情,它完美无缺地工作。

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 ; 扩展基本操作(登录),并使用redirect添加自定义逻辑;
See Link : 链接

This redirects using a 301 return code to the client: 这使用301返回代码重定向到客户端:

$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_editadmin_profile_edit ,加载编辑表单的路由是后端用户的 admin_profile_edit ,但我忘了在表单操作中更改它,这就是为什么在提交用户后不再重新调整因为它被重定向到应该在前端部分工作的fos_user_profile_edit editAction。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM