Django检查并设置用户组的权限

时间:2022-10-13 16:47:59

I have a usergroup called baseusers with a set of permissions I have added via admin. If baseusers doesn't exist I want to create it and set the same permissions it has now. I can create the group but how do I find out the permissions from shell and then add them in my code? For example two of the permissions are named can_fm_list and can_fm_add. Application is called fileman. But do I need to use id:s instead? Here is the code I would like the permissions to be set.

我有一个名为baseusers的用户组,其中包含我通过管理员添加的一组权限。如果baseusers不存在,我想创建它并设置它现在拥有的相同权限。我可以创建组但​​是如何从shell中找到权限然后将其添加到我的代码中?例如,两个权限名为can_fm_list和can_fm_add。应用程序称为fileman。但我需要使用id:s吗?这是我想要设置权限的代码。

userprofile/views.py

USERPROFILE / views.py

from fileman.models import Setting (Has the permissions)
from fileman.models import Newuserpath
from django.contrib.auth.models import Group

def register(request):

...

            if Group.objects.count() > 0:
            newuser.groups.add(1)
            else:
            newgroup = Group(name='baseusers')
            newgroup.save()
            newuser.groups.add(1)

fileman/models.py

FILEMAN / models.py

class Setting(models.Model):
    owner = AutoForeignKey(User, unique=True, related_name='fileman_Setting')
    root = models.CharField(max_length=250, null=True)
    home = models.CharField(max_length=250, null=True)
    buffer = models.TextField(blank=True)
    class Meta:
        permissions = (
            ("can_fm_list", _("Can look files list")),
            ("can_fm_add", _("Can upload files")),
            ("can_fm_rename", _("Can rename files")),
            ("can_fm_del", _("Can move files to basket")),
            ("can_fm_destruct", _("Can delete files")),
        )
    def __unicode__(self):
        return unicode(self.owner)
    def __init__(self, *args, **kwargs):
        super(Setting, self).__init__(*args, **kwargs)
        if not self.root:
            self.root = None
            self.home = None
    def writeBuffer(self, data):
        self.buffer = data
        self.save()

1 个解决方案

#1


12  

Take a look at this blog post: Django: Using The Permission System

看一下这篇博文:Django:使用权限系统

Adapted to your example:

适应你的例子:

can_fm_list = Permission.objects.get(name='can_fm_list')
newgroup.permissions.add(can_fm_list)

#1


12  

Take a look at this blog post: Django: Using The Permission System

看一下这篇博文:Django:使用权限系统

Adapted to your example:

适应你的例子:

can_fm_list = Permission.objects.get(name='can_fm_list')
newgroup.permissions.add(can_fm_list)