Multiple entity manager for FOSUserBundle

后端 未结 2 1377
醉梦人生
醉梦人生 2020-12-05 09:55

To use different Entity Manager / Connection based on URL in Symfony if fairly easy. With the following routing configuration



        
2条回答
  •  时光说笑
    2020-12-05 10:01

    FosUserBundle is not able to have more than one entity manager.

    The easiest way I found to use 2 databases, is to override the 'checkLoginAction' of the SecurityController.

    request->get("_username"));
                $user    =   $this->container->get('fos_user.user_manager')->findUserByUsername($username);
            $userDB2 =   .....
    
    
                $password = \trim($request->request->get('_password'));
    
    
                if ($user) {
                  // Get the encoder  for the users password
                  $encoder      =  $this->container->get('security.encoder_factory')->getEncoder($user);
                  $encoded_pass =  $encoder->encodePassword($password, $user->getSalt());
    
                  if (($user->getPassword() == $encoded_pass) || $this->checkSecondEM()) {
                    $this->logUser($request, $user);
                    return new RedirectResponse($this->container->get('router')->generate($this->container->get('session')->get('route'), $request->query->all() ));
                  } else {
                    // Password bad
                      return parent::loginAction($request);   
                  }
                } else {
                  // Username bad
                    return parent::loginAction($request);   
                }
            }
    
    }
    

提交回复
热议问题