privatethreads.py 8.5 KB

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