No Description

manage_access_control_db.py 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # IRIS Source Code
  2. # contact@dfir-iris.org
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU Lesser General Public
  6. # License as published by the Free Software Foundation; either
  7. # version 3 of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public License
  15. # along with this program; if not, write to the Free Software Foundation,
  16. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. from app import ac_current_user_has_permission
  18. from app.models.cases import Cases
  19. from app.models.authorization import Group
  20. from app.models.authorization import UserClient
  21. from app.models.authorization import Permissions
  22. from app.models.authorization import CaseAccessLevel
  23. from app.models.authorization import GroupCaseAccess
  24. from app.models.authorization import Organisation
  25. from app.models.authorization import OrganisationCaseAccess
  26. from app.models.authorization import User
  27. from app.models.authorization import UserCaseAccess
  28. from typing import Optional
  29. def manage_ac_audit_users_db():
  30. uca = UserCaseAccess.query.with_entities(
  31. User.name,
  32. User.user,
  33. User.id,
  34. User.uuid,
  35. UserCaseAccess.access_level,
  36. Cases.name,
  37. Cases.case_id
  38. ).join(
  39. UserCaseAccess.case
  40. ).join(
  41. UserCaseAccess.user
  42. ).all()
  43. gca = GroupCaseAccess.query.with_entities(
  44. Group.group_name,
  45. Group.group_id,
  46. Group.group_uuid,
  47. GroupCaseAccess.access_level,
  48. Cases.name,
  49. Cases.case_id
  50. ).join(
  51. GroupCaseAccess.case
  52. ).join(
  53. GroupCaseAccess.group
  54. ).all()
  55. oca = OrganisationCaseAccess.query.with_entities(
  56. Organisation.org_name,
  57. Organisation.org_id,
  58. Organisation.org_uuid,
  59. OrganisationCaseAccess.access_level,
  60. Cases.name,
  61. Cases.case_id
  62. ).all()
  63. ret = {
  64. 'users': [u._asdict() for u in uca],
  65. 'groups': [g._asdict() for g in gca],
  66. 'organisations': [o._asdict() for o in oca]
  67. }
  68. return ret
  69. def check_ua_case_client(user_id: int, case_id: int) -> Optional[UserClient]:
  70. """Check if the user has access to the case, through the customer of the case
  71. (in other words, check that the customer of the case is assigned to the user)
  72. Args:
  73. user_id (int): identifier of the user
  74. case_id (int): identifier of the case
  75. Returns:
  76. UserClient: the user relationship with the customer of the case, if it is assigned to the user
  77. None otherwise
  78. """
  79. if ac_current_user_has_permission(Permissions.server_administrator):
  80. # Return a dummy object
  81. uc = UserClient()
  82. uc.access_level = CaseAccessLevel.full_access.value
  83. return uc
  84. result = UserClient.query.filter(
  85. UserClient.user_id == user_id,
  86. Cases.case_id == case_id
  87. ).join(Cases,
  88. UserClient.client_id == Cases.client_id
  89. ).first()
  90. return result
  91. def get_client_users(client_id: int) -> list:
  92. """Get users for a client
  93. Args:
  94. client_id (int): Client ID
  95. Returns:
  96. list: List of users
  97. """
  98. result = UserClient.query.filter(
  99. UserClient.client_id == client_id
  100. ).all()
  101. return result
  102. def get_user_clients_id(user_id: int) -> list:
  103. """Get clients for a user
  104. Args:
  105. user_id (int): User ID
  106. Returns:
  107. list: List of clients
  108. """
  109. result = UserClient.query.filter(
  110. UserClient.user_id == user_id
  111. ).with_entities(
  112. UserClient.client_id
  113. ).all()
  114. return [r[0] for r in result]
  115. def user_has_client_access(user_id: int, client_id: int) -> bool:
  116. """Check if a user has access to a client
  117. Args:
  118. user_id (int): User ID
  119. client_id (int): Client ID
  120. Returns:
  121. bool: True if the user has access to the client
  122. """
  123. if ac_current_user_has_permission(Permissions.server_administrator):
  124. return True
  125. result = UserClient.query.filter(
  126. UserClient.user_id == user_id,
  127. UserClient.client_id == client_id
  128. ).first()
  129. return result is not None