• Stars
    star
    115
  • Rank 305,916 (Top 7 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 3 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PostgreSQL Row Level Security, The Rails right way to do multitenancy

Contributors Forks Stargazers Issues LinkedIn Hireable Donate


PgRls

PostgreSQL Row Level Security
The Rails right way to do multitenancy


Explore the docs ยป

Report Bug ยท Request Feature ยท API Repo

Table of Contents

It's time we start doing multitenancy right! You can avoid creating a separate Postgres schema/databases for each customer or trying to ensure the WHERE clause of every single query includes the particular company. Just integrate PgRls seamlessly to your application.

This gem will integrate PostgreSQL RLS to help you develop a great multitenancy application.

Required Installation

Installing

Add this line to your application's Gemfile:

gem 'pg_rls'

And then execute:

$ bundle install

Or install it yourself with:

$ gem install pg_rls

Instructions

rails generate pg_rls:install company #=> where company eq tenant model name

You can change company to anything you'd like, for example, tenant This will generate the model and inject all the required code

For any new model that needs to be under rls, you can generate it by writing

rails generate pg_rls user #=> where user eq model name

and it will generate all the necesary information for you.

You can swtich to another tenant by using

PgRls::Tenant.switch :app #=> where app eq tenant name

Don't forget to update how you want PgRls to find your tenant, you can set multiple options by modifying api/config/initializers/pg_rls.rb search_methods

You can add the following configuration to your Database Config File to improve performance and remove PgRls::SecureConnection from aplication_record.rb

# app/config/database.yml
<% def db_username
    return PgRls.username unless ENV['AS_DB_ADMIN']

    Rails.application.credentials.dig(:database, :server_1, :username)
   end %>

...

development:
  <<: *default
  database: example_development
  username: <%= db_username %> # Apply this to production and all env including tests

...

Testing

If you are getting PG::InsufficientPrivilege: ERROR: permission denied you can override does permistion by running RAILS_ENV=test rake db:grant_usage

Many application uses some sort of database cleaner before running thair spec so on each test that we run we'll have an empty state. Usually, those gems clear our user configuration for the database. To solve this issue, we must implement the following:

# spec/rails_helper.rb

...
# some database cleaning strategy

config.before(:suite) do
  # Create the tenant which in this example is company and we are using FactoryBot
  FactoryBot.create(:company, subdomain: 'app')
  # In this default case our initializer is set to search by subdomain so will use it
  PgRls::Tenant.switch :app
end

...

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/dandush03/pg_rls. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the PgRls project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Note

Currently we only support subdomain as a searcher but will soon integrate slug/domain and cookies support we recommed the use of ``

Show your support

Give a โญ๏ธ if you like this project!

If this project help you reduce time to develop, you can give me a cup of coffee :)

paypal

More Repositories

1

ndm

NDM Law Firm & Associates Is a law firm website where users can find information about the lawyers and the law firm itself.
JavaScript
19
star
2

FinalCapstone.BackEnd

This project is designed so you can keep your daily task like; working, sleeping, etc. and see a resume of your day.
Ruby
16
star
3

Microverse-JS-Capstone

My First Game with WebGL and Phaser, a good game with a nice intuitive UI, this game keeps track of an API and show the best practices of it.
JavaScript
13
star
4

FinalCapstone.FrontEnd

This project is designed so you can keep your daily task like; working, sleeping, etc. and see a resume of your day.
JavaScript
13
star
5

Microvers-RoR-Capstone

Twitter-like page, based on Gregoire Vella's design. on this page, you will be able to appreciate a user validation with the Rails framework and with the gem device.
Ruby
13
star
6

react-capstone

This is a WebApp where you can compare two different types of companies and look at it in a candlestick chart
JavaScript
9
star
7

Portafolio

My Portfolio, Check It Out!. This encapsulates all my projects and gives a details info regarding myself
JavaScript
6
star
8

capstone-build-linter

An Indent Gem that helps you to find an error in your code, this was my first attempt to create an open-source project that could be used anywhere
Ruby
5
star
9

Bazaar

This is a fruit bazar, my first html page ever! done with only static pages and Sass.based on Mohammed Awad design
HTML
4
star
10

Associations-Rails

Ruby
4
star
11

JS-TicTacToe

This is my first interactive game, don't judge the UI it was more about good and bad JS practice. This second Microverse JS Project
JavaScript
4
star
12

React-Calculator

JavaScript
3
star
13

Building-with-Responsive-Design

HTML
3
star
14

lap

Ruby
2
star
15

Design-Teardown

Microverse 5th Project
HTML
2
star
16

static-pages

Ruby
2
star
17

Ruby-on-Rails-Form

Ruby
2
star
18

dweb

JavaScript
2
star
19

Basic-Routes-Views-and-Controllers

Ruby
2
star
20

E-Commerce

Ruby
2
star
21

Positioning-and-Floating-Elements

3rd Microverse HTML Project
HTML
1
star
22

ndm-test-repo

JavaScript
1
star
23

devise-react

1
star
24

Using-Bootstrap

HTML
1
star
25

Building-with-Backgrounds-and-Gradients

HTML
1
star
26

embedding-images-and-video

First Microverse Project Mike&Daniel
HTML
1
star
27

aws-ec2-deploy

1
star
28

JS-Library

Microverse First JS Project
JavaScript
1
star
29

JS-WheatherApp

JavaScript
1
star
30

dandush03

JavaScript
1
star
31

JS-RestaurantPage

Third Microverse Project of the JS Section
JavaScript
1
star