decorators.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2018-2019 - Copyright Red Hat Inc
  4. Authors:
  5. Clement Verna <cverna@tutanota.com>
  6. Pierre-Yves Chibon <pingou@pingoured.fr>
  7. """
  8. from __future__ import absolute_import, unicode_literals
  9. from functools import wraps
  10. import flask
  11. from pagure.flask_app import admin_session_timedout
  12. def has_issue_tracker(function):
  13. """
  14. Decorator that checks if the current pagure project has the
  15. issue tracker active
  16. If not active returns a 404 page
  17. """
  18. @wraps(function)
  19. def check_issue_tracker(*args, **kwargs):
  20. repo = flask.g.repo
  21. if not flask.g.issues_enabled or not repo.settings.get(
  22. "issue_tracker", True
  23. ):
  24. flask.abort(
  25. 404, description="No issue tracker found for this project"
  26. )
  27. # forbid all POST requests if the issue tracker is made read-only
  28. if flask.request.method == "POST" and repo.settings.get(
  29. "issue_tracker_read_only", False
  30. ):
  31. flask.abort(
  32. 401,
  33. description="The issue tracker for this project is read-only",
  34. )
  35. return function(*args, **kwargs)
  36. return check_issue_tracker
  37. def has_pr_enabled(function):
  38. """
  39. Decorator that checks if the current pagure project has the
  40. issue tracker active or has PRs function active
  41. If not active returns a 404 page
  42. """
  43. @wraps(function)
  44. def check_trackers(*args, **kwargs):
  45. repo = flask.g.repo
  46. if not repo.settings.get("pull_requests", True):
  47. flask.abort(
  48. 404,
  49. description="Pull Requests are not enabled on this project",
  50. )
  51. return function(*args, **kwargs)
  52. return check_trackers
  53. def has_issue_or_pr_enabled(function):
  54. """
  55. Decorator that checks if the current pagure project has either their
  56. issue tracker or their PR active. If both of them are disabled, it
  57. returns a 404 page.
  58. """
  59. @wraps(function)
  60. def check_issue_pr_trackers(*args, **kwargs):
  61. repo = flask.g.repo
  62. issue_enabled = flask.g.issues_enabled
  63. issue_ro = repo.settings.get("issue_tracker_read_only", False)
  64. pr_enabled = repo.settings.get("pull_requests", True)
  65. if not issue_enabled and not pr_enabled:
  66. flask.abort(
  67. 404,
  68. description="Issue tracker and Pull-Request disabled for "
  69. "this project",
  70. )
  71. elif flask.request.method == "POST" and not pr_enabled and issue_ro:
  72. flask.abort(
  73. 401,
  74. description="The issue tracker for this project is read-only",
  75. )
  76. return function(*args, **kwargs)
  77. return check_issue_pr_trackers
  78. def is_repo_admin(function):
  79. """
  80. Decorator that checks if the current user is the admin of
  81. the project.
  82. If not active returns a 403 page
  83. """
  84. @wraps(function)
  85. def check_repo_admin(*args, **kwargs):
  86. if not flask.g.repo_admin:
  87. flask.abort(
  88. 403,
  89. description="You are not allowed to change the "
  90. "settings for this project",
  91. )
  92. return function(*args, **kwargs)
  93. return check_repo_admin
  94. def is_admin_sess_timedout(function):
  95. """
  96. Decorator that checks if the admin session has timeout.
  97. If not true redirect to the login page
  98. """
  99. @wraps(function)
  100. def check_session_timeout(*args, **kwargs):
  101. if admin_session_timedout():
  102. if flask.request.method == "POST":
  103. flask.flash("Action canceled, try it again", "error")
  104. return flask.redirect(
  105. flask.url_for("auth_login", next=flask.request.url)
  106. )
  107. return function(*args, **kwargs)
  108. return check_session_timeout