What is Multi-Tenancy? How to implement with Simple Trait in Laravel?

Tram Ho

Written by Phan Thanh

What is Multi-Tenancy?

Multi-Tenancy is a single instance of software that serves multiple customers privately.

Simple Understanding Multi-Tenancy is một hệ thống web application có nhiều khách hàng cùng sử dụng nhưng dữ liệu giữa các khách hàng hoàn tập độc lập, khách hàng này không thể truy cập vào dữ liệu của khách hàng kia.

Some examples include:

  • Hotel management system allows many hotels to access with independent accounts, independent data, but still the same site system.
  • The system manages dispatches used in corporations and many subsidiaries, with sites but independent data.
  • Jira project management system
  • CRM system of zoho, saleforce ….

The multi-tenancy problem actually meets a lot and depending on each system, it will be deployed in different ways. In this article, I will join you to learn to implement the simplest way into Laravel Framework project (Because I am also investigating yet know much)

Problem

There is a hotel management system and a book list for each hotel.

  • The CEO of Hotel A registered his account on the system, then created hotels to manage.
  • The CEO of Hotel B is similar (register an account on the system, then create hotels to manage.)

Problem:

  • If not Multi-Tenancy: CEO A can see the information of Mr. B’s hotel: name and address of the customers who have booked on the hotel Mr. B ==> Revealed the information, Mr. A at this time can take advantage of the information obtained to perform incorrect behaviors: spam email + phone number, etc.
  • If you have Multi-Tenancy: All hotel data of Mr. A and Mr. B are completely independent, unrelated and no one can see or access other people’s data (except admin).

Migration

Create an additional created_by_user_id field in the hotels table. This field is used to determine who created the hotel on the system.

  • Note: From ver 5.8, Laravel has changed the data type of the Id field to big int. So when linking foreign keys, you also have to set the data type of the link field to big int.

After that, add created_by_user_id to fillable in Model

Now how do the field insert itself into the DB when the user creates a Hotels? You can use Model Observers . However, in order to expand and reuse in different models, I will create a Trait Multitenantable instead of creating one Observers like this for each Model.

Trait Multitenantable

Create file app/Traits/Multitenantable.php

  • Note: bootMultitenantable() function with the name convention is bootXYZ() , Laravel will run this function automatically when Trait is used (It can be called trait “constructor”).

Trait Multitenantable will have a task when the user creates something, it automatically inserts the DB in the created_by_user_id field with the value of the user id being entered.

Implement trait this on model Hotel

Results when creating new 1 Hotel:

Maybe you are interested

Java is dying?

How to learn programming to not be unemployed?

Filtering Data By User

This is the most important step to solve the problem: All Mr. A and Mr. B’s hotel data is completely independent, irrelevant and no one can see or access other people’s data.

In Laravel is very simple, we use Global Scope to fillter all queries on the Model.

Add to the trait Multitenantable as follows:

Oki, this time it will filter all the hotels of the current user, and the other user’s hotels are not displayed and cannot be accessed.

The pure SQL will look like this: SELECT * FROM hotels WHERE created_by_user_id=1 (with 1 being the id of the user logged in).

Results at Hotel list:

Try accessing the thằng hotel có id=1 mà chưa set trường created_by_user_id :

404 now

Now try set created_by_user_id=2 , back and see the results (Add 1 record in list + access already)

What About Administrator To See All Entries?

In addition to ensure the Admin can view all the data on the system, you can check if more.

Ok, so I considered I implemented Multi-Tenancy on Hotels models.

Now that you want to tái sử dụng Trait này , you can use trong Model immediately (Remember that the implement model must have created_by_user_id field)

Conclude

What have I introduced to you Multi-Tenancy? How to implement Multi-Tenancy in the simplest way to project Laravel Framework. Hopefully from this article you can understand Multi-Tenancy and apply it to your problems.

Share the news now

Source : Viblo