userworkspace.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. import typing
  3. __author__ = 'damien'
  4. from sqlalchemy.orm import Session
  5. from tracim.models.auth import User
  6. from tracim.models.data import Workspace
  7. from tracim.models.data import UserRoleInWorkspace
  8. from tracim.models.data import RoleType
  9. class RoleApi(object):
  10. ALL_ROLE_VALUES = UserRoleInWorkspace.get_all_role_values()
  11. # Dict containing readable members roles for given role
  12. members_read_rights = {
  13. UserRoleInWorkspace.NOT_APPLICABLE: [],
  14. UserRoleInWorkspace.READER: [
  15. UserRoleInWorkspace.WORKSPACE_MANAGER,
  16. ],
  17. UserRoleInWorkspace.CONTRIBUTOR: [
  18. UserRoleInWorkspace.WORKSPACE_MANAGER,
  19. UserRoleInWorkspace.CONTENT_MANAGER,
  20. UserRoleInWorkspace.CONTRIBUTOR,
  21. ],
  22. UserRoleInWorkspace.CONTENT_MANAGER: [
  23. UserRoleInWorkspace.WORKSPACE_MANAGER,
  24. UserRoleInWorkspace.CONTENT_MANAGER,
  25. UserRoleInWorkspace.CONTRIBUTOR,
  26. UserRoleInWorkspace.READER,
  27. ],
  28. UserRoleInWorkspace.WORKSPACE_MANAGER: [
  29. UserRoleInWorkspace.WORKSPACE_MANAGER,
  30. UserRoleInWorkspace.CONTENT_MANAGER,
  31. UserRoleInWorkspace.CONTRIBUTOR,
  32. UserRoleInWorkspace.READER,
  33. ],
  34. }
  35. @classmethod
  36. def role_can_read_member_role(cls, reader_role: int, tested_role: int) \
  37. -> bool:
  38. """
  39. :param reader_role: role as viewer
  40. :param tested_role: role as viwed
  41. :return: True if given role can view member role in workspace.
  42. """
  43. if reader_role in cls.members_read_rights:
  44. return tested_role in cls.members_read_rights[reader_role]
  45. return False
  46. @classmethod
  47. def create_role(cls) -> UserRoleInWorkspace:
  48. role = UserRoleInWorkspace()
  49. return role
  50. def __init__(self, session: Session, current_user: typing.Optional[User]):
  51. self._session = session
  52. self._user = current_user
  53. def _get_one_rsc(self, user_id, workspace_id):
  54. """
  55. :param user_id:
  56. :param workspace_id:
  57. :return: a Query object, filtered query but without fetching the object.
  58. """
  59. return self._session.query(UserRoleInWorkspace).\
  60. filter(UserRoleInWorkspace.workspace_id == workspace_id).\
  61. filter(UserRoleInWorkspace.user_id == user_id)
  62. def get_one(self, user_id, workspace_id):
  63. return self._get_one_rsc(user_id, workspace_id).one()
  64. def create_one(
  65. self,
  66. user: User,
  67. workspace: Workspace,
  68. role_level: int,
  69. with_notif: bool,
  70. flush: bool=True
  71. ) -> UserRoleInWorkspace:
  72. role = self.create_role()
  73. role.user_id = user.user_id
  74. role.workspace = workspace
  75. role.role = role_level
  76. role.do_notify = with_notif
  77. if flush:
  78. self._session.flush()
  79. return role
  80. def delete_one(self, user_id, workspace_id, flush=True):
  81. self._get_one_rsc(user_id, workspace_id).delete()
  82. if flush:
  83. self._session.flush()
  84. def _get_all_for_user(self, user_id):
  85. return self._session.query(UserRoleInWorkspace)\
  86. .filter(UserRoleInWorkspace.user_id == user_id)
  87. def get_all_for_user(self, user_id):
  88. return self._get_all_for_user(user_id).all()
  89. def get_all_for_user_order_by_workspace(
  90. self,
  91. user_id: int
  92. ) -> UserRoleInWorkspace:
  93. return self._get_all_for_user(user_id)\
  94. .join(UserRoleInWorkspace.workspace).order_by(Workspace.label).all()
  95. def get_all_for_workspace(self, workspace_id):
  96. return self._session.query(UserRoleInWorkspace)\
  97. .filter(UserRoleInWorkspace.workspace_id == workspace_id).all()
  98. def save(self, role: UserRoleInWorkspace):
  99. self._session.flush()
  100. @classmethod
  101. def get_roles_for_select_field(cls):
  102. """
  103. :return: list of DictLikeClass instances representing available Roles
  104. (to be used in select fields)
  105. """
  106. result = list()
  107. for role_id in UserRoleInWorkspace.get_all_role_values():
  108. role = RoleType(role_id)
  109. result.append(role)
  110. return result