loginfo.py 871 B

123456789101112131415161718192021222324252627282930313233
  1. import re
  2. import jwt
  3. from django.http import HttpResponse
  4. from TestLaboratory.settings import SECRET_KEY
  5. from apps.user.models import User, Role
  6. class LogInfo(object):
  7. """使用类装饰器来权限校验"""
  8. def __init__(self, func):
  9. self.func = func
  10. def __call__(self, request, *args, **kwargs):
  11. token = request.META.get('HTTP_ACCESSTOKEN')
  12. SALT = SECRET_KEY
  13. payload = jwt.decode(token, SALT, True)
  14. user = User.objects.get(username=payload['username'])
  15. role = Role.objects.get(id="TestLaboratory_V1_Role_"+str(user.identify+1))
  16. permission = role.permissions.filter(method=request.method)
  17. log = None
  18. for per in permission:
  19. if re.search(per.url, request.path[:-1]):
  20. log = per
  21. break
  22. return self.func(request, *args, **kwargs)