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