privatethreads.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. from django import forms
  2. from django.core.exceptions import PermissionDenied
  3. from django.http import Http404
  4. from django.utils.translation import ugettext_lazy as _
  5. from misago.acl import algebra
  6. from misago.acl.decorators import return_boolean
  7. from misago.acl.models import Role
  8. from misago.categories.models import PRIVATE_THREADS_ROOT, Category
  9. from misago.core.forms import YesNoSwitch
  10. from misago.threads.models import Thread
  11. __all__ = [
  12. 'allow_use_private_threads',
  13. 'can_use_private_threads',
  14. 'allow_see_private_thread',
  15. 'can_see_private_thread',
  16. 'allow_change_owner',
  17. 'can_change_owner',
  18. 'allow_add_participants',
  19. 'can_add_participants',
  20. 'allow_remove_participant',
  21. 'can_remove_participant',
  22. 'allow_add_participant',
  23. 'can_add_participant',
  24. 'allow_message_user',
  25. 'can_message_user',
  26. ]
  27. class PermissionsForm(forms.Form):
  28. legend = _("Private threads")
  29. can_use_private_threads = YesNoSwitch(label=_("Can use private threads"))
  30. can_start_private_threads = YesNoSwitch(label=_("Can start private threads"))
  31. max_private_thread_participants = forms.IntegerField(
  32. label=_("Max number of users invited to private thread"),
  33. help_text=_("Enter 0 to don't limit number of participants."),
  34. initial=3,
  35. min_value=0,
  36. )
  37. can_add_everyone_to_private_threads = YesNoSwitch(
  38. label=_("Can add everyone to threads"),
  39. help_text=_("Allows user to add users that are blocking him to private threads."),
  40. )
  41. can_report_private_threads = YesNoSwitch(
  42. label=_("Can report private threads"),
  43. help_text=_(
  44. "Allows user to report private threads they are "
  45. "participating in, making them accessible to moderators."
  46. ),
  47. )
  48. can_moderate_private_threads = YesNoSwitch(
  49. label=_("Can moderate private threads"),
  50. help_text=_(
  51. "Allows user to read, reply, edit and delete content "
  52. "in reported private threads."
  53. ),
  54. )
  55. def change_permissions_form(role):
  56. if isinstance(role, Role) and role.special_role != 'anonymous':
  57. return PermissionsForm
  58. else:
  59. return None
  60. def build_acl(acl, roles, key_name):
  61. new_acl = {
  62. 'can_use_private_threads': 0,
  63. 'can_start_private_threads': 0,
  64. 'max_private_thread_participants': 3,
  65. 'can_add_everyone_to_private_threads': 0,
  66. 'can_report_private_threads': 0,
  67. 'can_moderate_private_threads': 0,
  68. }
  69. new_acl.update(acl)
  70. algebra.sum_acls(
  71. new_acl,
  72. roles=roles,
  73. key=key_name,
  74. can_use_private_threads=algebra.greater,
  75. can_start_private_threads=algebra.greater,
  76. max_private_thread_participants=algebra.greater_or_zero,
  77. can_add_everyone_to_private_threads=algebra.greater,
  78. can_report_private_threads=algebra.greater,
  79. can_moderate_private_threads=algebra.greater
  80. )
  81. if not new_acl['can_use_private_threads']:
  82. new_acl['can_start_private_threads'] = 0
  83. return new_acl
  84. private_category = Category.objects.private_threads()
  85. new_acl['visible_categories'].append(private_category.pk)
  86. new_acl['browseable_categories'].append(private_category.pk)
  87. if new_acl['can_moderate_private_threads']:
  88. new_acl['can_see_reports'].append(private_category.pk)
  89. category_acl = {
  90. 'can_see': 1,
  91. 'can_browse': 1,
  92. 'can_see_all_threads': 1,
  93. 'can_see_own_threads': 0,
  94. 'can_start_threads': new_acl['can_start_private_threads'],
  95. 'can_reply_threads': 1,
  96. 'can_edit_threads': 1,
  97. 'can_edit_posts': 1,
  98. 'can_hide_own_threads': 0,
  99. 'can_hide_own_posts': 1,
  100. 'thread_edit_time': 0,
  101. 'post_edit_time': 0,
  102. 'can_hide_threads': 0,
  103. 'can_hide_posts': 0,
  104. 'can_protect_posts': 0,
  105. 'can_move_posts': 0,
  106. 'can_merge_posts': 0,
  107. 'can_pin_threads': 0,
  108. 'can_close_threads': 0,
  109. 'can_move_threads': 0,
  110. 'can_merge_threads': 0,
  111. 'can_approve_content': 0,
  112. 'can_report_content': new_acl['can_report_private_threads'],
  113. 'can_see_reports': 0,
  114. 'can_see_posts_likes': 0,
  115. 'can_like_posts': 0,
  116. 'can_hide_events': 0,
  117. }
  118. if new_acl['can_moderate_private_threads']:
  119. category_acl.update({
  120. 'can_edit_threads': 2,
  121. 'can_edit_posts': 2,
  122. 'can_hide_threads': 2,
  123. 'can_hide_posts': 2,
  124. 'can_protect_posts': 1,
  125. 'can_merge_posts': 1,
  126. 'can_see_reports': 1,
  127. 'can_close_threads': 1,
  128. 'can_hide_events': 2,
  129. })
  130. new_acl['categories'][private_category.pk] = category_acl
  131. return new_acl
  132. def add_acl_to_thread(user, thread):
  133. if thread.thread_type.root_name != PRIVATE_THREADS_ROOT:
  134. return
  135. if not hasattr(thread, 'participant'):
  136. thread.participants_list = []
  137. thread.participant = None
  138. thread.acl.update({
  139. 'can_start_poll': False,
  140. 'can_change_owner': can_change_owner(user, thread),
  141. 'can_add_participants': can_add_participants(user, thread),
  142. })
  143. def register_with(registry):
  144. registry.acl_annotator(Thread, add_acl_to_thread)
  145. def allow_use_private_threads(user):
  146. if user.is_anonymous:
  147. raise PermissionDenied(_("You have to sign in to use private threads."))
  148. if not user.acl_cache['can_use_private_threads']:
  149. raise PermissionDenied(_("You can't use private threads."))
  150. can_use_private_threads = return_boolean(allow_use_private_threads)
  151. def allow_see_private_thread(user, target):
  152. if user.acl_cache['can_moderate_private_threads']:
  153. can_see_reported = target.has_reported_posts
  154. else:
  155. can_see_reported = False
  156. can_see_participating = user in [p.user for p in target.participants_list]
  157. if not (can_see_participating or can_see_reported):
  158. raise Http404()
  159. can_see_private_thread = return_boolean(allow_see_private_thread)
  160. def allow_change_owner(user, target):
  161. is_moderator = user.acl_cache['can_moderate_private_threads']
  162. is_owner = target.participant and target.participant.is_owner
  163. if not (is_owner or is_moderator):
  164. raise PermissionDenied(_("Only thread owner and moderators can change threads owners."))
  165. if not is_moderator and target.is_closed:
  166. raise PermissionDenied(_("Only moderators can change closed threads owners."))
  167. can_change_owner = return_boolean(allow_change_owner)
  168. def allow_add_participants(user, target):
  169. is_moderator = user.acl_cache['can_moderate_private_threads']
  170. if not is_moderator:
  171. if not target.participant or not target.participant.is_owner:
  172. raise PermissionDenied(_("You have to be thread owner to add new participants to it."))
  173. if target.is_closed:
  174. raise PermissionDenied(_("Only moderators can add participants to closed threads."))
  175. can_add_participants = return_boolean(allow_add_participants)
  176. def allow_remove_participant(user, thread, target):
  177. if user.acl_cache['can_moderate_private_threads']:
  178. return
  179. if user == target:
  180. return # we can always remove ourselves
  181. if thread.is_closed:
  182. raise PermissionDenied(_("Only moderators can remove participants from closed threads."))
  183. if not thread.participant or not thread.participant.is_owner:
  184. raise PermissionDenied(_("You have to be thread owner to remove participants from it."))
  185. can_remove_participant = return_boolean(allow_remove_participant)
  186. def allow_add_participant(user, target):
  187. message_format = {'user': target.username}
  188. if not can_use_private_threads(target):
  189. raise PermissionDenied(
  190. _("%(user)s can't participate in private threads.") % message_format
  191. )
  192. if user.acl_cache['can_add_everyone_to_private_threads']:
  193. return
  194. if user.acl_cache['can_be_blocked'] and target.is_blocking(user):
  195. raise PermissionDenied(_("%(user)s is blocking you.") % message_format)
  196. if target.can_be_messaged_by_nobody:
  197. raise PermissionDenied(
  198. _("%(user)s is not allowing invitations to private threads.") % message_format
  199. )
  200. if target.can_be_messaged_by_followed and not target.is_following(user):
  201. message = _("%(user)s limits invitations to private threads to followed users.")
  202. raise PermissionDenied(message % message_format)
  203. can_add_participant = return_boolean(allow_add_participant)
  204. def allow_message_user(user, target):
  205. allow_use_private_threads(user)
  206. allow_add_participant(user, target)
  207. can_message_user = return_boolean(allow_message_user)