-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi tenant multi database support, tried all but not working. #1499
Comments
Hi, you mentioned me from the issue I posted before. I would like to be honest with you that I am new to this package as well. I am not sure if the issue has something to do with this package or Eloquent. Why do you need to set-up the connection in your model constructor? I see you have already set the Have you defined this client connection in your db configuration? Did you create a connection configuration named client in config/database.php? |
Yes, I have a connection named 'client' in my DB configuration file. The reason of adding $connection in constructor was that even after adding connection property on Model, still the roles and properties were being fetched from the default MySQL connection. |
Sounds like your |
@drbyte , is it possible to have multiple settings for this package on the same laravel app? Anyway, I think, he is not trying to change the tables used by Role and Permission. If I understand it correctly, he has the same tables in two different databases, that is why he is using different connections for admin and client. So in that case the setting in Have you tried using the standard Model methods for assigning relations? Since the Role and Permission are just Eloquent Models, maybe you should try the Model methods first just to see if the client connection works, if it does (which I think should work) then possibly its the method in the package. I have not checked the codes for this package for I have no time. So maybe you can check it yourself once you confirmed that everything works as expected using the standard Model methods. |
Yes. One of those complications is switching out the config to alternate configs when you switch tenants. This is most often best done by observing an event that you fire when switching to a specific tenant. That's the basic fundamental to accommodate; again, read up on the concepts before implementing them, and if using a package be sure to study how that package implements it before using it.
No, you must switch the config during runtime. This package (like most) only knows ONE configuration at any time. But when you decide that you're in a specific tenant that's when you must switch the config to use the config that suits the tenant. |
@drbyte I got your point more clearly now. The I believe the So as expected, @drbyte's solution of loading the tenant configuration will solve your problem. I think, although not 100% sure, you just need to create a config for the client where you define Role and Permission class to use. Although, I do not know how to load the tenant specific config during runtime . |
@rlvillacarlos I used tinker to debug the issue mentioned by you. When I start tinker and after switching to any customer database and then if I try to access the value of Role and Permission model from permission configuration file, I see that the role and permission model are switched as well. |
@drbyte I have one unique column company id in customers table on main database. Customer need to enter the company ID as first step to login, if provided company ID exist in the main database, application will connect to the customer database and then after filling the email and password customer session will start, right after login I switch the Role and Permission models in permission configuration file and other things like configuring requests and queues etc. |
If you're not also switching the caching system (or at least the prefix) used by the tenant when switching tenants, you may need to also call |
@drbyte Yes, right after new tenant login, in authenticated method, I am doing that as well. /**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
$company = Company::with('plan')->first();
if($user->blocked)
{
Auth::guard('client')->logout();
flash()->error('You are blocked to access account. Reason - <b> ' .$user->block_reason);
return back();
}
// Reset cached roles and permissions
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
flash()->success('Welcome back.');
} I used Debugbar to look for the database queries, and as you can see permissions are still being fetched from the main database and that's why when I try to assign permission, I receive error that permission does not exist. Which of course |
Let's build a new app to recreate the problem. Starting from a fresh Laravel install. |
@drbyte Okay, these are the steps to recreate this problem only.
|
Thanks for the list. Also, I think there are some things missing in your list, such as all the exact steps you're using to switch connections and configurations. In short: the problem is in the code. But to identify where, we need to have the code to look at. |
@drbyte I have solved this by updating the permission config file. I have removed default modals and added new Role and Permission modal with connection property set to the client means these modal will fetch permissions and roles from the client database. But again if I go back to the main admin dashboard which is using default database connection, in this case roles and permissions are not being updating and showing error that "permission not found". I have added EnforceMainDatabaseOnPermissions middleware, which will update the config file and swap the default modals back and set db_connection property to 'default'. So basically if the main database permissions are working then client permissions will not work and vice versa. I have also tried to dd config values and they are as expected. I don't know why after switching modals and db_connection property the permissions are being fetched from wrong database. |
My solution. Using Bouncer package to manage roles and abilities on the main connection for the Admin side. Now everything is fine. |
I tried to extend the models, as well as updating permission config files while switching to tenant database but still when I try to update the role and permissions it does not work and always fetching roles and permissions using default database connection.
Permission model
Configuration update while switching to tenant connection
Role and permission seeder
Now when I try to update permission, I receive an error that permission does not exist, It means permissions are still being fetched from the main database.
The text was updated successfully, but these errors were encountered: