Symfony3:如何删除当前用户并重定向到主页?

时间:2017-06-24 19:21:55

标签: php symfony session doctrine-orm

我正在研究Delete User功能。用户可以删除他的帐户,然后他将被重定向到主页。

这是使用AJAX call实现的,在Controller中执行以下操作:

/**
 * @Route("settings/delete-user/{userId}", name="delete-user")
 */
public function deleteUserAction(Request $request,
                                 $userId,
                                 EntityManagerInterface $em,
                                 TranslatorInterface $translator,
                                 FormErrorCollector $errorCollector,
                                 SessionInterface $session,
                                 TokenStorageInterface $tokenStorage)
{
    $user = $this->getUser();
    $deleteUserForm = $this->createForm(ConfirmPasswordType::class);

    $deleteUserForm->handleRequest($request);
    if($request->isXmlHttpRequest() && $user->getId() == $userId){

        if($deleteUserForm->isValid()){

            $em->remove($user);
            $em->flush();

            $session->invalidate(0);

            return new JsonResponse(array(
                'status' => 'success',
                'message' => $translator->trans('USER_DELETED_SUCCESS')
            ));
        }else{

            $errors = $errorCollector->getErrors($deleteUserForm);

            return new JsonResponse(array(
                'status' => 'failure',
                'errors' => $errors
            ));
        }

    }else{

        return new JsonResponse('FORBIDEN');

    }
}

实际上,用户从数据库中删除,然后出现一个带有确认的模态和返回主页的链接。问题是当用户点击链接时,Symfony会显示以下错误:

  

您无法从不包含标识符的EntityUserProvider刷新用户。必须使用Doctrine映射的自己的标识符序列化用户对象。

显然,Symfony无法找到用户,因为我刚删除它。我试图删除SessionTokenStorage,希望问题得到解决,但根本不会。

那么,如何将已删除的用户重定向到主页?

更新

如果您使用的是Symfony 3.3或更高版本,则可以使用Autowire。答案的改进将是:

1.-添加用于TokenStorageInterface:

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

2.-将TokenStorageInterface作为参数传递给DeleteAction:

public function deleteUserAction(Request $request,$userId,TokenStorageInterface$tokenStorage)
{

3.-清除令牌和会话:

$tokenStorage->setToken(null);
$session->invalidate();

您还可以使用以下方式将autowire与会话服务结合使用:

use Symfony\Component\HttpFoundation\Session\SessionInterface;

1 个答案:

答案 0 :(得分:0)

您可以尝试在使用$this->get('security.token_storage')->setToken(null);删除之前手动注销用户,例如

/**
 * @Route("settings/delete-user/{userId}", name="delete-user")
 */
public function deleteUserAction(Request $request,
                                 $userId,
                                 EntityManagerInterface $em,
                                 TranslatorInterface $translator,
                                 FormErrorCollector $errorCollector,
                                 SessionInterface $session,
                                 TokenStorageInterface $tokenStorage)
{
    $user = $this->getUser();
    $deleteUserForm = $this->createForm(ConfirmPasswordType::class);

    $deleteUserForm->handleRequest($request);
    if($request->isXmlHttpRequest() && $user->getId() == $userId){

        if($deleteUserForm->isValid()){

            // force manual logout of logged in user    
            $this->get('security.token_storage')->setToken(null);

            $em->remove($user);
            $em->flush();

            $session->invalidate(0);

            return new JsonResponse(array(
                'status' => 'success',
                'message' => $translator->trans('USER_DELETED_SUCCESS')
            ));
        }else{

            $errors = $errorCollector->getErrors($deleteUserForm);

            return new JsonResponse(array(
                'status' => 'failure',
                'errors' => $errors
            ));
        }

    }else{

        return new JsonResponse('FORBIDEN');

    }
}

如果您正在使用“记住我”功能,请务必将答案重新设置为Log user out in Symfony 2 application when "remember me" is enabled,因为事情会变得更加棘手。