Authenticate in a database and change information in others

Asked

Viewed 380 times

1

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

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

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

I don’t know if I could explain it properly, but in short, the application will only be a bridge that will connect each user to their database, and the login of all should be centralized in a database "main".

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

NOTE: I cannot keep creating several connections in config/database.php.

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

1 answer

1

You can create a middleware that takes the user’s authenticated bd information and sets a new database 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 seat settings use in the class scope:

protected $connection = 'bd_dinamico';

or

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

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

    return $something;
}

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

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.