FOSUserBundle with multiple databases

I have one system. In this one facility with clients. From this system, I want to send authentication to these clients for another website / system, and unload / insert that client as fos_user on that system. On both systems, I have used FOSUserBundle to login / authenticate the user.

If I use the CompilerPassInterface from this link, it works well for me as a fos_user system. Paste the client as fos_user to another site. And also log in from this website.

But the problem is that on my current website the changepassword and registration don't work even if I switch my db connection on the current website.

public function sendAuthAction(Request $request)
{
    $user = $this->get('security.context')->getToken()->getUser();

    $request  = $this->getRequest();
    $id = $request->get('id');
    $em = $this->get('doctrine')->getManager();
    $client = $em->getRepository('AcmeClientBundle:Clients')->find($id);

    $username = $client->getName();
    $password= "";//here i auto generate the password;
    $email= $client->getEmail();
    $this->get('doctrine.dbal.dynamic_connection')->forceSwitch($dbname2, $dbuser2, $dbpass2);
    $em2 = $this->get('doctrine')->getManager('dynamic');
    $client = $em2->getRepository('AcmeUserBundle:User')->findBy(array('email' => $email));
    if($client)
    {
        return new JsonResponse('You had alreay send authentication to this client.');
    }
    $userManager = $this->container->get('fos_user.user_manager');

    $newuser = $userManager->createUser();

    $newuser->setEnabled(true);
    $newuser->setPlainPassword($password);
    $newuser->setUsername($username);
    $newuser->setRoles(array('ROLE_USER'));
    $newuser->setEmail($email);

    $userManager->updateUser($newuser, true);

    $this->get('doctrine.dbal.dynamic_connection')->forceSwitch($dbname, $dbuser, $dbpass);
    return new JsonResponse('success');
}

      

Problems arise with this method

  • After inserting the client into another connection, I log out of the current system.

  • And my password change controller doesn't work, it gives me the required username of the exception. once again forcing the connection to the current db in the changessword controller.

I tried using this method

fos_user:
   db_driver: orm
   firewall_name: main
   user_class: Acme\UserBundle\Entity\User
   service:
       user_manager: acme.user_manager.conn1 # need to set default

      

and also tried

model_manager_name: dynamic #or default

      

But in this case, any connection defined in config.yml, FOSUserBundle will only work for that connection. This way I can log in and register with the current system, or I can immediately insert the client into my second system.

I want to know that there is some method that I switch between the two doctrine connections along with FOSUserBundle for both.

My config file is located here:

doctrine:
  dbal:
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
            mapping_types:
                enum: string              
        dynamic:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name2%" # it not given in case of compilerpassinterface
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
            mapping_types:
                enum: string
            wrapper_class: 'Acme\DoctrineBundle\Connection\ConnectionWrapper' #it in case for dynamically switching db. 

   orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    entity_managers:
        default:
            connection: default
            mappings:
                FOSUserBundle: ~
                AcmeUserBundle: ~
                AcmeClientBundle: ~
        dynamic:
            connection: dynamic
            mappings:
                FOSUserBundle: ~
                AcmeUserBundle: ~
                AcmeClientBundle: ~

fos_user:
   db_driver: orm
   firewall_name: main
   user_class: Acme\UserBundle\Entity\User
   registration:
      form:
         type: acme_user_registration
   change_password:
      form:
         type: acme_user_change_password
         name: acme_user_change_password

      

+3


source to share





All Articles