Skip to main content
 首页 » 编程设计

python之Django:将 django.contrib.auth 用于 SAAS(用户、权限等)

2024年09月03日33zhoujg

我正在制作一个 SAAS,我一直在这里提出一系列与内置 Auth 系统相关的问题。我无法理解“为什么”和“如何”。主要是我不明白它如何适合我的 SAAS。

我(确实)知道以下内容:

  1. 您可以这样做:http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users
  2. 有很多理由使用内置的身份验证系统(如安全性)而不是自己动手

我(不知道)以下内容:

class MyUserProfile(models.Model): 
    """ 
    Administrator for an Account. Can edit tickets and other stuff. 
    """ 
    user = AutoOneToOneField(User, primary_key=True) 
    account = models.ForeignKey(Account) 
    dogs_name = models.CharField(max_length=255) 

在前面的示例中,account 正是您所期望的;付费使用我的软件的实体。 user 是我主要关心的问题。有人转到一个页面并使用用户名和密码等创建一个 UserProfile。当他们这样做时,相关的用户在哪里创建?我是否需要根据 request.POST['username'] 等在我的 View 中手动创建它,然后执行

myuserprof = MyUserProfile.create(user=foo_user_just_created, account=foo_account, dogs_name='Spot') 

我不知道为什么,但出于某种原因,我觉得我错过了什么。这个想法要求某人注册一个帐户,然后创建一个 MyUserProfile,其中包含一个要求输入密码、用户名、电子邮件等的表单,然后在我看来创建具有不同部分的 2 个不同的对象(MyUserProfile 和 User)的表格数据。我的意思是我不应该有用户表单吗?就像我说的,我觉得我要么跳过了一步,要么我处在错误的范式中。我不是 Django 的新手,但出于某种原因,我在处理不是我构建的东西时遇到了麻烦(我认为这可能是一个真正的心理问题)。

也许在某些开源项目上有一个很好的例子可以说明这种事情。

更新:糟糕,忘了提到在上面的代码中我尝试使用 django-annoying 中的 AutoOneToOneField,但我不知道所有用户属性的设置位置或如何决定将哪个用户对象附加到它。这东西快把我逼疯了。

此外,我是否需要使用 sites 应用程序来执行此操作,最后“ super 用户”是否拥有对所有内容的所有权限(我不希望帐户“Acme”中的人访问帐户“Microshaft”对象)?或者他们只是拥有所有 View 的所有权限?

请您参考如下方法:

Somebody goes to a page and creates a UserProfile with a username and password, etc.

UserProfile 没有用户名或密码字段。所以应该是某人转到一个页面并创建一个用户。然后,它会创建一个与新创建的 User 关联的 UserProfile。

问题是,您希望如何以及何时创建此 UserProfile 实例?

  1. 每当创建一个新的 User 时自动执行:使用信号,如所述in the docs
  2. 自动,每当从用户实例访问配置文件时:使用 AutoOneToOneField,并使用 user.userprofile 而不是 user.get_profile() 访问配置文件
  3. 手动。但不要忘记用户可能还没有关联 UserProfile,因此 user.get_profile() 可能会引发 DoesNotExist 异常。

When they do this, where does the related User get created?

事实并非如此。您必须明确地创建它。

This idea of asking somebody to sign up for an account, and then create a MyUserProfile with a form that asks for the password, username, email, et al, and then in my view creating 2 different objects (MyUserProfile and User) with different parts of the form data. I mean I shouldn't have a User form right?

为什么不呢?您想在这里一次性创建一个 User 和他的关联配置文件,对吗?您最终可以直接使用 POST 数据,或使用 Form 访问字段,或者更好的是,使用 2 个 ModelForm(一个用于User,一个用于 UserProfile),您将在同一 View 中处理(也许 this question 可以提供帮助?)

Maybe there is a good example of this sort of thing being done on some open source project.

我建议您查看 django-registration和 django-profiles .

注意事项

通过扩展模型本身,您还有另一种向 User 对象添加信息的方法。它将允许您将额外的字段直接放入用户模型中,并且可能更容易理解和使用。

这里就不细说了,看that tutorial了解更多信息。

其他问题

I tried to use AutoOneToOneField from django-annoying, but I have no idea where all the User's attributes get set or how to decide which User object to attach to it. This stuff is driving me crazy

请参阅上文了解如何使用它。如果你觉得不舒服,最好关注the documentation ,建议在用户配置文件中使用 ForeignKey 和 unique=True。

Also, do I need to use the sites app to do this stuff

来自site framework docs : 如果您的单个 Django 安装支持多个站点并且您需要以某种方式区分这些站点,请使用它。

and finally does a "super user" have all permissions to everything (I don't want people from Account "Acme" to access account "Microshaft" objects)?

同样,来自 docs , 指定此用户拥有所有权限而无需明确分配它们。这意味着 Django 无处不在使用 built-in permission system (例如默认管理页面), super 用户将被授权。

在 views 中,您自己编写,或者如果您调整一些 ModelAdmin,则由您决定如何检查权限。