2014-01-10 19 views
1

我可以以編程方式爲整個應用程序提供組的所有權限嗎?
當然,我可以給該組全部添加,更改,刪除應用程序中每個特定模型的權限。但後來,如果我需要添加另一個模型,我需要更改代碼,我也授予了權限。
因此,我需要找到一種可能性,即將所有模型都授予應用程序中的所有模型,而不必知道它們的名稱。
Django文檔不幫我。整個應用程序的Django組權限

編輯
爲了給你更多的細節:我sublcassed的RemoteUserBackend和推翻的configure_user方法將一個新的用戶添加到特定的組。如果沒有創建這個羣體,我創建它,並希望給予其必要的權限:

class MyRemoteUserBackend(RemoteUserBackend): 

    def configure_user(self, user): 
     group, created = Group.objects.get_or_create(name="mygroup") 
     if created: 
      pass 
      # Give permissions here 

     user.groups.add(group) 

     group.save() 
+0

我不認爲有使用默認的許可制度的方式 – yuvi

+0

即進入我分的唯一的事d是編寫一箇中間件,檢查請求路徑是否與應用程序匹配,然後檢查用戶權限。然而,這是一個相當模糊的方式。 – schneck

+0

也許有辦法得到模塊的所有類名?那麼我可以做一些像'在模塊中的classname:classname.addpermission ...' – 0xAffe

回答

0

你可以使超級用戶組的用戶,那麼當你檢查權限,做到:

if user.is_superuser: 
    #allow 
else: 
    #check permissions 
+0

不......我真的不想讓我的用戶成爲超級用戶。如果我這樣做了,我將不再需要許可系統 – 0xAffe

2

這是管理命令的速寫是同步的應用模式與權限組:

# coding=utf-8 

from django.core.management.base import BaseCommand, CommandError 
from django.contrib.contenttypes.models import ContentType 
from django.contrib.auth.models import Permission, Group 

class Command(BaseCommand): 
    args = '<group_name app_label>' 
    help = ('Syncs permissions of group with given name with permissions 
      of all models of app with giver app_label ') 

    def handle(self, *args, **options): 
     group_name= args[0] 
     app_label = args[1] 

     group = Group.objects.get(name=group_name) 
     cts = ContentType.objects.filter(app_label=app_label) 
     perms = Permission.objects.filter(content_type__in=cts) 
     group.permissions.add(*perms) 
+0

我不會在這裏仍然有問題來獲取模塊中的所有模型嗎? – 0xAffe

+0

沒有看到任何問題 - 'app_label'識別某個應用程序,此應用程序的所有模型都在DB中具有相應的ContentType對象,因此獲取這些內容類型的所有權限意味着獲取應用程序的所有模型的所有權限。 –

+0

啊好的。我沒有聽說過ContentType ...我會閱讀它 – 0xAffe