如何在django中创建组权限

时间:2013-09-14 02:08:07

标签: django django-models django-authentication

我正在尝试在auth_group_permission表中创建一行。

我尝试了以下内容:

group_permission = group_permissions.add(group=group, permission=permission)

group_permission = group.group_permissions_set.add(permission=permission)

group_permission = group.permissions_set.add(permission=permission)

这些都不起作用。有谁知道如何在此表中添加记录?

2 个答案:

答案 0 :(得分:23)

以下answer帮助我设置了小组。

from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType

content_type = ContentType.objects.get(app_label='myapp', model='BlogPost')
permission = Permission.objects.create(codename='can_publish',
                                       name='Can Publish Posts',
                                       content_type=content_type)
user = User.objects.get(username='duke_nukem')
group = Group.objects.get(name='wizard')
group.permissions.add(permission)
user.groups.add(group)

您可以通过shell python manage.py shell添加此权限,然后输入上面的代码。

答案 1 :(得分:0)

# Contain number of permission list in your system ex can_add_address, can_add_address
class Permissions(models.Model):
    permission_name = models.CharField(max_length=255, help_text="permission key")
    description = models.CharField(max_length=255, help_text="permission description")
    created_by = models.ForeignKey(User, db_index=True)
    created_on = models.DateTimeField(auto_now_add=True, help_text="Date when Role is added.")
    updated_on = models.DateTimeField(auto_now=True, help_text="Date when Role is modified.")
    enabled = models.BooleanField(default=True, help_text="Enable/Disable this object")

# create multiple group 
class RoleGroup(models.Model):
    name = models.CharField(max_length=255, help_text="Short title of Role")
    created_by = models.ForeignKey(User, db_index=True)
    created_on = models.DateTimeField(auto_now_add=True, help_text="Date when Role is added.")
    updated_on = models.DateTimeField(auto_now=True, help_text="Date when Role is modified.")
    enabled = models.BooleanField(default=True, help_text="Enable/Disable this object")

# add group permisson name with RoleGroup ex, HR name group cantaion multiple permissions
class RolePermission(models.Model):
    role_group = models.ForeignKey(RoleGroup, related_name='role_permission_group_name')
    permission_name = models.CharField(max_length=255, help_text="Short title of permission ex. can_add_address")

# Assign Multiple group to user 
class UserGroup(models.Model):
    user = models.ForeignKey(User, db_index=True, related_name='role_group')
    role_group = models.ForeignKey(RoleGroup, related_name='role_group_name', null = True, blank = True)
    created_by = models.ForeignKey(User, db_index=True)
    created_on = models.DateTimeField(auto_now_add=True, help_text="Date when Role is added.")
    updated_on = models.DateTimeField(auto_now=True, help_text="Date when Role is modified.")
    enabled = models.BooleanField(default=True, help_text="Enable/Disable this object")


#create has_permission static method in Util class
class Util :
@staticmethod
def has_permission(user, permission_name) :
    role_group = UserGroup.objects.filter(user = user).values_list('role_group_id', flat = True)
    permission_names = RolePermission.objects.filter(role_group_id__in = role_group).values_list('permission_name', flat = True)
    if permission_name in permission_names:
        return True
    return False



#Call Permisson Methods 
if Util.has_permission(request.user, 'can_edit_address') :
    // Not have permisson code  
else :
    // Have permisson code