django - 为什么 Django 使用 is_staff 和 is_superuser 而不是将它们作为权限?

标签 django authentication

我已经为此纠结了几天了。 django.contrib.auth.models 有一个允许多个权限的用户模型,但有 is_staff、is_superuser 和 is_active 的 3 个标志。

is_staff = models.BooleanField(_('staff status'), default=False, help_text=_('Designates whether the user can log into this admin site.'))
is_active = models.BooleanField(_('active'), default=True, help_text=_('Designates whether this user should be treated as active. Unselect this instead of deleting accounts.'))
is_superuser = models.BooleanField(_('superuser status'), default=False, help_text=_('Designates that this user has all permissions without explicitly assigning them.'))
groups = models.ManyToManyField(Group, verbose_name=_('groups'), blank=True, help_text=_('The groups this user belongs to. A user will get all permissions granted to each of his/her group.'))
user_permissions = models.ManyToManyField(Permission, verbose_name=_('user permissions'), blank=True, help_text='Specific permissions for this user.')

我无法理解的是为什么 is_staff 和 is_superuser 不是权限,您可以使用大约相同数量的代码以这种方式实现它,但出于某种原因,他们选择将这两个字段移动到模型上的 bool 值。

我问的部分原因是因为我有一个类似的情况,可以使用第三个 bool 值(或者我可以在获得许可的情况下这样做),我试图理解为什么他们会将这两个与权限分开?

我想我理解 is_active,它允许您在某人返回时将其锁定而无需“修复”他们的权限(尽管您可以拥有 is_locked_out “权限”,但这听起来是错误的。)

最佳答案

我要猜测性能。检查 bool 字段比从另一个表中获取与用户相关的权限或执行 sql 连接要便宜得多。考虑到在某些情况下,每个请求都会检查用户授权状态,将 bool 值放在用户模型上并检查它们更明智(尽管不是那么干净)。

关于django - 为什么 Django 使用 is_staff 和 is_superuser 而不是将它们作为权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17351468/

相关文章:

python - botocore.exceptions.ClientError : An error occurred (AccessDenied) when calling the PutObject operation: Access Denied

python - 获取 http 响应的链接在 django 中只工作一次

mysql - Django 与 MySQL 和 UTF-8

python - 在 Jinja2 模板中使用 DateTimeFields 显示本地时间 - Django

Django多语言网站: how to?

c# - 首先使用ef核心代码将密码存储在sql server数据库中

svn - SVN : Could not authenticate to server: rejected Basic challenge?是什么原因及解决办法

facebook - 使用 instagram API 对用户进行身份验证时,如何将 facebook 身份验证添加到登录页面?

java - Apache Shiro 是我的案例的正确选择吗?

java - JBoss JAAS 自定义登录模块