privatethreads.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. __all__ = [
  10. 'allow_use_private_threads',
  11. 'can_use_private_threads',
  12. 'allow_message_user',
  13. 'can_message_user',
  14. 'exclude_invisible_private_threads',
  15. ]
  16. """
  17. Admin Permissions Form
  18. """
  19. class PermissionsForm(forms.Form):
  20. legend = _("Private threads")
  21. can_use_private_threads = forms.YesNoSwitch(
  22. label=_("Can use private threads"))
  23. can_start_private_threads = forms.YesNoSwitch(
  24. label=_("Can start private threads"))
  25. max_private_thread_participants = forms.IntegerField(
  26. label=_("Max number of users invited to private thread"),
  27. help_text=_("Enter 0 to don't limit number of participants."),
  28. initial=3,
  29. min_value=0)
  30. can_add_everyone_to_private_threads = forms.YesNoSwitch(
  31. label=_("Can add everyone to threads"),
  32. help_text=_("Allows user to add users that are "
  33. "blocking him to private threads."))
  34. can_report_private_threads = forms.YesNoSwitch(
  35. label=_("Can report private threads"),
  36. help_text=_("Allows user to report private threads they are "
  37. "participating in, making them accessible to moderators."))
  38. can_moderate_private_threads = forms.YesNoSwitch(
  39. label=_("Can moderate private threads"),
  40. help_text=_("Allows user to read, reply, edit and delete "
  41. "content in reported private threads."))
  42. def change_permissions_form(role):
  43. if isinstance(role, Role) and role.special_role != 'anonymous':
  44. return PermissionsForm
  45. else:
  46. return None
  47. """
  48. ACL Builder
  49. """
  50. def build_acl(acl, roles, key_name):
  51. new_acl = {
  52. 'can_use_private_threads': 0,
  53. 'can_start_private_threads': 0,
  54. 'max_private_thread_participants': 3,
  55. 'can_add_everyone_to_private_threads': 0,
  56. 'can_report_private_threads': 0,
  57. 'can_moderate_private_threads': 0,
  58. }
  59. new_acl.update(acl)
  60. algebra.sum_acls(new_acl, roles=roles, key=key_name,
  61. can_use_private_threads=algebra.greater,
  62. can_start_private_threads=algebra.greater,
  63. max_private_thread_participants=algebra.greater_or_zero,
  64. can_add_everyone_to_private_threads=algebra.greater,
  65. can_report_private_threads=algebra.greater,
  66. can_moderate_private_threads=algebra.greater
  67. )
  68. return new_acl
  69. """
  70. ACL tests
  71. """
  72. def allow_use_private_threads(user):
  73. if user.is_anonymous():
  74. raise PermissionDenied(_("Unsigned members can't use "
  75. "private threads system."))
  76. if not user.acl['can_use_private_threads']:
  77. raise PermissionDenied(_("You can't use private threads system."))
  78. can_use_private_threads = return_boolean(allow_use_private_threads)
  79. def allow_message_user(user, target):
  80. allow_use_private_threads(user)
  81. if not user.acl['can_start_private_threads']:
  82. raise PermissionDenied(_("You can't start private threads."))
  83. if user.acl['can_add_everyone_to_private_threads']:
  84. return None
  85. message_format = {'user': user.username}
  86. if user.acl['can_be_blocked'] and target.is_blocking(user):
  87. message = _("%(user)s is blocking you.")
  88. raise PermissionDenied(message % message_format)
  89. if target.can_be_messaged_by_nobody:
  90. message = _("%(user)s is not allowing invitations to private threads.")
  91. raise PermissionDenied(message % message_format)
  92. if target.can_be_messaged_by_followed and not target.is_following(user):
  93. message = _("%(user)s is allowing invitations to private "
  94. "threads only from followed users.")
  95. raise PermissionDenied(message % message_format)
  96. can_message_user = return_boolean(allow_message_user)
  97. """
  98. Queryset helpers
  99. """
  100. def exclude_invisible_private_threads(queryset, user):
  101. if user.acl['can_moderate_private_threads']:
  102. see_participating = Q(participants=user)
  103. see_reported = Q(has_reported_posts=True)
  104. return queryset.filter(see_reported | see_participating)
  105. else:
  106. return queryset.filter(participants=user)