暫無描述

permissions.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. import logging
  19. from uuid import uuid4
  20. from flask import session
  21. from flask_login import current_user
  22. from flask import request
  23. from app.blueprints.access_controls import get_case_access_from_api
  24. from app.iris_engine.access_control.utils import ac_get_effective_permissions_of_user
  25. from app.iris_engine.access_control.utils import ac_fast_check_current_user_has_case_access
  26. class PermissionDeniedError(Exception):
  27. pass
  28. def _deny_permission():
  29. error_uuid = uuid4()
  30. message = f'Permission denied (EID {error_uuid})'
  31. logging.warning(message)
  32. raise PermissionDeniedError(message)
  33. # When moving down permission checks from the REST layer into the business layer,
  34. # this method is used to replace manual calls to ac_fast_check_current_user_has_case_access
  35. def permissions_check_current_user_has_some_case_access(case_identifier, access_levels):
  36. if not ac_fast_check_current_user_has_case_access(case_identifier, access_levels):
  37. _deny_permission()
  38. # TODO: should remove this method and use permissions_check_current_user_has_some_case_access, only
  39. # I am pretty sure the access checks are done with the wrong case identifier for graphql
  40. # This one comes from ac_api_case_requires, whereas the other one comes from the way api_delete_case was written...
  41. # When moving down permission checks from the REST layer into the business layer,
  42. # this method is used to replace annotation ac_api_case_requires
  43. def permissions_check_current_user_has_some_case_access_stricter(access_levels):
  44. redir, caseid, has_access = get_case_access_from_api(request, access_levels)
  45. # TODO: do we really want to keep the details of the errors, when permission is denied => more work, more complex code?
  46. if not caseid or redir:
  47. _deny_permission()
  48. if not has_access:
  49. _deny_permission()
  50. # When moving down permission checks from the REST layer into the business layer,
  51. # this method is used to replace annotation ac_api_requires
  52. def permissions_check_current_user_has_some_permission(permissions):
  53. if 'permissions' not in session:
  54. session['permissions'] = ac_get_effective_permissions_of_user(current_user)
  55. for permission in permissions:
  56. if session['permissions'] & permission.value:
  57. return
  58. _deny_permission()