Aucune description

states.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  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 datetime import datetime
  19. from flask_login import current_user
  20. from sqlalchemy import and_
  21. from app import db
  22. from app.models.models import ObjectState
  23. def _update_object_state(object_name, caseid, userid=None) -> ObjectState:
  24. """
  25. Expects a db commit soon after
  26. Args:
  27. object_name: name of the object to update
  28. caseid: case id
  29. userid: user id
  30. Returns:
  31. ObjectState object
  32. """
  33. if not userid:
  34. userid = current_user.id
  35. os = ObjectState.query.filter(and_(
  36. ObjectState.object_name == object_name,
  37. ObjectState.object_case_id == caseid
  38. )).first()
  39. if os:
  40. os.object_last_update = datetime.utcnow()
  41. os.object_state = os.object_state + 1
  42. os.object_updated_by_id = userid
  43. os.object_case_id = caseid
  44. else:
  45. os = ObjectState()
  46. os.object_name = object_name
  47. os.object_state = 0
  48. os.object_last_update = datetime.utcnow()
  49. os.object_updated_by_id = userid
  50. os.object_case_id = caseid
  51. db.session.add(os)
  52. return os
  53. def get_object_state(object_name, caseid):
  54. os = ObjectState.query.with_entities(
  55. ObjectState.object_state,
  56. ObjectState.object_last_update
  57. ).filter(and_(
  58. ObjectState.object_name == object_name,
  59. ObjectState.object_case_id == caseid
  60. )).first()
  61. if os:
  62. return os._asdict()
  63. else:
  64. return None
  65. def delete_case_states(caseid):
  66. ObjectState.query.filter(
  67. ObjectState.object_case_id == caseid
  68. ).delete()
  69. def update_timeline_state(caseid, userid=None):
  70. return _update_object_state('timeline', caseid=caseid, userid=userid)
  71. def get_timeline_state(caseid):
  72. return get_object_state('timeline', caseid=caseid)
  73. def update_tasks_state(caseid, userid=None):
  74. return _update_object_state('tasks', caseid=caseid, userid=userid)
  75. def get_tasks_state(caseid):
  76. return get_object_state('tasks', caseid=caseid)
  77. def update_evidences_state(caseid, userid=None):
  78. return _update_object_state('evidences', caseid=caseid, userid=userid)
  79. def get_evidences_state(caseid):
  80. return get_object_state('evidences', caseid=caseid)
  81. def update_ioc_state(caseid, userid=None):
  82. return _update_object_state('ioc', caseid, userid=userid)
  83. def get_ioc_state(caseid):
  84. return get_object_state('ioc', caseid=caseid)
  85. def update_assets_state(caseid, userid=None):
  86. return _update_object_state('assets', caseid=caseid, userid=userid)
  87. def get_assets_state(caseid):
  88. return get_object_state('assets', caseid=caseid)
  89. def update_notes_state(caseid, userid=None):
  90. return _update_object_state('notes', caseid=caseid, userid=userid)
  91. def get_notes_state(caseid):
  92. return get_object_state('notes', caseid=caseid)