整个应用程序的Django组权限 [英] Django group permissions for whole app
问题描述
我可以通过编程方式为群组授予整个应用程序的所有权限吗?
当然,我可以给组中应用程序中每个特定模型的所有添加,更改,删除权限.但是稍后,如果需要添加其他模型,则需要更改代码,并在其中授予权限.
因此,我需要找到一种可能性,即在不知道其名称的情况下,也可以向应用程序内的所有模型授予所有权限.
Django文档对此无济于事.
Can I give a group all permissions for a whole application programmatically?
Of course I could give the group all add,change,delete permissions for every specific model in the application. But later, if I need to add another model, I need to change the code, where I give the permissions, too.
So I need find a possibility to grant all permissions too all models inside an app, without knowing their names.
The Django documentation doesn't help me with that.
修改
为您提供更多详细信息:我降低了RemoteUserBackend的覆盖范围,并覆盖了configure_user方法以将新用户添加到特定组.如果未创建此组,那么我将创建它并希望为其授予必要的权限:
Edit
To give you a bit more details: I sublcassed the RemoteUserBackend and overrode the configure_user method to add a new user to a specific group. If this group isn't created, I create it and want to give it the necessary permissions:
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()
推荐答案
这是管理命令的快速草图,该命令将应用程序模型的权限与组同步:
This is quick sketch of management command that syncs app models permissions with group:
# 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)
这篇关于整个应用程序的Django组权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!