0

I'm using the apartment gem in a rails app.

I have one database with schemas for each tenant and one public schema for the Tenant table.

I have excluded the www subdomain:

Apartment::Elevators::Subdomain.excluded_subdomains = ['www']

Then, if I enter public.page.com or www.page.com, Apartment won't switch to another tenant, but it will stay at the public one. Of course "public" is not a tenant itself, is just the common data between tenants, so, I don't want any user using the public schema.

What would be the correct way to avoid this?

This app is running on AWS, so, route 53 is going to prevent this, but, although I want to avoid rails from serving request through this subdomain.

lcjury
  • 1,158
  • 1
  • 14
  • 26
  • 1
    [here](https://gorails.com/episodes/multitenancy-with-apartment?autoplay=1) you will found the answer. You must configure you routes file, and reject connections from excluded subdomains. – Alex Baidan Feb 18 '19 at 18:46
  • That was exactly what I wanted :)! thanks, Oleksii Baidan, Why don't you put a bit of code to mark your answer as the correct one? – lcjury Feb 18 '19 at 18:51
  • will do in a minute :) – Alex Baidan Feb 18 '19 at 18:51

1 Answers1

1

Apart from excluding domain from Apartment, you need to exclude them from routes. In my project I'm using this code for manage this:

I'm using initializer to create array of excleded subdomains.

# config/initializers/apartment/subdomain_exlusions.rb
Apartment::Elevators::Subdomain.excluded_subdomains = ['www', 'admin']

Then, we can use this array in helper class in routes.

# config/routes.rb
class ExcludedSubdomainConstraint
  def self.matches?(request)
    request.subdomain.present? && !Apartment::Elevators::Subdomain.excluded_subdomains.include?(request.subdomain)
  end
end

Rails.application.routes.draw do
  constraints ExcludedSubdomainConstraint do
    # here routes that are accessible in subdomains
  end
end

As a bonus, you can route excluded subdomains to another constrain


class DashboardSubdomainConstraint
  def self.matches?(request)
    Apartment::Elevators::Subdomain.excluded_subdomains.include?(request.subdomain) || request.subdomain == ''
  end
end

constraints DashboardSubdomainConstraint do
  namespace :dashboard do
    get '/settings'
  end
end

will give you a route like www.domain.com/dashboard/settinigs with access to public tenant.

TIP. And you can use different root method in concerns

Alex Baidan
  • 1,065
  • 7
  • 15