简体   繁体   English

Django中多个站点的用户

[英]Users for multiple sites in Django

I am trying to get multiple sites to use the same database and code but in a way which forces each user to have their own login to each site. 我试图让多个站点使用相同的数据库和代码,但是以一种迫使每个用户对每个站点都有自己的登录名的方式。

I have seen a few suggestions as to how to make this work but I'm not sure which way to go. 我已经看到了一些有关如何进行这项工作的建议,但是我不确定该走哪条路。

I am using the Sites Framework, using the subdomain to identify the current site so I'm not using SITE_ID at all. 我正在使用Sites Framework,并使用子域来标识当前站点,因此我根本不使用SITE_ID。

  1. Use the sites framework - This answer ( https://stackoverflow.com/a/1405902/1180442 ) suggests using the sites framework to do it, but I'm having trouble with the get_user() method, as it doesn't have access to the request and that's where my site info is stored. 使用站点框架 -此答案( https://stackoverflow.com/a/1405902/1180442 )建议使用站点框架来执行此操作,但是我在使用get_user()方法时遇到了麻烦,因为它没有访问该请求,这就是我的网站信息的存储位置。
  2. Use separate databases for users - I'm really not sure about this one but I think it might cause bigger problems down the line. 为用户使用单独的数据库 -我真的不确定这一数据库,但我认为这可能会导致更大的问题。
  3. Change to using SITE_ID - I want to try and avoid this if possible as it will mean having to run many different instances of my app, one for each site, which uses it's own settings.py. 更改为使用SITE_ID-如果可能的话,我想尝试避免这种情况,因为这将意味着必须运行我的应用程序的许多不同实例,每个站点一个使用其自己的settings.py。 This will quickly turn into a nightmare, I think. 我认为,这将很快变成一场噩梦。
  4. Permissions - I'm wondering if this should be something that I get the permissions framework to use? 权限 -我想知道是否应该让我使用权限框架? So one set of users for all sites but each user can have permissions to see each site, as long as they've registered with that site? 因此,只要所有用户都已在该站点上注册,那么所有站点都有一组用户,但每个用户都有权查看每个站点?

Can anyone help with this? 有人能帮忙吗?

I quite like the idea of number 1 but I just need to get the request in the get_user() method so I can do this 我非常喜欢数字1的想法,但是我只需要在get_user()方法中获取请求,就可以做到这一点

def get_user(self, user_id):
        try:
            # I can't do this because there is no request available here
            return User.objects.get(pk=user_id, site=request.site)
        except User.DoesNotExist:
            return None

to prevent people logged in to one site being able to log into another using the same session. 以防止登录到一个站点的人能够使用同一会话登录到另一个站点。

How I actually do it, not for users but for common databases, Is to design a main, hidden app with a REST API architecture. 实际上,我不是为用户而是为普通数据库这样做的,目的是设计一个具有REST API架构的主隐藏应用程序。 My other apps, naturally have their own DB and exchange their data via batch or stream process depending on the need. 我的其他应用程序自然有自己的数据库,并根据需要通过批处理或流处理过程交换数据。 I use django-rest-framework. 我使用django-rest-framework。

For your case what I would do is that whenever a user makes a Log In request I would send it via HTTPS to my main database and get it authenticated in my main app. 对于您的情况,我将要做的是,每当用户发出登录请求时,我都会通过HTTPS将其发送到主数据库,并在主应用程序中对其进行身份验证。 Whenever I would need to validate the user status I would simply make a get request to the main app. 每当我需要验证用户状态时,我只需向主应用程序发出获取请求即可。

This architecture is not that different from the one that many mobile apps have. 这种架构与许多移动应用程序所拥有的架构没有什么不同。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM