Skip to content

A Vemto plugin for simple user authentication multi-tenancy using a trait

Notifications You must be signed in to change notification settings

TiagoSilvaPereira/vemto-simple-multitenancy-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Multi Tenancy

This is a Vemto plugin. Vemto is a GUI Laravel generator with a complete set of tools for starting new Laravel projects.

This plugin is intended to bring simple User Login multi-tenancy to your Vemto Laravel projects.

It adds a Trait (BelongsToTenant.php) to all models you marked as "owned by tenant", making it possible to create and list records that are owned by the Authenticated User.

Permissions

By default, Vemto generates permissions and adds them to the super-admin (you need to change the code or manually add them to other users).

So, when using this plugin, it is recommended to initially disable the permissions modules, so it would not be necessary to manually add permissions or change the code to test the multi-tenancy.

image

Settings

This plugin has only two simple settings:

  • Tenant field name: the field that is used to identify the tenant (the authentication model, that is User by default)
  • Models owned by tenant - the models owned by the tenant - it is recommended to mark only models directly related to the "User" model (models that have a belongsTo:user relationship).

image

After marking a model as "owned by tenant", it will show a small yellow marker on the Schema Editor:

image

IMPORTANT: - all models marked as "owned by tenant" need to have a belongsTo:user relationship (or whatever authentication model you are using) and the relationship FK field needs to have the same name you configured on the Plugin Settings.

image

Trait Scope

After generating the code, this plugin will add the trait BelongsToTenant.php to the models marked as "owned by tenant".

All models with that trait have a Global Scope that automatically saves the user_id (or the field you configured) and retrieves the data based on the Authenticated User.

If you want to retrieve something without the Tenancy Scope, you can use the withoutTenancy method. For example:

Order::all(); // Return all orders owned by the authenticated user

Order::withoutTenancy()->all(); // Return all orders from the database

Releases

No releases published

Packages

No packages published