decorators.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from functools import wraps
  5. from .utils import json_response
  6. def permission_required_supper(view_func):
  7. @wraps(view_func)
  8. def wrapper(*args, **kwargs):
  9. request = None
  10. for item in args:
  11. if hasattr(item, 'user'):
  12. request = item
  13. break
  14. if request is None or not request.user.is_supper:
  15. return json_response(error='需要管理员权限')
  16. return view_func(*args, **kwargs)
  17. return wrapper
  18. def permission_required(perm_list):
  19. def decorate(view_func):
  20. codes = (perm_list,) if isinstance(perm_list, str) else perm_list
  21. @wraps(view_func)
  22. def wrapper(*args, **kwargs):
  23. request = None
  24. for item in args:
  25. if hasattr(item, 'user'):
  26. request = item
  27. break
  28. if request is None or (not request.user.is_supper and not request.user.has_perms(codes)):
  29. return json_response(error='拒绝访问')
  30. return view_func(*args, **kwargs)
  31. return wrapper
  32. return decorate