45 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
			
		
		
	
	
			45 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
| from rbac.permission import RbacPermission
 | |
| from rbac.models import UserProfile
 | |
| from crm.models import Consumer
 | |
| from rest_framework.permissions import IsAuthenticated
 | |
| 
 | |
| # 学员接口列表
 | |
| ConsumerPerms = [
 | |
|     'paper_list',
 | |
|     'gen_monitest',
 | |
|     'questioncat_list',
 | |
|     'my_collects',
 | |
|     'my_subjects',
 | |
|     'my_examtest',
 | |
|     'examtest_create',
 | |
|     'article_list'
 | |
| ]
 | |
| 
 | |
| class MyPermission(RbacPermission):
 | |
| 
 | |
|     def has_permission(self, request, view):
 | |
|         """
 | |
|         权限校验逻辑
 | |
|         :param request:
 | |
|         :param view:
 | |
|         :return:
 | |
|         """
 | |
|         perms = []
 | |
|         if 'perms' in request.session:
 | |
|             perms = request.session['perms']
 | |
|         elif isinstance(request.user,UserProfile): # 如果是管理员表
 | |
|             # perms = get_permission_list(request.user)
 | |
|             return True
 | |
|         elif isinstance(request.user,Consumer):
 | |
|             perms = ConsumerPerms
 | |
|         if perms:
 | |
|             if not hasattr(view, 'perms_map'):
 | |
|                 return True
 | |
|             else:
 | |
|                 perms_map = view.perms_map
 | |
|                 _method = request._request.method.lower()
 | |
|                 for i in perms_map:
 | |
|                     for method, alias in i.items():
 | |
|                         if (_method == method or method == '*') and alias in perms:
 | |
|                             return True
 | |
|         return False |