permissions.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. from django.core.exceptions import PermissionDenied
  2. from django.db.models import Q
  3. from django.http import Http404
  4. from django.utils.translation import ugettext_lazy as _
  5. from misago.acl import algebra
  6. from misago.acl.decorators import return_boolean
  7. from misago.core import forms
  8. from misago.forums.models import Forum, RoleForumACL, ForumRole
  9. from misago.forums.permissions import get_forums_roles
  10. from misago.threads.models import Thread, Post
  11. """
  12. Admin Permissions Form
  13. """
  14. class PermissionsForm(forms.Form):
  15. legend = _("Threads")
  16. can_see_all_threads = forms.TypedChoiceField(
  17. label=_("Can see threads"),
  18. coerce=int,
  19. initial=0,
  20. choices=((0, _("Started threads")), (1, _("All threads"))))
  21. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  22. can_reply_threads = forms.TypedChoiceField(
  23. label=_("Can reply to threads"),
  24. coerce=int,
  25. initial=0,
  26. choices=((0, _("No")), (1, _("Open threads")), (2, _("All threads"))))
  27. can_edit_threads = forms.TypedChoiceField(
  28. label=_("Can edit threads"),
  29. coerce=int,
  30. initial=0,
  31. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  32. can_hide_own_threads = forms.TypedChoiceField(
  33. label=_("Can hide own threads"),
  34. help_text=_("Only threads started within time limit and "
  35. "with no replies can be hidden."),
  36. coerce=int,
  37. initial=0,
  38. choices=(
  39. (0, _("No")),
  40. (1, _("Hide threads")),
  41. (2, _("Delete threads"))
  42. ))
  43. thread_edit_time = forms.IntegerField(
  44. label=_("Min. time for own thread edit, in minutes"),
  45. help_text=_("Enter 0 to don't limit time for editing own threads."),
  46. initial=0,
  47. min_value=0)
  48. can_hide_threads = forms.TypedChoiceField(
  49. label=_("Can hide threads"),
  50. coerce=int,
  51. initial=0,
  52. choices=(
  53. (0, _("No")),
  54. (1, _("Hide threads")),
  55. (2, _("Delete threads"))
  56. ))
  57. can_edit_replies = forms.TypedChoiceField(
  58. label=_("Can edit replies"),
  59. coerce=int,
  60. initial=0,
  61. choices=((0, _("No")), (1, _("Own replies")), (2, _("All replies"))))
  62. can_hide_own_replies = forms.TypedChoiceField(
  63. label=_("Can hide own replies"),
  64. help_text=_("Only last replies to thread made within "
  65. "edit time limit can be hidden."),
  66. coerce=int,
  67. initial=0,
  68. choices=(
  69. (0, _("No")),
  70. (1, _("Hide replies")),
  71. (2, _("Delete replies"))
  72. ))
  73. reply_edit_time = forms.IntegerField(
  74. label=_("Min. time for own reply edit, in minutes"),
  75. help_text=_("Enter 0 to don't limit time for editing own replies."),
  76. initial=0,
  77. min_value=0)
  78. can_hide_replies = forms.TypedChoiceField(
  79. label=_("Can hide replies"),
  80. coerce=int,
  81. initial=0,
  82. choices=(
  83. (0, _("No")),
  84. (1, _("Hide replies")),
  85. (2, _("Delete replies"))
  86. ))
  87. can_protect_posts = forms.YesNoSwitch(
  88. label=_("Can protect posts"),
  89. help_text=_("Only users with this permission "
  90. "can edit protected posts."))
  91. can_change_threads_labels = forms.TypedChoiceField(
  92. label=_("Can change threads labels"), coerce=int, initial=0,
  93. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  94. can_change_threads_weight = forms.TypedChoiceField(
  95. label=_("Can change threads weight"), coerce=int, initial=0,
  96. choices=(
  97. (0, _("No")),
  98. (1, _("Pin threads")),
  99. (2, _("Make announcements")),
  100. ))
  101. can_close_threads = forms.TypedChoiceField(
  102. label=_("Can close threads"),
  103. coerce=int,
  104. initial=0,
  105. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  106. can_review_moderated_content = forms.YesNoSwitch(
  107. label=_("Can review moderated content"),
  108. help_text=_("Will see and be able to accept moderated content."))
  109. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  110. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  111. def change_permissions_form(role):
  112. if isinstance(role, ForumRole):
  113. return PermissionsForm
  114. else:
  115. return None
  116. """
  117. ACL Builder
  118. """
  119. def build_acl(acl, roles, key_name):
  120. acl['moderated_forums'] = []
  121. forums_roles = get_forums_roles(roles)
  122. for forum in Forum.objects.all_forums():
  123. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  124. if forum_acl['can_browse']:
  125. acl['forums'][forum.pk] = build_forum_acl(
  126. forum_acl, forum, forums_roles, key_name)
  127. return acl
  128. def build_forum_acl(acl, forum, forums_roles, key_name):
  129. forum_roles = forums_roles.get(forum.pk, [])
  130. final_acl = {
  131. 'can_see_all_threads': 0,
  132. 'can_start_threads': 0,
  133. 'can_reply_threads': 0,
  134. 'can_edit_threads': 0,
  135. 'can_edit_replies': 0,
  136. 'can_hide_own_threads': 0,
  137. 'can_hide_own_replies': 0,
  138. 'thread_edit_time': 0,
  139. 'reply_edit_time': 0,
  140. 'can_hide_threads': 0,
  141. 'can_hide_replies': 0,
  142. 'can_protect_posts': 0,
  143. 'can_change_threads_labels': 0,
  144. 'can_change_threads_weight': 0,
  145. 'can_close_threads': 0,
  146. 'can_review_moderated_content': 0,
  147. 'can_report_content': 0,
  148. 'can_see_reports': 0,
  149. }
  150. final_acl.update(acl)
  151. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  152. can_see_all_threads=algebra.greater,
  153. can_start_threads=algebra.greater,
  154. can_reply_threads=algebra.greater,
  155. can_edit_threads=algebra.greater,
  156. can_edit_replies=algebra.greater,
  157. can_hide_threads=algebra.greater,
  158. can_hide_replies=algebra.greater,
  159. can_hide_own_threads=algebra.greater,
  160. can_hide_own_replies=algebra.greater,
  161. thread_edit_time=algebra.greater_or_zero,
  162. reply_edit_time=algebra.greater_or_zero,
  163. can_protect_posts=algebra.greater,
  164. can_change_threads_labels=algebra.greater,
  165. can_change_threads_weight=algebra.greater,
  166. can_close_threads=algebra.greater,
  167. can_review_moderated_content=algebra.greater,
  168. can_report_content=algebra.greater,
  169. can_see_reports=algebra.greater,
  170. )
  171. return final_acl
  172. """
  173. ACL's for targets
  174. """
  175. def add_acl_to_target(user, target):
  176. if isinstance(target, Forum):
  177. add_acl_to_forum(user, target)
  178. if isinstance(target, Thread):
  179. add_acl_to_thread(user, target)
  180. if isinstance(target, Post):
  181. add_acl_to_post(user, target)
  182. def add_acl_to_forum(user, forum):
  183. forum_acl = user.acl['forums'].get(forum.pk, {})
  184. forum.acl.update({
  185. 'can_see_all_threads': 0,
  186. 'can_start_threads': 0,
  187. 'can_reply_threads': 0,
  188. 'can_edit_threads': 0,
  189. 'can_edit_replies': 0,
  190. 'can_hide_own_threads': 0,
  191. 'can_hide_own_replies': 0,
  192. 'thread_edit_time': 0,
  193. 'reply_edit_time': 0,
  194. 'can_hide_threads': 0,
  195. 'can_hide_replies': 0,
  196. 'can_protect_posts': 0,
  197. 'can_change_threads_labels': 0,
  198. 'can_change_threads_weight': 0,
  199. 'can_close_threads': 0,
  200. 'can_review_moderated_content': 0,
  201. 'can_report_content': 0,
  202. 'can_see_reports': 0,
  203. })
  204. if user.is_authenticated():
  205. algebra.sum_acls(forum.acl, acls=[forum_acl],
  206. can_see_all_threads=algebra.greater,
  207. can_start_threads=algebra.greater,
  208. can_reply_threads=algebra.greater,
  209. can_edit_threads=algebra.greater,
  210. can_edit_replies=algebra.greater,
  211. can_hide_threads=algebra.greater,
  212. can_hide_replies=algebra.greater,
  213. can_hide_own_threads=algebra.greater,
  214. can_hide_own_replies=algebra.greater,
  215. thread_edit_time=algebra.greater_or_zero,
  216. reply_edit_time=algebra.greater_or_zero,
  217. can_protect_posts=algebra.greater,
  218. can_change_threads_labels=algebra.greater,
  219. can_change_threads_weight=algebra.greater,
  220. can_close_threads=algebra.greater,
  221. can_review_moderated_content=algebra.greater,
  222. can_report_content=algebra.greater,
  223. can_see_reports=algebra.greater,
  224. )
  225. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  226. def add_acl_to_thread(user, thread):
  227. pass
  228. def add_acl_to_post(user, post):
  229. pass
  230. """
  231. ACL tests
  232. """
  233. def allow_see_thread(user, target):
  234. forum_acl = user.acl['forums'].get(target.forum_id, {})
  235. if not forum_acl.get('can_see_all_threads'):
  236. if user.is_anonymous() or user.pk != target.starter_id:
  237. message = _("You can't see other users threads in this forum.")
  238. raise PermissionDenied(user)
  239. can_see_thread = return_boolean(allow_see_thread)
  240. def allow_start_thread(user, target):
  241. if target.is_closed:
  242. message = _("This forum is closed. You can't start new threads in it.")
  243. raise PermissionDenied(message)
  244. if user.is_anonymous():
  245. raise PermissionDenied(_("You have to sign in to start new thread."))
  246. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  247. raise PermissionDenied(_("You don't have permission to start "
  248. "new threads in this forum."))
  249. can_start_thread = return_boolean(allow_start_thread)
  250. """
  251. Queryset helpers
  252. """
  253. def exclude_invisible_threads(user, forum, queryset):
  254. if user.is_authenticated():
  255. condition_author = Q(starter_id=user.id)
  256. can_mod = forum.acl['can_review_moderated_content']
  257. can_hide = forum.acl['can_hide_threads']
  258. if not can_mod and not can_hide:
  259. condition = Q(is_moderated=False) & Q(is_hidden=False)
  260. queryset = queryset.filter(condition_author | condition)
  261. elif not can_mod:
  262. condition = Q(is_moderated=False)
  263. queryset = queryset.filter(condition_author | condition)
  264. elif not can_hide:
  265. condition = Q(is_hidden=False)
  266. queryset = queryset.filter(condition_author | condition)
  267. else:
  268. if not forum.acl['can_review_moderated_content']:
  269. queryset = queryset.filter(is_moderated=False)
  270. if not forum.acl['can_hide_threads']:
  271. queryset = queryset.filter(is_hidden=False)
  272. return queryset
  273. def exclude_invisible_postss(user, forum, queryset):
  274. if user.is_authenticated():
  275. if not forum.acl['can_review_moderated_content']:
  276. condition_author = Q(starter_id=user.id)
  277. condition = Q(is_moderated=False)
  278. queryset = queryset.filter(condition_author | condition)
  279. elif not forum.acl['can_review_moderated_content']:
  280. queryset = queryset.filter(is_moderated=False)
  281. return queryset