0

我希望能够创建一个用户(在基于django的openwisp2中),他可以创建另一个用户,但不能为该用户授予个人权限。这个新用户只能授予预定义的组权限。如何在django中创建一个可以创建另一个用户但不授予权限的用户(只授予预定义的组权限)?

当我向用户授予用户添加权限时,我看到该用户默认获得了“权限添加”选项(事实上我没有授予此用户的“权限添加”权限)。我观察到,这位新用户有幸创建了一个超级用户(这非常令人惊讶)

回答

0

不幸的是,由于默认的django用户&权限如何实现OpenWISP 2的开箱即用系统工作。

一旦用户有权限添加和更改用户的详细信息,他也将能够添加/删除其他用户的超级用户标志。 因此,该权限只应授予受信任的用户。

为了实现你想要的,你需要改变UserAdmin class of the openwisp-users module

我尝试了这些变化,这似乎工作得很好:

class UserAdmin(BaseUserAdmin, BaseAdmin): 
    # ... omitting existing code for brevity ... 

    def get_readonly_fields(self, request, obj=None): 
     # retrieve readonly fields 
     fields = super(UserAdmin, self).get_readonly_fields(request, obj) 
     # do not allow operators to set the is_superuser flag 
     if not request.user.is_superuser: 
      fields += fields[:] + ['is_superuser'] # copy to avoid modifying reference 
     return fields 

    def has_change_permission(self, request, obj=None): 
     # do not allow operators to edit details of superusers 
     # returns 403 if trying to access the change form of a superuser 
     if obj and obj.is_superuser and not request.user.is_superuser: 
      return False 
     return super(UserAdmin, self).has_change_permission(request, obj) 

    def get_queryset(self, request): 
     qs = super(UserAdmin, self).get_queryset(request) 
     # hide superusers from operators (they can't edit their details) 
     if not request.user.is_superuser: 
      qs = qs.filter(is_superuser=False) 
     return qs 

这些变化实现以下3两件事:

  • 使is_superuser场只读非超级用户(也称为运营商)
  • 将超级用户隐藏到用户列表中的非超级用户
  • 明确禁止将超级用户的详细信息更改为非超级用户(又名运营商)

这些变化可以集成在openwisp2以及。如果可以的话,享受并尝试贡献(例如:在openwisp-users中打开问题或提出请求)!

PS:I've included this feature (plus tests and improvements) in the openwisp-users module