permissions.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  106. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  107. def change_permissions_form(role):
  108. if isinstance(role, ForumRole):
  109. return PermissionsForm
  110. else:
  111. return None
  112. """
  113. ACL Builder
  114. """
  115. def build_acl(acl, roles, key_name):
  116. acl['moderated_forums'] = []
  117. forums_roles = get_forums_roles(roles)
  118. for forum in Forum.objects.all_forums():
  119. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  120. if forum_acl['can_browse']:
  121. acl['forums'][forum.pk] = build_forum_acl(
  122. forum_acl, forum, forums_roles, key_name)
  123. return acl
  124. def build_forum_acl(acl, forum, forums_roles, key_name):
  125. forum_roles = forums_roles.get(forum.pk, [])
  126. final_acl = {
  127. 'can_see_all_threads': 0,
  128. 'can_start_threads': 0,
  129. 'can_reply_threads': 0,
  130. 'can_edit_threads': 0,
  131. 'can_edit_replies': 0,
  132. 'can_hide_own_threads': 0,
  133. 'can_hide_own_replies': 0,
  134. 'thread_edit_time': 0,
  135. 'reply_edit_time': 0,
  136. 'can_hide_threads': 0,
  137. 'can_hide_replies': 0,
  138. 'can_protect_posts': 0,
  139. 'can_change_threads_weight': 0,
  140. 'can_close_threads': 0,
  141. 'can_review_moderated_content': 0,
  142. 'can_report_content': 0,
  143. 'can_see_reports': 0,
  144. }
  145. final_acl.update(acl)
  146. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  147. can_see_all_threads=algebra.greater,
  148. can_start_threads=algebra.greater,
  149. can_reply_threads=algebra.greater,
  150. can_edit_threads=algebra.greater,
  151. can_edit_replies=algebra.greater,
  152. can_hide_threads=algebra.greater,
  153. can_hide_replies=algebra.greater,
  154. can_hide_own_threads=algebra.greater,
  155. can_hide_own_replies=algebra.greater,
  156. thread_edit_time=algebra.greater_or_zero,
  157. reply_edit_time=algebra.greater_or_zero,
  158. can_protect_posts=algebra.greater,
  159. can_change_threads_weight=algebra.greater,
  160. can_close_threads=algebra.greater,
  161. can_review_moderated_content=algebra.greater,
  162. can_report_content=algebra.greater,
  163. can_see_reports=algebra.greater,
  164. )
  165. return final_acl
  166. """
  167. ACL's for targets
  168. """
  169. def add_acl_to_target(user, target):
  170. if isinstance(target, Forum):
  171. add_acl_to_forum(user, target)
  172. if isinstance(target, Thread):
  173. add_acl_to_thread(user, target)
  174. if isinstance(target, Post):
  175. add_acl_to_post(user, target)
  176. def add_acl_to_forum(user, forum):
  177. forum_acl = user.acl['forums'].get(forum.pk, {})
  178. forum.acl.update({
  179. 'can_see_all_threads': 0,
  180. 'can_start_threads': 0,
  181. 'can_reply_threads': 0,
  182. 'can_edit_threads': 0,
  183. 'can_edit_replies': 0,
  184. 'can_hide_own_threads': 0,
  185. 'can_hide_own_replies': 0,
  186. 'thread_edit_time': 0,
  187. 'reply_edit_time': 0,
  188. 'can_hide_threads': 0,
  189. 'can_hide_replies': 0,
  190. 'can_protect_posts': 0,
  191. 'can_change_threads_weight': 0,
  192. 'can_close_threads': 0,
  193. 'can_review_moderated_content': 0,
  194. 'can_report_content': 0,
  195. 'can_see_reports': 0,
  196. })
  197. if user.is_authenticated():
  198. algebra.sum_acls(forum.acl, acls=[forum_acl],
  199. can_see_all_threads=algebra.greater,
  200. can_start_threads=algebra.greater,
  201. can_reply_threads=algebra.greater,
  202. can_edit_threads=algebra.greater,
  203. can_edit_replies=algebra.greater,
  204. can_hide_threads=algebra.greater,
  205. can_hide_replies=algebra.greater,
  206. can_hide_own_threads=algebra.greater,
  207. can_hide_own_replies=algebra.greater,
  208. thread_edit_time=algebra.greater_or_zero,
  209. reply_edit_time=algebra.greater_or_zero,
  210. can_protect_posts=algebra.greater,
  211. can_change_threads_weight=algebra.greater,
  212. can_close_threads=algebra.greater,
  213. can_review_moderated_content=algebra.greater,
  214. can_report_content=algebra.greater,
  215. can_see_reports=algebra.greater,
  216. )
  217. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  218. def add_acl_to_thread(user, thread):
  219. pass
  220. def add_acl_to_post(user, post):
  221. pass
  222. """
  223. ACL tests
  224. """
  225. def allow_see_thread(user, target):
  226. forum_acl = user.acl['forums'].get(target.forum_id, {})
  227. if not forum_acl.get('can_see_all_threads'):
  228. if user.is_anonymous() or user.pk != target.starter_id:
  229. message = _("You can't see other users threads in this forum.")
  230. raise PermissionDenied(user)
  231. can_see_thread = return_boolean(allow_see_thread)
  232. def allow_start_thread(user, target):
  233. if target.is_closed:
  234. message = _("This forum is closed. You can't start new threads in it.")
  235. raise PermissionDenied(message)
  236. if user.is_anonymous():
  237. raise PermissionDenied(_("You have to sign in to start new thread."))
  238. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  239. raise PermissionDenied(_("You don't have permission to start "
  240. "new threads in this forum."))
  241. can_start_thread = return_boolean(allow_start_thread)