Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to create a group permission in django

I am attempting to create a row in the auth_group_permission table.

I have tried the following:

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)

None of these work. Does anyone know how to add a record to this table?

like image 633
Atma Avatar asked Sep 14 '13 02:09

Atma


People also ask

How do I add permissions in Django?

Just like when you were working with the built-in Django permissions, you can also add custom permissions to views by using the permission_required decorator or, in a class-based view, using the PermissionRequiredMixin . For function-based views: from django. contrib.


1 Answers

# 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  
like image 190
mukesh mali Avatar answered Sep 18 '22 07:09

mukesh mali