permissions.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_hide_own_threads = forms.TypedChoiceField(
  32. label=_("Can hide own threads"),
  33. help_text=_("Only threads started within time limit and "
  34. "with no replies can be hidden."),
  35. coerce=int,
  36. initial=0,
  37. choices=(
  38. (0, _("No")),
  39. (1, _("Hide threads")),
  40. (2, _("Delete threads"))
  41. ))
  42. thread_edit_time = forms.IntegerField(
  43. label=_("Min. time for own thread edit, in minutes"),
  44. help_text=_("Enter 0 to don't limit time for editing own threads."),
  45. initial=0,
  46. min_value=0)
  47. can_hide_threads = forms.TypedChoiceField(
  48. label=_("Can hide threads"),
  49. coerce=int,
  50. initial=0,
  51. choices=(
  52. (0, _("No")),
  53. (1, _("Hide threads")),
  54. (2, _("Delete threads"))
  55. ))
  56. can_edit_replies = forms.TypedChoiceField(
  57. label=_("Can edit replies"),
  58. coerce=int,
  59. initial=0,
  60. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  61. can_hide_own_replies = forms.TypedChoiceField(
  62. label=_("Can hide own replies"),
  63. help_text=_("Only last replies to thread made within "
  64. "edit time limit can be hidden."),
  65. coerce=int,
  66. initial=0,
  67. choices=(
  68. (0, _("No")),
  69. (1, _("Hide replies")),
  70. (2, _("Delete replies"))
  71. ))
  72. reply_edit_time = forms.IntegerField(
  73. label=_("Min. time for own reply edit, in minutes"),
  74. help_text=_("Enter 0 to don't limit time for editing own replies."),
  75. initial=0,
  76. min_value=0)
  77. can_hide_replies = forms.TypedChoiceField(
  78. label=_("Can hide replies"),
  79. coerce=int,
  80. initial=0,
  81. choices=(
  82. (0, _("No")),
  83. (1, _("Hide replies")),
  84. (2, _("Delete replies"))
  85. ))
  86. can_protect_posts = forms.YesNoSwitch(
  87. label=_("Can protect posts"),
  88. help_text=_("Only users with this permission "
  89. "can edit protected posts."))
  90. can_change_threads_weight = forms.TypedChoiceField(
  91. label=_("Can change threads weight"), coerce=int, initial=0,
  92. choices=(
  93. (0, _("No")),
  94. (1, _("Pin threads")),
  95. (2, _("Make announcements")),
  96. ))
  97. can_close_threads = forms.TypedChoiceField(
  98. label=_("Can close threads"),
  99. coerce=int,
  100. initial=0,
  101. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  102. can_review_moderated_content = forms.YesNoSwitch(
  103. label=_("Can review moderated content"),
  104. help_text=_("Will see and be able to accept moderated content."))
  105. def change_permissions_form(role):
  106. if isinstance(role, ForumRole):
  107. return PermissionsForm
  108. else:
  109. return None
  110. """
  111. ACL Builder
  112. """
  113. def build_acl(acl, roles, key_name):
  114. acl['moderated_forums'] = []
  115. forums_roles = get_forums_roles(roles)
  116. for forum in Forum.objects.all_forums():
  117. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  118. if forum_acl['can_browse']:
  119. acl['forums'][forum.pk] = build_forum_acl(
  120. forum_acl, forum, forums_roles, key_name)
  121. return acl
  122. def build_forum_acl(acl, forum, forums_roles, key_name):
  123. forum_roles = forums_roles.get(forum.pk, [])
  124. final_acl = {
  125. 'can_see_all_threads': 0,
  126. 'can_start_threads': 0,
  127. 'can_reply_threads': 0,
  128. 'can_edit_threads': 0,
  129. 'can_edit_replies': 0,
  130. 'can_hide_own_threads': 0,
  131. 'can_hide_own_replies': 0,
  132. 'thread_edit_time': 0,
  133. 'reply_edit_time': 0,
  134. 'can_hide_threads': 0,
  135. 'can_hide_replies': 0,
  136. 'can_protect_posts': 0,
  137. 'can_change_threads_weight': 0,
  138. 'can_close_threads': 0,
  139. 'can_review_moderated_content': 0,
  140. }
  141. final_acl.update(acl)
  142. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  143. can_see_all_threads=algebra.greater,
  144. can_start_threads=algebra.greater,
  145. can_reply_threads=algebra.greater,
  146. can_edit_threads=algebra.greater,
  147. can_edit_replies=algebra.greater,
  148. can_hide_threads=algebra.greater,
  149. can_hide_replies=algebra.greater,
  150. can_hide_own_threads=algebra.greater,
  151. can_hide_own_replies=algebra.greater,
  152. thread_edit_time=algebra.greater_or_zero,
  153. reply_edit_time=algebra.greater_or_zero,
  154. can_protect_posts=algebra.greater,
  155. can_change_threads_weight=algebra.greater,
  156. can_close_threads=algebra.greater,
  157. can_review_moderated_content=algebra.greater,
  158. )
  159. return final_acl
  160. """
  161. ACL's for targets
  162. """
  163. def add_acl_to_target(user, target):
  164. if isinstance(target, Forum):
  165. add_acl_to_forum(user, target)
  166. if isinstance(target, Thread):
  167. add_acl_to_thread(user, target)
  168. if isinstance(target, Post):
  169. add_acl_to_post(user, target)
  170. def add_acl_to_forum(user, forum):
  171. forum_acl = user.acl['forums'].get(forum.pk, {})
  172. forum.acl.update({
  173. 'can_see_all_threads': 0,
  174. 'can_start_threads': 0,
  175. 'can_reply_threads': 0,
  176. 'can_edit_threads': 0,
  177. 'can_edit_replies': 0,
  178. 'can_hide_own_threads': 0,
  179. 'can_hide_own_replies': 0,
  180. 'thread_edit_time': 0,
  181. 'reply_edit_time': 0,
  182. 'can_hide_threads': 0,
  183. 'can_hide_replies': 0,
  184. 'can_protect_posts': 0,
  185. 'can_change_threads_weight': 0,
  186. 'can_close_threads': 0,
  187. 'can_review_moderated_content': 0,
  188. })
  189. if user.is_authenticated():
  190. algebra.sum_acls(forum.acl, acls=[forum_acl],
  191. can_see_all_threads=algebra.greater,
  192. can_start_threads=algebra.greater,
  193. can_reply_threads=algebra.greater,
  194. can_edit_threads=algebra.greater,
  195. can_edit_replies=algebra.greater,
  196. can_hide_threads=algebra.greater,
  197. can_hide_replies=algebra.greater,
  198. can_hide_own_threads=algebra.greater,
  199. can_hide_own_replies=algebra.greater,
  200. thread_edit_time=algebra.greater_or_zero,
  201. reply_edit_time=algebra.greater_or_zero,
  202. can_protect_posts=algebra.greater,
  203. can_change_threads_weight=algebra.greater,
  204. can_close_threads=algebra.greater,
  205. can_review_moderated_content=algebra.greater,
  206. )
  207. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  208. def add_acl_to_thread(user, thread):
  209. pass
  210. def add_acl_to_post(user, post):
  211. pass
  212. """
  213. ACL tests
  214. """
  215. def allow_see_thread(user, target):
  216. forum_acl = user.acl['forums'].get(target.forum_id, {})
  217. if not forum_acl.get('can_see_all_threads'):
  218. if user.is_anonymous() or user.pk != target.starter_id:
  219. message = _("You can't see other users threads in this forum.")
  220. raise PermissionDenied(user)
  221. can_see_thread = return_boolean(allow_see_thread)
  222. def allow_start_thread(user, target):
  223. if target.is_closed:
  224. message = _("This forum is closed. You can't start new threads in it.")
  225. raise PermissionDenied(message)
  226. if user.is_anonymous():
  227. raise PermissionDenied(_("You have to sign in to start new thread."))
  228. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  229. raise PermissionDenied(_("You don't have permission to start "
  230. "new threads in this forum."))
  231. can_start_thread = return_boolean(allow_start_thread)