privatethreads.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.contrib.auth import get_user_model
  2. from django.core.exceptions import PermissionDenied
  3. from django.utils.translation import ugettext_lazy as _
  4. from misago.acl import add_acl, algebra
  5. from misago.acl.decorators import return_boolean
  6. from misago.core import forms
  7. __all__ = [
  8. 'allow_message_user',
  9. 'can_message_user'
  10. ]
  11. """
  12. Admin Permissions Form
  13. """
  14. class PermissionsForm(forms.Form):
  15. legend = _("Private threads")
  16. can_use_private_threads = forms.YesNoSwitch(
  17. label=_("Can use private threads"))
  18. can_start_private_threads = forms.YesNoSwitch(
  19. label=_("Can start private threads"))
  20. max_private_thread_participants = forms.IntegerField(
  21. label=_("Max number of users invited to private thread"),
  22. help_text=_("Enter 0 to don't limit number of participants."),
  23. initial=3,
  24. min_value=0)
  25. can_add_everyone_to_private_threads = forms.YesNoSwitch(
  26. label=_("Can add everyone to threads"),
  27. help_text=_("Allows user to add users that are "
  28. "blocking him to private threads."))
  29. can_report_private_threads = forms.YesNoSwitch(
  30. label=_("Can report private threads"),
  31. help_text=_("Allows user to report private threads they are "
  32. "participating in, making them accessible to moderators."))
  33. can_moderate_private_threads = forms.YesNoSwitch(
  34. label=_("Can moderate private threads"),
  35. help_text=_("Allows user to read, reply, edit and delete "
  36. "content in reported private threads."))
  37. def change_permissions_form(role):
  38. if isinstance(role, Role) and role.special_role != 'anonymous':
  39. return PermissionsForm
  40. else:
  41. return None
  42. """
  43. ACL Builder
  44. """
  45. def build_acl(acl, roles, key_name):
  46. new_acl = {
  47. 'can_use_private_threads': 0,
  48. 'can_start_private_threads': 0,
  49. 'max_private_thread_participants': 3,
  50. 'can_add_everyone_to_private_threads': 0,
  51. 'can_report_private_threads': 0,
  52. 'can_moderate_private_threads': 0,
  53. }
  54. new_acl.update(acl)
  55. algebra.sum_acls(new_acl, roles=roles, key=key_name,
  56. can_use_private_threads=algebra.greater,
  57. can_start_private_threads=algebra.greater,
  58. max_private_thread_participants=algebra.greater_or_zero,
  59. can_add_everyone_to_private_threads=algebra.greater,
  60. can_report_private_threads=algebra.greater,
  61. can_moderate_private_threads=algebra.greater
  62. )
  63. return new_acl
  64. """
  65. ACL tests
  66. """
  67. def allow_message_user(user, target):
  68. if not user.acl['can_use_private_threads']:
  69. raise PermissionDenied(_("You can't use private threads system."))
  70. if not user.acl['can_start_private_threads']:
  71. raise PermissionDenied(_("You can't start private threads."))
  72. if user.acl['can_add_everyone_to_private_threads']:
  73. return None
  74. message_format = {'user': user.username}
  75. if user.acl['can_be_blocked'] and target.is_blocking(user):
  76. message = _("%(user)s is blocking you.")
  77. raise PermissionDenied(message % message_format)
  78. if target.can_be_messaged_by_nobody:
  79. message = _("%(user)s is not allowing invitations to private threads.")
  80. raise PermissionDenied(message % message_format)
  81. if target.can_be_messaged_by_followed and not target.is_following(user):
  82. message = _("%(user)s is allowing invitations to private "
  83. "threads only from followed users.")
  84. raise PermissionDenied(message % message_format)
  85. can_message_user = return_boolean(allow_message_user)