User Authentication and multi-tenancy

114 views
Skip to first unread message

vijay....@jaarwis.com

unread,
Dec 11, 2014, 7:41:46 AM12/11/14
to django...@googlegroups.com
Hi
I started writing an app with multi-tenancy, for which i had to set database connection on fly based on sub-domain's name.My code goes on like this for same:
import re
import threading 
request_cfg = threading.local()


class RouterMiddleware(object):
    def process_view( self, request, view_func, args, kwargs ):
        pattern = re.compile("\\b(http://|https://|www.|.com|8000|:|//)\\W\\d+", re.I)
        words = request.get_host()        
        db_name = [pattern.sub("", words)][0].split('.')[0]
        request_cfg.cfg = db_name
    
    def process_response( self, request, response ):
        if hasattr( request_cfg, 'cfg' ):
            del request_cfg.cfg
        return response


class DatabaseRouter (object):
    def _default_db( self ):
        if hasattr( request_cfg, 'cfg' ):
            return request_cfg.cfg
        else:
            return 'default'
        
    def db_for_read( self, model, **hints ):
        return self._default_db()
    
    def db_for_write( self, model, **hints ):
        return self._default_db()

Problem is after login it does not redirects me to dashboard, i fall on url like this: http://store1.saas.com:8000/accounts/login/?next=/dashboard/
implying that user has not been authenticated and it redirects me back to login page. I printed self.request.user.is_authenticated() in AccountAuthView and it prints True.
If i set request_cfg.cfg = 'default' in my middleware it works fine.Have i broken/bypassed something in middleware or dbrouter? Whats the fix? Thanks

Maik Hoepfel

unread,
Dec 12, 2014, 3:46:34 AM12/12/14
to django...@googlegroups.com
Hi Vijay,

oh wow, steady now. Changing DB on the fly? That's adventurous. What are the specific requirements of the multi-tenancy? What is shared, what isn't? We should be able to find a more robust approach.

Regards,

Maik

--
https://github.com/tangentlabs/django-oscar
http://django-oscar.readthedocs.org/en/latest/
https://twitter.com/django_oscar
---
You received this message because you are subscribed to the Google Groups "django-oscar" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-oscar...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-oscar.
To view this discussion on the web, visit https://groups.google.com/d/msgid/django-oscar/52df0a3f-6812-42eb-8131-39d6ed6b3ba2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vijay Shanker

unread,
Dec 12, 2014, 4:06:03 AM12/12/14
to django...@googlegroups.com
Hi
Every tenant gets its own database, at time of store creation i intend to copy a database schema and set it in settings.DATABASES, but then i ran into this problem which i could not figure out even after banging my head on it for 5-6 hours.

You received this message because you are subscribed to a topic in the Google Groups "django-oscar" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-oscar/9DbTQLmFlmo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-oscar...@googlegroups.com.

Maik Hoepfel

unread,
Dec 12, 2014, 4:26:16 AM12/12/14
to django...@googlegroups.com
Hi,

if each store is completely independent, I would opt to introduce the separation at a higher level. Set the database settings in an environment variable, and spin up separate processes for each shop. You can still share the same codebase, and you will break a lot less assumptions. Of course you can also introduce even more isolation by spinning up containers/VMs or such like, but it doesn't sound like that's what you want.

Regards,

Maik

Reply all
Reply to author
Forward
0 new messages