Advertisement
Guest User

Untitled

a guest
May 13th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. class UnauthorisedAccessError(Exception):
  2. """ User does not have access priviledges """
  3.  
  4.  
  5. @bp.app_errorhandler(UnauthorisedAccessError)
  6. def handle_login_error(e):
  7. """Redirect to the login page when LoginError is raised."""
  8.  
  9. flash("You do not have sufficient access rights to perform this action",
  10. 'error')
  11. return redirect(url_for('auth.login'))
  12.  
  13.  
  14. def check_user_group(required_groups):
  15. """
  16. Traverse list of required access groups to check for one or more matches
  17. """
  18. access = [current_user.has_access(PermissionGroups.query.filter_by(
  19. group_name=group
  20. ).first())
  21. for group in required_groups]
  22. if not any(access):
  23. raise UnauthorisedAccessError
  24.  
  25.  
  26. def group_required(*groups):
  27. """
  28. Decorate a function to require the user to have at least one of the groups.
  29. """
  30.  
  31. def decorator(func):
  32. @wraps(func)
  33. def check_auth(*args, **kwargs):
  34. check_user_group(*groups)
  35.  
  36. return func(*args, **kwargs)
  37.  
  38. return check_auth
  39.  
  40. return decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement