授予“用户更改”权限时,如何防止 Django 管理中的权限升级允许?
我有一个拥有大量客户群的 Django 网站。我想让我们的客户服务部门能够更改普通用户帐户,执行诸如更改密码、电子邮件地址等操作。但是,如果我授予某人内置的 auth |用户 |可以更改用户权限,他们能够在任何帐户(包括他们自己的帐户)上设置is_superuser
标志。 (!!!)
对于非超级用户员工删除此选项的最佳方法是什么?我确信它涉及子类化 django.contrib.auth.forms.UserChangeForm 并将其挂接到我已经自定义的 UserAdmin 对象中......以某种方式。但我找不到任何关于如何执行此操作的文档,而且我还不太了解内部结构。
I have a django site with a large customer base. I would like to give our customer service department the ability to alter normal user accounts, doing things like changing passwords, email addresses, etc. However, if I grant someone the built-in auth | user | Can change user
permission, they gain the ability to set the is_superuser
flag on any account, including their own. (!!!)
What's the best way to remove this option for non-superuser staff? I'm sure it involves subclassing django.contrib.auth.forms.UserChangeForm
and hooking it into my already-custom UserAdmin
object... somehow. But I can't find any documentation on how to do this, and I don't yet understand the internals well enough.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不仅如此,他们还获得了将任何权限一一赋予自己的能力,同样的效果……
好吧,不一定。您在 django 管理的更改页面中看到的表单是由管理应用程序动态创建的,并且基于
UserChangeForm
,但此类几乎没有向username
字段添加正则表达式验证。自定义
UserAdmin
是此处的方法。基本上,您希望将fieldsets
属性更改为类似的内容:但这里的问题是此限制将适用于所有用户。如果这不是您想要的,您可以覆盖
change_view
以根据用户的权限进行不同的行为。代码片段:Not only this, they also gain the ability to give themselves any permissions one-by-one, same effect...
Well, not necessarily. The form you see in the change page of django's admin is dynamically created by the admin application, and based on
UserChangeForm
, but this class barely adds regex validation to theusername
field.A custom
UserAdmin
is the way to go here. Basically, you want to change thefieldsets
property to something like that :But the problem here is that this restriction will apply to all users. If this is not what you want, you could for example override
change_view
to behave differently depending on the permission of the users. Code snippet :接受的答案的下面部分有一个竞争条件,如果两个员工用户尝试同时访问管理表单,其中一个可能会获得超级用户表单。
为了避免这种竞争条件(并且在我看来提高解决方案的整体质量),我们可以直接重写
get_fieldsets()
和get_readonly_fields()
方法:The below part of the accepted answer has a race condition where if two staff users try to access the admin form at the same time, one of them may get the superuser form.
To avoid this race condition (and in my opinion improve the overall quality of the solution), we can override the
get_fieldsets()
andget_readonly_fields()
methods directly:非常感谢克莱门特。当我对我的网站执行相同操作时,我想到的是,我还需要将所有字段设置为除您自己以外的用户只读。因此,根据克莱门特的回答,我添加了只读字段和密码字段,在查看非自身时隐藏
Great thanks to Clément. What I came up with when doing the same for my site is that I needed additionally to make all fields readonly for users you other than self. So basing on Clément's answer I addeed readonly fields and password field hiding when viewing not self
这种方法是根据网络上的一些有用提示组合而成的。在本例中,我们正在修改 UserAdmin,以便对于具有用户添加/更改权限的非超级用户员工,他们可以授予其他用户的唯一权限和组是该员工已经拥有的权限和组。
(对于 Django 1.11)
如果用户被授予更改组的权限,则同样应该对 GroupAdmin 执行此操作。
This approach was put together from several helpful tips on the web. In this case we are modifying UserAdmin so that, for non-superuser staff with user add/change permission, the only permissions and groups they can grant another user are the ones the staff member already has.
(for Django 1.11)
This should likewise be done for GroupAdmin if a user is given permission to change groups.
django 1.1的完整代码(仅限工作人员(非超级用户)的基本用户信息)
Full code for django 1.1 (limited to basic user information for staff (not superusers))