如何扩展 Django 用户模型来管理权限

发布于 2024-12-12 20:59:52 字数 585 浏览 2 评论 0原文

我正在使用 Django 1.3 和 Python2.6 开发一个网络应用程序。我必须扩展 Django 用户模型,以便存在三种类型的用户并管理每种类型的权限。

为了说明这一点,假设存在三种类型的用户:教师、助教和学生。助教将能够创建新的“作业”,教师将能够“审查”并确认它,学生“提交”解决方案,教师将必须“审查”这些解决方案,而助教最终可以检查这些“审查”的解决方案。现在存在三种类型的用户:可以创建作业并对解决方案评分的助教、可以查看作业和解决方案以确认它们的教员以及学生 code> 谁可以提交解决方案。

现在,我明白有两种方法可以扩展用户模型。我可以创建一个 UserProfile 并添加一个名为“user_type”的字段。另一种方法是对 User 模型进行子类化。我认为子分类是一种更好的方法,因为有些字段因不同类型的用户而异。

我将有一个登录表单,但可以为不同类型的用户使用不同的注册表单。您为什么选择这两种方法中的任何一种?

据我所知,管理不同类型用户权限的最佳方法是通过用户组。如何创建组,检查用户是否属于某个组?选择如何扩展用户模型会影响我管理权限的方式吗?

I am working on a web-app using Django 1.3 and Python2.6. I have to extend the Django User model such that there are three types of users and manage permissions for each type.

To elucidate, say there are three types of Users: Faculty, TAs and Students. TAs will be able to create a new 'Assignment', Faculty will be able to 'review' and confirm it, Students 'submit' Solutions, which Faculty will have to 'review' and TAs finally can check those 'reviewed' Solutions. Now there are three types of users: TAs who can create Assigments and grade Solutions, Faculty who can review Assignments and Solutions to confirm them, and Students who can submit Solutions.

Now, I understand that there are two ways to extend a User model. I can create a UserProfile and add a field called 'user_type'. Other way is to subclass the User model. I think sub-classing is a better approach coz there are fields which vary for different type of users.

I will have a single login form, but can have different registration forms for different types of users. Why would you choose either of the approaches?

I understand that the best way to manage permissions for different types of users is through User Groups. How to create groups, check if the user belongs to a group? Does the choice of how to extend the User model effect the way I manage permissions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清欢 2024-12-19 20:59:52

如果创建子类,主类会自动实例化,所以更方便。如果您创建一个配置文件类,则需要手动实例化这两个类。

对于简单的情况,您可能只需要在允许操作之前检查用户类型。您可以使用内置的 Django 权限后端

无论您创建子类还是创建概要文件类,都不会影响组工作流程。组和用户之间始终存在多对多关系。但是,最好在 User 类和 Group 类之间进行设置,而不是在 UserProfile 类和 Group 类之间进行设置。

如果您计划一个更大的项目,我可以简要描述权限框架必须定义的内容:

  • 主体(尝试创建、访问或编辑某些内容的用户或组)
  • 对象(操作的目标)
  • 所有者(所有者)对象)
  • 角色(主体与对象或对象所有者之间的关系)
  • 权限(定义允许哪些角色执行哪些操作的规则)

If you create a subclass, the main class will be instantiated automatically, so it's more convenient. If you make a profile class, you'll need to instantiate manually both of the classes.

For simple cases you might need to just check the user type before allowing actions. You may use the buildt-in permissions backends of Django.

Whether you subclass or make a profile class, does not affect much the groups workflow. There will always be a ManyToMany relation between groups and users. But it's better to make it between User and Group classes than, say, between UserProfile and Group.

If you plan a bigger project, I can give a brief description of what a permission framework has to define:

  • subject (the user or group trying to create, access or edit something)
  • object (the target of the action)
  • owner (the owner of the object)
  • role (the relationship between the subject and the object or the object's owner)
  • permission (the rule defining what roles are allowed to do what actions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文