1. Django Built-in Permission Management
1.1 Permission Categories
- Permission
Used to define User A’s permission on Task. - User
If User A has permission on Model B, then User A has the corresponding permission on all instances in Model B. Theuser_permissionfield of the User object is used to manage the user’s permissions. Useassign_permto assign permissions to a User. - Group
If Group C has permission on Model B, then all Users belonging to Group C have the corresponding permission on Model B.
1.2 Configuration and Implementation
- Permission
After Django defines each model, by default it adds three permissions for that model: add, change, and delete. Custom permissions can be added manually when defining the model:
| |
Every permission is an instance of the django.contrib.auth.Permission type, which contains three fields: name, codename, and content_type. content_type reflects which model the permission belongs to; codename is like view_task above and is used when checking permissions in code logic; name is the description of the permission, and it is what is displayed by default when a permission is printed to the screen or a page.
Creating a custom permission in a model can be understood, from the perspective of system development, as creating a built-in permission of the system. If a requirement involves creating custom permissions while the user is using the system, then use the following approach:
| |
- User and Group
Django’s built-in permission authentication is bound into django.contrib.auth, and the Permission model in auth in turn depends on contenttypes.
| |
| |
The following tables are then created in the database:
auth_groupauth_group_permissionsauth_permissionauth_userauth_user_groupsauth_user_user_permissions
From the table names you can see that auth_user has two external relationships, groups and user_permissions.
| |
The command above lets the interpreter load the settings file of the Django project and enter a mode in which objects in the project can be operated on directly.
| |
The groups and user_permissions of every object have three methods for modifying permissions.
A.groups.[add|remove|clear()]A.user_permissions.[add|remove|clear()]
1.3 Usage
There are usually two ways to use it,
- Check by calling the has_perm() function inside a View function.
A/Group.has_perm(‘applabel.task’) is used to check a user’s/group’s permission. In addition, A.get_all_permissions() lists all of a user’s permissions, and A.get_group_permissions() lists the permissions of the groups the user belongs to. - Use a decorator before the View function.
@permission_required(‘applabel.task’)
2. Django-guardian
Take the common multi-author blog system as an example: each blog post is an object. The Django built-in permission control approach described above cannot achieve object-level control. Django does not provide object-level permission control, but it left an interface in its architecture. django-guardian is a very popular object-level permission control component. Object Permission is a permission mechanism at object granularity that allows authorization for each specific object. If the read/write permission on object b is granted to User A, then User A has read/write permission only on object b, and cannot operate on other objects of the same kind.
2.1 Configuration
| |
| |
The following tables are then created in the database:
- guardian_groupobjectpermission
- guardian_userobjectpermission
2.2 Usage
- Editing permissions
guardian.shortcuts.assign(perm, user_or_group, obj=None) add a permissionguardian.shortcuts.remove_perm(perm,user_or_group=None, obj=None) remove a permissionguardian.shortcuts.get_perms(user_or_group,obj) get all permissions
- perm: this parameter is a string representing a permission, and its format must be
app.perm_codenameorperm_codename. However, if the third parameter is None, it must be inapp.perm_codenameformat. It is therefore still recommended to use theapp.perm_codenameformat uniformly. Note that app is not the full path of the app, but the module name at the last level. This differs from the full app path inINSTALL_APP, so if your app module has more than one level, pay close attention here. user_or_group: this parameter is a User or Group type object.- obj: this parameter is the related object. This parameter can be omitted; if omitted, the Model permission is granted.
- Checking permissions
user.has_perm('app.view_task')#检测权限ObjectPermissionChecker(request.user).has_perm('app.view_task', task)guardian.decorators.permission_required()
