organisational.py 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # -*- coding: utf-8 -*-
  2. from tracim_backend.models import User
  3. from tracim_backend.models.data import UserRoleInWorkspace
  4. CALENDAR_PERMISSION_READ = 'r'
  5. CALENDAR_PERMISSION_WRITE = 'w'
  6. class Calendar(object):
  7. def __init__(self, related_object, path):
  8. self._related_object = related_object
  9. self._path = path
  10. @property
  11. def related_object(self):
  12. return self._related_object
  13. def user_can_read(self, user: User) -> bool:
  14. raise NotImplementedError()
  15. def user_can_write(self, user: User) -> bool:
  16. raise NotImplementedError()
  17. class UserCalendar(Calendar):
  18. def user_can_write(self, user: User) -> bool:
  19. return self._related_object.user_id == user.user_id
  20. def user_can_read(self, user: User) -> bool:
  21. return self._related_object.user_id == user.user_id
  22. class WorkspaceCalendar(Calendar):
  23. _workspace_rights = {
  24. UserRoleInWorkspace.NOT_APPLICABLE:
  25. [],
  26. UserRoleInWorkspace.READER:
  27. [CALENDAR_PERMISSION_READ],
  28. UserRoleInWorkspace.CONTRIBUTOR:
  29. [CALENDAR_PERMISSION_READ, CALENDAR_PERMISSION_WRITE],
  30. UserRoleInWorkspace.CONTENT_MANAGER:
  31. [CALENDAR_PERMISSION_READ, CALENDAR_PERMISSION_WRITE],
  32. UserRoleInWorkspace.WORKSPACE_MANAGER:
  33. [CALENDAR_PERMISSION_READ, CALENDAR_PERMISSION_WRITE],
  34. }
  35. def user_can_write(self, user: User) -> bool:
  36. role = user.get_role(self._related_object)
  37. return CALENDAR_PERMISSION_WRITE in self._workspace_rights[role]
  38. def user_can_read(self, user: User) -> bool:
  39. role = user.get_role(self._related_object)
  40. return CALENDAR_PERMISSION_READ in self._workspace_rights[role]