权限-Permissions
权限Permissions
权限控制可以限制用户对于视图的访问和对于具体数据对象的访问。
- 在执行视图的dispatch方法前,会先进行视图访问权限的判断
- 在通过get_object()获取具体对象时,会进行模型对象访问权限的判断
区分不同的用户访问不同 的接口
1、源码分析
#APIView----》dispatch-----》initial-----》self.check_permissions(reqeust)  (APIView的对象方法)
	    def check_permissions(self, request):
        #遍历权限对象列表得到一个权限对象 (权限器),进行权限认证
        for permission in self.get_permissions():#权限类的对象,放到列表中
            #权限类一定有一个has_permission权限方法,用来做权限认证的
            #参数:权限对象self、请求对象request、视图类对象
            #返回值:有权限返回True,无权限返回False,
            if not permission.has_permission(request, self):
                self.permission_denied(
                    request,
                    message=getattr(permission, 'message', None),
                    code=getattr(permission, 'code', None)
                )

2、自定义权限
#写一个类,继承BasePermission,重写has_permission,如果权限通过,就返回True,不通过返回False
from rest_framework.permissions import BasePermission
class UserPermission(BasePermission):
    def has_permission(self, request, view):
        # 不是超级用户不能访问,
        # 由于已经认证过了,所有request里有user对象,(当前登录的用户)
        user = request.user  # 当前登录用户
        print(user.get_user_type_display())
        # 该字段用来choices参数,通过get_字段名_display()就能取出choices后面的中文
        if user.user_type == 1:
            return True
        else:
            return False
局部使用
path('test/', views.TestView.as_view()),
path('test2/', views.Test2View.as_view()),
    
    
# 只有超级用户可以访问
from app01.app_auth import UserPermission
class TestView(APIView):
    authentication_classes = [MyAuthentication]
    permission_classes = [UserPermission]
    def get(self, request, *args, **kwargs):
        return Response('我是测试数据1111')
        # return Response(True)
# 只要登录用户就可以访问
class Test2View(APIView):
    authentication_classes = [MyAuthentication]
    def get(self, request, *args, **kwargs):
        return Response('我是测试数据2222')
只有登录后,才能认证,才能有权限

sb用户不是管理员用户,所有不能访问test路由

但是sb用户 可以访问test2路由,只要登录了就能访问

全局使用
#settings.py
REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': [
        "app01.app_auth.MyAuthentication"
    ],
    'DEFAULT_PERMISSION_CLASSES': [
        'app01.app_auth.UserPermission',
    ],
}
加了全局配置后,test2就不能访问了,也需要超级管理员才能访问,这首就需要局部禁用,加个空列表就可可以了
局部禁用
class Test2View(APIView):
    authentication_classes = [MyAuthentication]
    permission_classes = []
    def get(self, request, *args, **kwargs):
        return Response('我是测试数据2222')
3、内置权限
IsAdminUser使用
先创建超级用户 python manage.py createsuperuser
并登录。

#views.py
from rest_framework.permissions import IsAdminUser
from rest_framework.authentication import BasicAuthentication,SessionAuthentication
# 超级管理员可以查看
class Test3View(APIView):
    authentication_classes = [SessionAuthentication]
    permission_classes = [IsAdminUser]
    def get(self, request, *args, **kwargs):
        return Response('我是测试数据3333')
    
    
#urls.py
 path('test3/', views.Test3View.as_view()),
    
先登录到admin,再访问test3路由,就有quan'xia

本文来自博客园,作者:ExpiredSaury,转载请注明原文链接:https://www.cnblogs.com/saury/p/16976698.html
 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号