3

I have one system. In that one entity with clients. From this system I want to sent authentication to that clients for another website/system and dump/insert this client as fos_user in that system. In Both system i have used FOSUserBundle for user login/authentication.

If I use CompilerPassInterface from this link, It's working good for me for both system fos_user. Insert client as fos_user in another website. And login from this current website.

But problem is that in my current website changepassword and registration is not working even if i switch my db connection in current site.

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');
}

Using this method problems are

  1. After inserting client in another connection i logout from current system.

  2. And my change password controller is not working, It give me an exception username is required. even again forcing connection to current db in changepssword controller.

I have tried to use 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 whatever connection defined in config.yml FOSUserBundle will work only for that connection. So either i can login and register in current system or i can insert client in my second system at once.

I want to know that any method is there that, i switch between two doctrine connection along with FOSUserBundle work for both.

My config file is 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's 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's 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
Community
  • 1
  • 1
herr
  • 837
  • 1
  • 8
  • 21

0 Answers0