他们可以在任何帐户上设置 is_superuser 标志,包括他们自己的。 (!!!)
不仅如此,他们还获得了一个接一个地给自己任何权限的能力,同样的效果......
我确定它涉及子类化 django.contrib.auth.forms.UserChangeForm
嗯,不一定。你在 django 的 admin 的 change page 中看到的表单是由 admin 应用程序动态创建的,并且基于 UserChangeForm,但是这个类几乎没有在 username 字段中添加正则表达式验证。
并将其连接到我已经自定义的 UserAdmin 对象中......
自定义UserAdmin 是前往此处的方式。基本上,您想将fieldsets 属性更改为类似的内容:
class MyUserAdmin(UserAdmin):
fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
# Removing the permission part
# (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}),
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
# Keeping the group parts? Ok, but they shouldn't be able to define
# their own groups, up to you...
(_('Groups'), {'fields': ('groups',)}),
)
但这里的问题是这个限制将适用于所有用户。如果这不是您想要的,您可以例如覆盖 change_view 以根据用户的权限采取不同的行为。代码sn-p:
class MyUserAdmin(UserAdmin):
staff_fieldsets = (
(None, {'fields': ('username', 'password')}),
(_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
# No permissions
(_('Important dates'), {'fields': ('last_login', 'date_joined')}),
(_('Groups'), {'fields': ('groups',)}),
)
def change_view(self, request, *args, **kwargs):
# for non-superuser
if not request.user.is_superuser:
try:
self.fieldsets = self.staff_fieldsets
response = super(MyUserAdmin, self).change_view(request, *args, **kwargs)
finally:
# Reset fieldsets to its original value
self.fieldsets = UserAdmin.fieldsets
return response
else:
return super(MyUserAdmin, self).change_view(request, *args, **kwargs)