Authenticate in a database and change information in others

1

So I need to create an application with Laravel 5 where different users can be authenticated in a specific database where the system is.

After logging in, each user can change information within the system, and this information must be on another server, another database. Each user will change information in a different database.

My idea is that when the user is authenticated, the database that is linked to his login is defined so that he can change what he wants. The application should know which database to change according to the user who is logged in.

I do not know if I could explain it right, but in short, the application will be just a bridge that will bind each user to his database, and everyone's login should be centralized in a "main" database. >

Can anyone give me a light on how to do this? Is there a "package" I can use?

  

NOTE: I can not create multiple connections in config / database.php.

The connection information will be in the main application database, linked to the user registration that will be authenticated. This has to be dynamic, check the host information and etc. when the user logs in and already make the connection to his database.

    
asked by anonymous 12.08.2016 / 21:41

1 answer

1

You can create a middleware that takes the authenticated user's bd information and sets up a new bank configuration:

$conn = array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'DATABASE',
    'username'  => 'USERNAME',
    'password'  => 'SOME_PASSWORD',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
);

Config::set('database.connections.bd_dinamico', $conn);

and when you want to use the settings of the bank use in the scope of the class:

protected $connection = 'bd_dinamico';

or

public function method()
{
    $this->setConnection('bd_dinamico');

    $this = $someModel->find(1);

    return $something;
}

In this way you will be working with the data within your dynamic connection

References:

20.11.2016 / 17:11