Няма описание

context_routes.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. from flask import Blueprint
  19. from flask import redirect
  20. from flask import request
  21. from flask_login import current_user
  22. from app import app
  23. from app import cache
  24. from app import db
  25. from app.datamgmt.context.context_db import ctx_search_user_cases
  26. from app.models.authorization import Permissions
  27. from app.models.cases import Cases
  28. from app.models.models import Client
  29. from app.blueprints.access_controls import ac_api_requires, not_authenticated_redirection_url
  30. from app.blueprints.responses import response_success
  31. context_rest_blueprint = Blueprint('context_rest', __name__)
  32. @context_rest_blueprint.route('/context/search-cases', methods=['GET'])
  33. @ac_api_requires()
  34. def cases_context_search():
  35. search = request.args.get('q')
  36. # Get all investigations not closed
  37. datao = ctx_search_user_cases(search, current_user.id, max_results=100)
  38. return response_success(data=datao)
  39. # TODO why is this route not prefixed with annotation @ac_api_requires?
  40. @context_rest_blueprint.route('/context/set', methods=['POST'])
  41. def set_ctx():
  42. """
  43. Set the context elements of a user i.e the current case
  44. :return: Page
  45. """
  46. if not current_user.is_authenticated:
  47. return redirect(not_authenticated_redirection_url(request.full_path))
  48. ctx = request.form.get('ctx')
  49. ctx_h = request.form.get('ctx_h')
  50. current_user.ctx_case = ctx
  51. current_user.ctx_human_case = ctx_h
  52. db.session.commit()
  53. _update_user_case_ctx()
  54. return response_success(msg="Saved")
  55. # TODO should move this method somewhere else, it is not a REST route
  56. @app.context_processor
  57. def iris_version():
  58. return dict(iris_version=app.config.get('IRIS_VERSION'),
  59. organisation_name=app.config.get('ORGANISATION_NAME'),
  60. std_permissions=Permissions,
  61. demo_domain=app.config.get('DEMO_DOMAIN', None))
  62. # TODO should move this method somewhere else, it is not a REST route
  63. @app.context_processor
  64. @cache.cached(timeout=3600, key_prefix='iris_has_updates')
  65. def has_updates():
  66. return dict(has_updates=False)
  67. def _update_user_case_ctx():
  68. """
  69. Retrieve a list of cases for the case selector
  70. :return:
  71. """
  72. # Get all investigations not closed
  73. res = Cases.query.with_entities(
  74. Cases.name,
  75. Client.name,
  76. Cases.case_id,
  77. Cases.close_date) \
  78. .join(Cases.client) \
  79. .order_by(Cases.open_date) \
  80. .all()
  81. data = [row for row in res]
  82. if current_user and current_user.ctx_case:
  83. # If the current user have a current case,
  84. # Look for it in the fresh list. If not
  85. # exists then remove from the user context
  86. is_found = False
  87. for row in data:
  88. if row[2] == current_user.ctx_case:
  89. is_found = True
  90. break
  91. if not is_found:
  92. # The case does not exist,
  93. # Removes it from the context
  94. current_user.ctx_case = None
  95. current_user.ctx_human_case = "Not set"
  96. db.session.commit()
  97. app.jinja_env.globals.update({
  98. 'cases_context_selector': data
  99. })
  100. return data