django users and rights management

Posted by brbsta on Fri, 20 Dec 2019 14:53:47 +0100

django stores the relationship between app and model by using the content U type table. In the permission table, name (name of permission, both in Chinese and English) content ﹣ type ﹣ ID (foreign key relationship with content ﹣ type, used to bind model) codename (permission name used when writing code) is set. How to customize permission?

1, Custom permissions

Instance code:

def add_permission(request)
        '''Custom create new permission'''
        # obtain model stay content_type object
        content_type = ContentType.objects.get_for_model(Article)    
        permission = Permission.objects.create(codename='black_article', name='Lahei article', content_type=content_type)
        return HttpResponse('Permission created successfully')

Or, when establishing the model, add:

    class Article(models.Model):
        title = models.CharField(max_length=64, verbose_name="title")
        content = models.TextField(verbose_name="content")

        class Meta:
            db_table = 'Article'
            permissions = [
                ('black_article', 'Lahei article')
        ]

2, User and permission binding

Permission itself is only a data, and must be bound with the User to play a role. The management between User model and permission can be managed in the following ways:

1. Mysuer.user'permissions.set (permission'list): directly give a list of permissions.
2. Mysuer. User "permissions. Add (permission, permission,...): add permissions one by one.
3. Mysuer. User "permissions. Remove (permission, permission,...): delete permissions one by one.
4. myuser.user_permissions.clear(): clear permissions.
5. Myuser. Get all permissions(): get all permissions.
6. Myser. Has ﹣ perm ('< app ﹣ name >. < codename >'): judge whether you have a certain permission. A string for permission parameter, the format is: app_name.codename
Instance code:

def user_add_permission(request):
        '''User permission instance'''
        # Get the currently logged in user object
        user = request.user
        # obtain app01 All permissions for
        content_type = ContentType.objects.get_for_model(app01)
        permissions = Permission.objects.filter(content_type=content_type)
        # Add for current user app01 All permissions for
        # One way
        user.user_permissions.set(permissions)
        # Mode 2
        user.user_permissions.add(permissions[1], permissions[2])
        user.save()
        # Or use this principle:*[1,2,3] = 1,2,3 Add
        user.user_permissions.add(*permissions)
        user.save()
        # View all permissions of the current user
        print(user.get_all_permissions())
        # Delete all permissions of the current user
        user.user_permissions.clear()
        return HttpResponse('Inspection authority')

3, Group operation

1. Group. Object. Create (group? Name): create a group
2. group.permissions: permissions on a group. Many to many relationship.
1> Group.permissions.add: add permissions
2> Group.permissions.remove: remove permissions
3> Group.permissions.clear: clear all permissions
4> User. Get group? Permissions(): get the permissions of the group to which the user belongs.
3. user.groups: all groups on a user. Many to many relationship.

The sample code is as follows:

def operate_group(request):
        group = Group.object.create(name='edit')  # Create and edit this group
        content_type = ContentType.object.get_for_model(Article) #
        permissions = Permission.object.filter(content_type=content_type) # obtain Article All permissions for table creation
        # add permission
        group.permissions.set(permissions) # Method 1: add all permissions directly
        group.permissions.add(*permissions) # Method 2: add all permissions
        group.permissions.add([permissions[0],permissions[1]) # Add specified permission
        group.save()
        # Add users to groups
        group = Group.object.filter(name='edit').first()
        user = User.object.first()
        user.group.add(group)
        user.save()
        return HttpResponse('success')

4, Use rights management in templates:

Under settings.templates.options.context'processors, because the django.contrib.auth.context'processors.auth context processor is added, all permissions of users can be obtained directly through perms in the template. The sample code is as follows:

{% if perms. Model name. Permission name%} / / if you have the permission name, you will see the following link
        < a href = '/ article /' > Article</a>
{% endif %}

Topics: Python Django