question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature-Request: Change tenant-domain from subdomain to url-parameter?

See original GitHub issue

Hi guys,

I am just thinking: One of the big benefits of this app is the added security layer via database-schemas. So users are probably quite security concerned and will also invest in an ssl-certificate.

Which is quite expensive for a startup… I think unlimited ssl-subdomains-certificates are around 500$ per year?

Which brings me to the point: How easy do you think would it be to implement a switch “DOMAINTYPE = SUBDOMAIN/URL-PARAMETER”?

So that instead of

tenant.domain.com 

we would see

www.domain.com/tenant/*?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
owaiscommented, Mar 6, 2019

I got it to work with something like this in python3:

custom middleware

from django.conf import settings
from django.db import connection
from django_tenants.middleware.main import TenantMainMiddleware

from tenants.models import Tenant
from tenants.urls import generate_tenant_url_module


class TenantMiddleware(TenantMainMiddleware):
    @staticmethod
    def tenant_id_from_request(request):
        if request.path.startswith("/t/"):
            tenant_id = request.path.lstrip("/t/").split("/")[0]
            return tenant_id
        return ""

    def process_request(self, request):
        connection.set_schema_to_public()
        tenant_id = self.tenant_id_from_request(request)
        if not tenant_id:
            return

        try:
            tenant = Tenant.objects.get(id=tenant_id)
        except Tenant.DoesNotExist:
            raise self.TENANT_NOT_FOUND_EXCEPTION(
                'No tenant for tenant ID"%s"' % tenant_id
            )
        request.tenant = tenant
        connection.set_tenant(request.tenant)
        request.urlconf = generate_tenant_url_module(tenant)

tenants/urls.py

import sys
from types import ModuleType

from django.contrib import admin
from django.urls import path


def generate_tenant_url_module(tenant) -> str:
    module_name = "tenant_urls_{0}".format(tenant.schema_name)
    mod = ModuleType(module_name)
    mod.urlpatterns = [path("t/{0}/admin".format(str(tenant.id)), admin.site.urls)]
    sys.modules[module_name] = mod

    return module_name

Looks like this is all that is needed to make /abc work on the public schema and /t/<tenant_id>/abc work on the tenant schema. Django admin works as well and so do all the reverse calls django and other libs make internally.

1reaction
kosz85commented, Jul 27, 2015

It’s easy, I have such middleware in my projects that supports both methods, thought we mostly use /tnt/<tenant_name>/ instead of domain. It’s just process_response part that is selecting current tenant, and probably adding some value to request or some global settings like settings.URL_PREFIX. It’s a bit more painful if you use other tools that do smth with url like django-rest-framework, as you need to hack into rest_framework.reverse.reverse and rest_framework.reverse.reverse_lazy, to always provide your current URL_PREFIX, but after you have it, it allows you to have lang in your paths (/tnt/test/en/), and any other feature related to prefixing url path.

For free certs check https://startssl.com.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature-Request: Change tenant-domain from subdomain to ...
Hi guys, I am just thinking: One of the big benefits of this app is the added security layer via database-schemas.
Read more >
htaccess, redirect virtual subdomain to URL parameter
First I added the subdomain * under mydomain.ne.ro, pointed to domain folder/path: ... Also You can change with this(for Uppercase or Lowercase letters...
Read more >
Change subdomain authentication type in Azure Active Directory
Change default subdomain authentication settings inherited from root domain settings in Azure Active Directory.
Read more >
Feature request: limit matching to subdomain [not possible]
I'm not having any trouble filling logins on any matching URL. I have a.domain, b.domain, c.domain with different passwords per sub domain. But ......
Read more >
Cant change my subdomain - Typeform Community
I tried to change my subdomain but not happened, showing some error. no one else has used that domain from our sidemy website...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found