cartalyst / sentinel

A framework agnostic authentication & authorization system.
BSD 3-Clause "New" or "Revised" License
1.51k stars 238 forks source link

registerAndActivate on a different connection doesn't work with config() #559

Open rxng opened 3 years ago

rxng commented 3 years ago

I am creating and migrating databases on the fly, and have managed to successfully do this with seeding of data.

However, when it comes to seeding a user on this new db using Sentinel::registerAndActivate , Sentinel defaults back to the main connection and creates a user on that connection.

It doesn't respect the connection that I have specified for the new database via config('database.connection')

Is there a workaround for this?

brunogaspar commented 3 years ago

How are you setting the active database to be used?

rxng commented 3 years ago

I am using config(“database.connection”)

And then php artisan migrate and seed using —database=

It all works but Sentinel doesn’t respect the database parameter

brunogaspar commented 3 years ago

The Artisan commands like migrate and seed only set the connection for that session and after it finishes, they restore the connection to what was the default connection, you can check the Laravel source.

So you need to really change the connection at runtime, unsure if modifying the config would suffice here, as you might need to call the setDefaultConnection() method on the Database Connection instance.

rxng commented 3 years ago

The problem is setting default connection changes my main connection also. This interferes with creating tenant on the fly.

I solved this by creating a new model with different connection and setting Sentinel to use this model.

But Sentinel should really allow a connection parameter, that would be so much better

brunogaspar commented 3 years ago

You're more than welcomed to submit a pull request to allow that to be changed at runtime, just keep in mind that it needs to work with both native and framework implementations.