如何在由“站点”支持的多个 Django 站点上获取唯一用户框架?
我正在构建一个 Django 站点框架,它将为多个独立站点提供支持,所有站点都使用相同的应用程序,但具有自己的模板。我计划通过使用多个设置文件并为它们设置唯一的 SITE_ID 来实现此目的,就像 Django 文档中针对 django.contrib.sites Framework
但是,我不希望站点 A 的用户能够登录站点 B。在检查了由syncdb创建的用户表后,我看不到可能将用户限制到特定站点的列。我还尝试在一个站点上创建一个用户“bob”,然后使用 shell 命令列出另一侧的所有用户,果然,bob 出现在那里。
如何确保所有用户都仅限于各自的网站?
I am building a Django site framework which will power several independent sites, all using the same apps but with their own templates. I plan to accomplish this by using multiple settings-files and setting a unique SITE_ID for them, like suggested in the Django docs for the django.contrib.sites framework
However, I don't want a user from site A to be able to login on site B. After inspecting the user table created by syncdb, I can see no column which might restrict a user to a specific site. I have also tried to create a user, 'bob', on one site and then using the shell command to list all users on the other side and sure enough, bob shows up there.
How can I ensure all users are restricted to their respective sites?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最兼容的方法是创建一个 用户配置文件模型,其中包含站点模型的外键,然后编写 自定义身份验证后端,根据该 FK 的值检查当前站点。一些示例代码:
定义您的配置文件模型,比方说在 app/models.py 中:
编写自定义身份验证后端,继承默认后端,比方说在 app/auth_backend.py 中:
此身份验证后端假设所有用户都有一个配置文件;您需要确保您的用户创建/注册过程始终创建一个。
重写的
authenticate
方法可确保用户只能登录正确的站点。每个请求都会调用get_user
方法,根据用户会话中存储的身份验证信息从数据库中获取用户;我们的覆盖确保用户无法登录站点 A,然后使用相同的会话 cookie 获得对站点 B 的未经授权的访问。(感谢 Jan Wrobel 指出需要处理后一种情况。)The most compatible way to do this would be to create a user Profile model that includes a foreign key to the Site model, then write a custom auth backend that checks the current site against the value of that FK. Some sample code:
Define your profile model, let's say in app/models.py:
Write your custom auth backend, inheriting from the default one, let's say in app/auth_backend.py:
This auth backend assumes all users have a profile; you'd need to make sure that your user creation/registration process always creates one.
The overridden
authenticate
method ensures that a user can only login on the correct site. Theget_user
method is called on every request to fetch the user from the database based on the stored authentication information in the user's session; our override ensures that a user can't login on site A and then use that same session cookie to gain unauthorized access to site B. (Thanks to Jan Wrobel for pointing out the need to handle the latter case.)您可以插入考虑站点 ID 的自己的授权和身份验证后端。
请参阅 django 文档上的其他身份验证源 身份验证后端参考
除此之外,如果您django源太旧了,你可以自己修改authenticate()或login()代码。毕竟……这不是开源的奇迹之一吗?请注意,这样做可能会影响与其他模块的兼容性。
希望这有帮助。
You can plug your own authorization and authentication backends that take the site id into consideration.
See other authentication sources on the django documentation and the authentication backends references
Besides that, if your django source is too old, you can always modify the authenticate() or login() code yourself. After all... Isn't that one of the wonders of open source. Be aware that by doing so you may affect your compatibility with other modules.
Hope this helps.
你必须知道,很多人抱怨 Django 默认的授权系统和权限 - 它只有对象和对象实例的简单规则 - 这意味着什么,如果不编写任何代码,这是不可能的。
但是,有一些授权挂钩可以帮助您实现此目标,例如:
看看那里:
http://code.djangoproject.com/browser/ django/trunk/django/contrib/auth/models.py
并获得班级许可。
您可以添加自己的权限并为其定义规则(用户和内容类型有一个外键)。
然而,如果没有猴子修补/改变一些方法,这可能会很困难。
You have to know, that many people complain for Django default authorization system and privileges - it has simply rules for objects, for instances of the objects - what it means, that without writing any code it woudn't be possible.
However, there are some authorization hooks which can helps you to achieve this goal, for example:
Take a look there:
http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py
and for class Permission.
You can add your own permission and define rules for them (there is a ForeignKey for User and for ContentType).
However2, without monkeypatching/change some methods it could be difficult.