drqefnd544707688 2014-07-16 13:50
浏览 6
已采纳

对新注册用户的确认似乎不适用于我的开发

I'm overwriting registerAction() with my own logic as follow:

public function saveAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    /** @var $userManager \FOS\UserBundle\Model\UserManagerInterface */
    $userManager = $this->container->get('fos_user.user_manager');
    /** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
    $dispatcher = $this->container->get('event_dispatcher');

    $user = $userManager->createUser();

    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);

    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }

    $entity = new SysUsuario();
    $form = $this->createForm(new UsuarioType(), $entity);
    $form->handleRequest($request);

    $user_data = $request->get('user_register');
    $profile_data = $request->get('user_register')['perfil'];

    if ($form->isValid()) {
        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);

        $user->setUsername($profile_data['rif']);
        $user->setEmail($user_data['email']);
        $user->setPlainPassword($user_data['password']);
        $user->setEnabled(true);

        $role = $profile_data['roleType'];
        if ($role === "O") {
            $user->addRole("ROLE_OPERADOR");
        }
        elseif ($role === "CH") {
            $user->addRole("ROLE_CENTRO_HIPICO");
        }

        $userManager->updateUser($user);

        $profile = new SysPerfil();
        $profile->setPersJuridica($profile_data['persJuridica']);
        $profile->setRif($profile_data['rif']);
        $profile->setCi($profile_data['ci']);
        $profile->setNombre($profile_data['nombre']);
        $profile->setApellido($profile_data['apellido']);
        $profile->setRoleType($profile_data['roleType']);
        $profile->setUser($user);

        $em->persist($profile);
        $em->flush();

        if (null === $response = $event->getResponse()) {
            $url = $this->container->get('router')->generate('fos_user_registration_confirmed');
            $response = new RedirectResponse($url);
        }

        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));

        return $response;
    }
    else {
        $errors = $this->getFormErrors($formProfile);
    }

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

All it's working fine, users are registered and profile are created, email with token arrive to my inbox but when I click on the link I got redirected to login page without any confirmation around activation successfully or failed or whatever. I don't know if I'm right but after the register is done I look into user table and confirmation_token is always empty, is that right? Also it's right to enabled the user by doing $user->setEnabled(true) after update the user trough UserManager? What is missing here? Why confirmation_token is not set? What I'm trying to do is prevent users to login if they are not confirmed they account trough the email that FOSUser send, any advice or help?

  • 写回答

1条回答 默认 最新

  • doupang5433 2014-07-31 14:10
    关注

    Nevermind I found where my error was, I forgot to set confirmation: true under FOSUser configurations at config.yml file:

    # FOSUserBundle
    fos_user:
        registration:
            confirmation:
                enabled:    true # I've missed this one
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 俄罗斯方块中无法同时消除多个满行
  • ¥15 c#转安卓 java html
  • ¥15 os.listdir文件路径找不到
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低
  • ¥15 银河麒麟linux系统如何修改/etc/hosts权限为777
  • ¥50 医院HIS系统代码、逻辑学习
  • ¥30 docker离线安装mysql报错,如何解决?
  • ¥15 构建工单的总账影响在哪里查询或修改
  • ¥15 三个简单项目写完之后有重赏之后联系我