Multi-Tenancy
This module allows you to restrict access to CRUD entries only to the users who actually created them.
Video Demo
If you want to see this module in action, here's a 5-minute video:
How Does It Work
There are two types of Multi-Tenancy - while installing the module, you can choose: - User Multi-Tenancy (every user sees only records they created) - Team Multi-Tenancy (every user sees all records created by any member of their team)
After installing the module, you will see a checkbox for each CRUD separately, whether to use this restriction setting.
First, let's talk about User Multi-Tenancy.
How Does The Result Look In QuickAdminPanel Code?
First, for every affected CRUD we add a field created_by_id with relationship to User model:
Next, we have a special Trait for the filter by user: app/Traits/MultiTenantModelTrait.php
The code here may look complicated, but the logic is simple - whether to add global scope or not.
Read more about Eloquent Query Scopes in official Laravel documentaiton here.
Finally, we use that Trait in the model, like app/Customer.php:
Team Multi-Tenancy
For this setting, we generate separate CRUD called Teams where administrator can manage the teams. Also, every user may belong to multiple teams:
And then the Trait app/Traits/MultiTenantModelTrait.php is expanded with logic about teams:
How To Customize The Module?
All the logic of that module is inside of app/Traits/MultiTenantModelTrait.php file. So whatever you want to customize, you should do it there.
For example, you may want to customize how administrator records are treated. By default, entries created by administrator role user are visible to everyone, because they are considered "system records". If you want to change that, you need to delete orWhereNull('created_by_id') condition in the Trait:
Another customization example, in a blog article: Teams Multi-Tenancy: Add “Team Admin” to Manage Users
For more complex logic, you can copy-paste some logic and create another trait, and use your trait in some models you want.
Finally, if on some models or some queries you want to disable that filtering, there is a method called withoutGlobalScope():
Last updated