55 lines
1.5 KiB
Python
55 lines
1.5 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',
|
|
'exercise'
|
|
]
|
|
|
|
VistorPerms = [
|
|
'gen_monitest',
|
|
'questioncat_list',
|
|
'my_examtest',
|
|
'examtest_create'
|
|
]
|
|
|
|
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):
|
|
if request.user.workscope:
|
|
perms = ConsumerPerms
|
|
else:
|
|
perms = VistorPerms
|
|
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)or alias == '*':
|
|
return True
|
|
return False |