decorators.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018 - Copyright Red Hat Inc
  4. Authors:
  5. Clement Verna <cverna@tutanota.com>
  6. """
  7. from __future__ import unicode_literals
  8. import flask
  9. from pagure.flask_app import admin_session_timedout
  10. from functools import wraps
  11. def has_issue_tracker(function):
  12. """
  13. Decorator that checks if the current pagure project has the
  14. issue tracker active
  15. If not active returns a 404 page
  16. """
  17. @wraps(function)
  18. def check_issue_tracker(*args, **kwargs):
  19. repo = flask.g.repo
  20. if not repo.settings.get("issue_tracker", True):
  21. flask.abort(404, "No issue tracker found for this project")
  22. # forbid all POST requests if the issue tracker is made read-only
  23. if flask.request.method == "POST" and repo.settings.get(
  24. "issue_tracker_read_only", False
  25. ):
  26. flask.abort(401, "The issue tracker for this project is read-only")
  27. return function(*args, **kwargs)
  28. return check_issue_tracker
  29. def has_trackers(function):
  30. """
  31. Decorator that checks if the current pagure project has the
  32. issue tracker active or has PRs function active
  33. If not active returns a 404 page
  34. """
  35. @wraps(function)
  36. def check_trackers(*args, **kwargs):
  37. repo = flask.g.repo
  38. if not repo.settings.get(
  39. "issue_tracker", True
  40. ) and not repo.settings.get("pull_requests", True):
  41. flask.abort(404, "No ticket trackers found for this project")
  42. return function(*args, **kwargs)
  43. return check_trackers
  44. def is_repo_admin(function):
  45. """
  46. Decorator that checks if the current user is the admin of
  47. the project.
  48. If not active returns a 403 page
  49. """
  50. @wraps(function)
  51. def check_repo_admin(*args, **kwargs):
  52. if not flask.g.repo_admin:
  53. flask.abort(
  54. 403,
  55. "You are not allowed to change the "
  56. "settings for this project",
  57. )
  58. return function(*args, **kwargs)
  59. return check_repo_admin
  60. def is_admin_sess_timedout(function):
  61. """
  62. Decorator that checks if the admin session has timeout.
  63. If not true redirect to the login page
  64. """
  65. @wraps(function)
  66. def check_session_timeout(*args, **kwargs):
  67. if admin_session_timedout():
  68. if flask.request.method == "POST":
  69. flask.flash("Action canceled, try it again", "error")
  70. return flask.redirect(
  71. flask.url_for("auth_login", next=flask.request.url)
  72. )
  73. return function(*args, **kwargs)
  74. return check_session_timeout