permissions.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. from django.core.exceptions import PermissionDenied
  2. from django.http import Http404
  3. from django.utils.translation import ugettext_lazy as _
  4. from misago.acl import algebra
  5. from misago.acl.decorators import return_boolean
  6. from misago.core import forms
  7. from misago.forums.models import Forum, RoleForumACL, ForumRole
  8. from misago.forums.permissions import get_forums_roles
  9. from misago.threads.models import Thread, Post
  10. """
  11. Admin Permissions Form
  12. """
  13. class PermissionsForm(forms.Form):
  14. legend = _("Threads")
  15. can_see_all_threads = forms.TypedChoiceField(
  16. label=_("Can see threads"),
  17. coerce=int,
  18. initial=0,
  19. choices=((0, _("Started threads")), (1, _("All threads"))))
  20. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  21. can_reply_threads = forms.TypedChoiceField(
  22. label=_("Can reply to threads"),
  23. coerce=int,
  24. initial=0,
  25. choices=((0, _("No")), (1, _("Open threads")), (2, _("All threads"))))
  26. can_edit_threads = forms.TypedChoiceField(
  27. label=_("Can edit threads"),
  28. coerce=int,
  29. initial=0,
  30. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  31. can_edit_replies = forms.TypedChoiceField(
  32. label=_("Can edit replies"),
  33. coerce=int,
  34. initial=0,
  35. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  36. can_change_threads_weight = forms.TypedChoiceField(
  37. label=_("Can change threads weight"), coerce=int, initial=0,
  38. choices=(
  39. (0, _("No")),
  40. (1, _("Pin threads")),
  41. (2, _("Make announcements")),
  42. ))
  43. can_close_threads = forms.TypedChoiceField(
  44. label=_("Can close threads"),
  45. coerce=int,
  46. initial=0,
  47. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  48. def change_permissions_form(role):
  49. if isinstance(role, ForumRole):
  50. return PermissionsForm
  51. else:
  52. return None
  53. """
  54. ACL Builder
  55. """
  56. def build_acl(acl, roles, key_name):
  57. forums_roles = get_forums_roles(roles)
  58. for forum in Forum.objects.all_forums():
  59. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  60. if forum_acl['can_browse']:
  61. acl['forums'][forum.pk] = build_forum_acl(
  62. forum_acl, forum, forums_roles, key_name)
  63. return acl
  64. def build_forum_acl(acl, forum, forums_roles, key_name):
  65. forum_roles = forums_roles.get(forum.pk, [])
  66. final_acl = {
  67. 'can_see_all_threads': 0,
  68. 'can_start_threads': 0,
  69. 'can_change_threads_weight': 0,
  70. 'can_close_threads': 0,
  71. }
  72. final_acl.update(acl)
  73. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  74. can_see_all_threads=algebra.greater,
  75. can_start_threads=algebra.greater,
  76. can_change_threads_weight=algebra.greater,
  77. can_close_threads=algebra.greater,
  78. )
  79. return final_acl
  80. """
  81. ACL's for targets
  82. """
  83. def add_acl_to_target(user, target):
  84. if isinstance(target, Forum):
  85. add_acl_to_forum(user, target)
  86. if isinstance(target, Thread):
  87. add_acl_to_thread(user, target)
  88. if isinstance(target, Post):
  89. add_acl_to_post(user, target)
  90. def add_acl_to_forum(user, forum):
  91. forum_acl = user.acl['forums'].get(forum.pk, {})
  92. forum.acl['can_see_all_threads'] = forum_acl.get('can_see_all_threads', 0)
  93. forum.acl.update({
  94. 'can_start_threads': 0,
  95. 'can_change_threads_weight': 0,
  96. 'can_close_threads': 0,
  97. })
  98. if user.is_authenticated():
  99. algebra.sum_acls(forum.acl, acls=[forum_acl],
  100. can_see_all_threads=algebra.greater,
  101. can_start_threads=algebra.greater,
  102. can_change_threads_weight=algebra.greater,
  103. can_close_threads=algebra.greater,
  104. )
  105. def add_acl_to_thread(user, thread):
  106. pass
  107. def add_acl_to_post(user, post):
  108. pass
  109. """
  110. ACL tests
  111. """
  112. def allow_see_thread(user, target):
  113. forum_acl = user.acl['forums'].get(target.forum_id, {})
  114. if not forum_acl.get('can_see_all_threads'):
  115. if user.is_anonymous() or user.pk != target.starter_id:
  116. message = _("You can't see other users threads in this forum.")
  117. raise PermissionDenied(user)
  118. can_see_thread = return_boolean(allow_see_thread)
  119. def allow_start_thread(user, target):
  120. if target.is_closed:
  121. message = _("This forum is closed. You can't start new threads in it.")
  122. raise PermissionDenied(message)
  123. if user.is_anonymous():
  124. raise PermissionDenied(_("You have to sign in to start new thread."))
  125. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  126. raise PermissionDenied(_("You don't have permission to start "
  127. "new threads in this forum."))
  128. can_start_thread = return_boolean(allow_start_thread)