privatethreads.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from django.contrib.auth import get_user_model
  2. from django.core.exceptions import PermissionDenied
  3. from django.db.models import Q
  4. from django.utils.translation import ugettext_lazy as _
  5. from misago.acl import add_acl, algebra
  6. from misago.acl.decorators import return_boolean
  7. from misago.acl.models import Role
  8. from misago.core import forms
  9. from misago.forums.models import Forum
  10. __all__ = [
  11. 'allow_use_private_threads',
  12. 'can_use_private_threads',
  13. 'allow_see_private_thread',
  14. 'can_see_private_thread',
  15. 'allow_see_private_post',
  16. 'can_see_private_post',
  17. 'allow_message_user',
  18. 'can_message_user',
  19. 'exclude_invisible_private_threads',
  20. ]
  21. """
  22. Admin Permissions Form
  23. """
  24. class PermissionsForm(forms.Form):
  25. legend = _("Private threads")
  26. can_use_private_threads = forms.YesNoSwitch(
  27. label=_("Can use private threads"))
  28. can_start_private_threads = forms.YesNoSwitch(
  29. label=_("Can start private threads"))
  30. max_private_thread_participants = forms.IntegerField(
  31. label=_("Max number of users invited to private thread"),
  32. help_text=_("Enter 0 to don't limit number of participants."),
  33. initial=3,
  34. min_value=0)
  35. can_add_everyone_to_private_threads = forms.YesNoSwitch(
  36. label=_("Can add everyone to threads"),
  37. help_text=_("Allows user to add users that are "
  38. "blocking him to private threads."))
  39. can_report_private_threads = forms.YesNoSwitch(
  40. label=_("Can report private threads"),
  41. help_text=_("Allows user to report private threads they are "
  42. "participating in, making them accessible to moderators."))
  43. can_moderate_private_threads = forms.YesNoSwitch(
  44. label=_("Can moderate private threads"),
  45. help_text=_("Allows user to read, reply, edit and delete "
  46. "content in reported private threads."))
  47. def change_permissions_form(role):
  48. if isinstance(role, Role) and role.special_role != 'anonymous':
  49. return PermissionsForm
  50. else:
  51. return None
  52. """
  53. ACL Builder
  54. """
  55. def build_acl(acl, roles, key_name):
  56. new_acl = {
  57. 'can_use_private_threads': 0,
  58. 'can_start_private_threads': 0,
  59. 'max_private_thread_participants': 3,
  60. 'can_add_everyone_to_private_threads': 0,
  61. 'can_report_private_threads': 0,
  62. 'can_moderate_private_threads': 0,
  63. }
  64. new_acl.update(acl)
  65. algebra.sum_acls(new_acl, roles=roles, key=key_name,
  66. can_use_private_threads=algebra.greater,
  67. can_start_private_threads=algebra.greater,
  68. max_private_thread_participants=algebra.greater_or_zero,
  69. can_add_everyone_to_private_threads=algebra.greater,
  70. can_report_private_threads=algebra.greater,
  71. can_moderate_private_threads=algebra.greater
  72. )
  73. if not new_acl['can_use_private_threads']:
  74. return new_acl
  75. private_forum = Forum.objects.private_threads()
  76. if new_acl['can_moderate_private_threads']:
  77. new_acl['moderated_forums'].append(private_forum.pk)
  78. forum_acl = {
  79. 'can_see': 1,
  80. 'can_browse': 1,
  81. 'can_see_all_threads': 1,
  82. 'can_see_own_threads': 0,
  83. 'can_start_threads': new_acl['can_start_private_threads'],
  84. 'can_reply_threads': 1,
  85. 'can_edit_threads': 1,
  86. 'can_edit_posts': 1,
  87. 'can_hide_own_threads': 0,
  88. 'can_hide_own_posts': 1,
  89. 'thread_edit_time': 0,
  90. 'post_edit_time': 0,
  91. 'can_hide_threads': 0,
  92. 'can_hide_posts': 0,
  93. 'can_protect_posts': 0,
  94. 'can_merge_posts': 0,
  95. 'can_close_threads': 0,
  96. 'can_review_moderated_content': 0,
  97. 'can_report_content': new_acl['can_report_private_threads'],
  98. 'can_see_reports': 0,
  99. 'can_hide_events': 0,
  100. }
  101. if new_acl['can_moderate_private_threads']:
  102. forum_acl.update({
  103. 'can_edit_threads': 2,
  104. 'can_edit_posts': 2,
  105. 'can_hide_threads': 2,
  106. 'can_hide_posts': 2,
  107. 'can_protect_posts': 1,
  108. 'can_merge_posts': 1,
  109. 'can_see_reports': 1,
  110. 'can_see_reports': 1,
  111. 'can_review_moderated_content': 1,
  112. 'can_hide_events': 2,
  113. })
  114. new_acl['forums'][private_forum.pk] = forum_acl
  115. return new_acl
  116. """
  117. ACL tests
  118. """
  119. def allow_use_private_threads(user):
  120. if user.is_anonymous():
  121. raise PermissionDenied(_("Unsigned members can't use "
  122. "private threads system."))
  123. if not user.acl['can_use_private_threads']:
  124. raise PermissionDenied(_("You can't use private threads system."))
  125. can_use_private_threads = return_boolean(allow_use_private_threads)
  126. def allow_see_private_thread(user, target):
  127. can_see_moderated = user.acl.get('can_moderate_private_threads')
  128. can_see_moderated = can_see_moderated and target.has_reported_posts
  129. can_see_participating = user in [p.user for p in target.participants_list]
  130. if not (can_see_participating or can_see_moderated):
  131. raise Http404()
  132. can_see_private_thread = return_boolean(allow_see_private_thread)
  133. def allow_see_private_post(user, target):
  134. can_see_moderated = user.acl.get('can_moderate_private_threads')
  135. if not (can_see_moderated and target.thread.has_reported_posts):
  136. for participant in target.thread.participants_list:
  137. if participant.user == user and participant.is_removed:
  138. if post.posted_on > target.last_post_on:
  139. raise Http404()
  140. can_see_private_post = return_boolean(allow_see_private_post)
  141. def allow_message_user(user, target):
  142. allow_use_private_threads(user)
  143. if user == target:
  144. raise PermissionDenied(_("You can't message yourself."))
  145. if not user.acl['can_start_private_threads']:
  146. raise PermissionDenied(_("You can't start private threads."))
  147. message_format = {'user': user.username}
  148. if not can_use_private_threads(target):
  149. message = _("%(user)s can't participate in private threads.")
  150. raise PermissionDenied(message % message_format)
  151. if user.acl['can_add_everyone_to_private_threads']:
  152. return None
  153. if user.acl['can_be_blocked'] and target.is_blocking(user):
  154. message = _("%(user)s is blocking you.")
  155. raise PermissionDenied(message % message_format)
  156. if target.can_be_messaged_by_nobody:
  157. message = _("%(user)s is not allowing invitations to private threads.")
  158. raise PermissionDenied(message % message_format)
  159. if target.can_be_messaged_by_followed and not target.is_following(user):
  160. message = _("%(user)s is allowing invitations to private "
  161. "threads only from followed users.")
  162. raise PermissionDenied(message % message_format)
  163. can_message_user = return_boolean(allow_message_user)
  164. """
  165. Queryset helpers
  166. """
  167. def exclude_invisible_private_threads(queryset, user):
  168. if user.acl['can_moderate_private_threads']:
  169. see_participating = Q(participants=user)
  170. see_reported = Q(has_reported_posts=True)
  171. return queryset.filter(see_reported | see_participating)
  172. else:
  173. return queryset.filter(participants=user)