permissions.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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, Event
  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_move_posts = forms.YesNoSwitch(
  92. label=_("Can move posts"))
  93. can_merge_posts = forms.YesNoSwitch(
  94. label=_("Can merge posts"))
  95. can_change_threads_labels = forms.TypedChoiceField(
  96. label=_("Can change threads labels"), coerce=int, initial=0,
  97. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  98. can_change_threads_weight = forms.TypedChoiceField(
  99. label=_("Can change threads weight"), coerce=int, initial=0,
  100. choices=(
  101. (0, _("No")),
  102. (1, _("Pin threads")),
  103. (2, _("Make announcements")),
  104. ))
  105. can_close_threads = forms.TypedChoiceField(
  106. label=_("Can close threads"),
  107. coerce=int,
  108. initial=0,
  109. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  110. can_move_threads = forms.YesNoSwitch(
  111. label=_("Can move threads"))
  112. can_merge_threads = forms.YesNoSwitch(
  113. label=_("Can merge threads"))
  114. can_split_threads = forms.YesNoSwitch(
  115. label=_("Can split threads"))
  116. can_review_moderated_content = forms.YesNoSwitch(
  117. label=_("Can review moderated content"),
  118. help_text=_("Will see and be able to accept moderated content."))
  119. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  120. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  121. can_hide_events = forms.TypedChoiceField(
  122. label=_("Can hide events"),
  123. coerce=int,
  124. initial=0,
  125. choices=(
  126. (0, _("No")),
  127. (1, _("Hide events")),
  128. (2, _("Delete events"))
  129. ))
  130. def change_permissions_form(role):
  131. if isinstance(role, ForumRole):
  132. return PermissionsForm
  133. else:
  134. return None
  135. """
  136. ACL Builder
  137. """
  138. def build_acl(acl, roles, key_name):
  139. acl['moderated_forums'] = []
  140. forums_roles = get_forums_roles(roles)
  141. for forum in Forum.objects.all_forums():
  142. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  143. if forum_acl['can_browse']:
  144. acl['forums'][forum.pk] = build_forum_acl(
  145. forum_acl, forum, forums_roles, key_name)
  146. return acl
  147. def build_forum_acl(acl, forum, forums_roles, key_name):
  148. forum_roles = forums_roles.get(forum.pk, [])
  149. final_acl = {
  150. 'can_see_all_threads': 0,
  151. 'can_start_threads': 0,
  152. 'can_reply_threads': 0,
  153. 'can_edit_threads': 0,
  154. 'can_edit_replies': 0,
  155. 'can_hide_own_threads': 0,
  156. 'can_hide_own_replies': 0,
  157. 'thread_edit_time': 0,
  158. 'reply_edit_time': 0,
  159. 'can_hide_threads': 0,
  160. 'can_hide_replies': 0,
  161. 'can_protect_posts': 0,
  162. 'can_move_posts': 0,
  163. 'can_merge_posts': 0,
  164. 'can_change_threads_labels': 0,
  165. 'can_change_threads_weight': 0,
  166. 'can_close_threads': 0,
  167. 'can_move_threads': 0,
  168. 'can_merge_threads': 0,
  169. 'can_split_threads': 0,
  170. 'can_review_moderated_content': 0,
  171. 'can_report_content': 0,
  172. 'can_see_reports': 0,
  173. 'can_can_hide_events': 0,
  174. }
  175. final_acl.update(acl)
  176. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  177. can_see_all_threads=algebra.greater,
  178. can_start_threads=algebra.greater,
  179. can_reply_threads=algebra.greater,
  180. can_edit_threads=algebra.greater,
  181. can_edit_replies=algebra.greater,
  182. can_hide_threads=algebra.greater,
  183. can_hide_replies=algebra.greater,
  184. can_hide_own_threads=algebra.greater,
  185. can_hide_own_replies=algebra.greater,
  186. thread_edit_time=algebra.greater_or_zero,
  187. reply_edit_time=algebra.greater_or_zero,
  188. can_protect_posts=algebra.greater,
  189. can_move_posts=algebra.greater,
  190. can_merge_posts=algebra.greater,
  191. can_change_threads_labels=algebra.greater,
  192. can_change_threads_weight=algebra.greater,
  193. can_close_threads=algebra.greater,
  194. can_move_threads=algebra.greater,
  195. can_merge_threads=algebra.greater,
  196. can_split_threads=algebra.greater,
  197. can_review_moderated_content=algebra.greater,
  198. can_report_content=algebra.greater,
  199. can_see_reports=algebra.greater,
  200. can_can_hide_events=algebra.greater,
  201. )
  202. return final_acl
  203. """
  204. ACL's for targets
  205. """
  206. def add_acl_to_target(user, target):
  207. if isinstance(target, Forum):
  208. add_acl_to_forum(user, target)
  209. if isinstance(target, Thread):
  210. add_acl_to_thread(user, target)
  211. if isinstance(target, Post):
  212. add_acl_to_post(user, target)
  213. if isinstance(target, Event):
  214. add_acl_to_event(user, target)
  215. def add_acl_to_forum(user, forum):
  216. forum_acl = user.acl['forums'].get(forum.pk, {})
  217. forum.acl.update({
  218. 'can_see_all_threads': 0,
  219. 'can_start_threads': 0,
  220. 'can_reply_threads': 0,
  221. 'can_edit_threads': 0,
  222. 'can_edit_replies': 0,
  223. 'can_hide_own_threads': 0,
  224. 'can_hide_own_replies': 0,
  225. 'thread_edit_time': 0,
  226. 'reply_edit_time': 0,
  227. 'can_hide_threads': 0,
  228. 'can_hide_replies': 0,
  229. 'can_protect_posts': 0,
  230. 'can_move_posts': 0,
  231. 'can_merge_posts': 0,
  232. 'can_change_threads_labels': 0,
  233. 'can_change_threads_weight': 0,
  234. 'can_close_threads': 0,
  235. 'can_move_threads': 0,
  236. 'can_merge_threads': 0,
  237. 'can_split_threads': 0,
  238. 'can_review_moderated_content': 0,
  239. 'can_report_content': 0,
  240. 'can_see_reports': 0,
  241. 'can_can_hide_events': 0,
  242. })
  243. algebra.sum_acls(forum.acl, acls=[forum_acl],
  244. can_see_all_threads=algebra.greater)
  245. if user.is_authenticated():
  246. algebra.sum_acls(forum.acl, acls=[forum_acl],
  247. can_start_threads=algebra.greater,
  248. can_reply_threads=algebra.greater,
  249. can_edit_threads=algebra.greater,
  250. can_edit_replies=algebra.greater,
  251. can_hide_threads=algebra.greater,
  252. can_hide_replies=algebra.greater,
  253. can_hide_own_threads=algebra.greater,
  254. can_hide_own_replies=algebra.greater,
  255. thread_edit_time=algebra.greater_or_zero,
  256. reply_edit_time=algebra.greater_or_zero,
  257. can_protect_posts=algebra.greater,
  258. can_move_posts=algebra.greater,
  259. can_merge_posts=algebra.greater,
  260. can_change_threads_labels=algebra.greater,
  261. can_change_threads_weight=algebra.greater,
  262. can_close_threads=algebra.greater,
  263. can_move_threads=algebra.greater,
  264. can_merge_threads=algebra.greater,
  265. can_split_threads=algebra.greater,
  266. can_review_moderated_content=algebra.greater,
  267. can_report_content=algebra.greater,
  268. can_see_reports=algebra.greater,
  269. can_can_hide_events=algebra.greater,
  270. )
  271. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  272. def add_acl_to_thread(user, thread):
  273. pass
  274. def add_acl_to_post(user, post):
  275. pass
  276. def add_acl_to_event(user, event):
  277. forum_acl = user.acl['forums'].get(event.forum_id, {})
  278. can_hide_events = forum_acl.get('can_can_hide_events', 0)
  279. event.acl['can_hide'] = can_hide_events > 0
  280. event.acl['can_delete'] = can_hide_events == 2
  281. """
  282. ACL tests
  283. """
  284. def allow_see_thread(user, target):
  285. forum_acl = user.acl['forums'].get(target.forum_id, {})
  286. if not forum_acl.get('can_see_all_threads'):
  287. if user.is_anonymous() or user.pk != target.starter_id:
  288. message = _("You can't see other users threads in this forum.")
  289. raise PermissionDenied(user)
  290. can_see_thread = return_boolean(allow_see_thread)
  291. def allow_start_thread(user, target):
  292. if target.is_closed:
  293. message = _("This forum is closed. You can't start new threads in it.")
  294. raise PermissionDenied(message)
  295. if user.is_anonymous():
  296. raise PermissionDenied(_("You have to sign in to start new thread."))
  297. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  298. raise PermissionDenied(_("You don't have permission to start "
  299. "new threads in this forum."))
  300. can_start_thread = return_boolean(allow_start_thread)
  301. """
  302. Queryset helpers
  303. """
  304. def exclude_invisible_threads(user, forum, queryset):
  305. if user.is_authenticated():
  306. condition_author = Q(starter_id=user.id)
  307. can_mod = forum.acl['can_review_moderated_content']
  308. can_hide = forum.acl['can_hide_threads']
  309. if not can_mod and not can_hide:
  310. condition = Q(is_moderated=False) & Q(is_hidden=False)
  311. queryset = queryset.filter(condition_author | condition)
  312. elif not can_mod:
  313. condition = Q(is_moderated=False)
  314. queryset = queryset.filter(condition_author | condition)
  315. elif not can_hide:
  316. condition = Q(is_hidden=False)
  317. queryset = queryset.filter(condition_author | condition)
  318. else:
  319. if not forum.acl['can_review_moderated_content']:
  320. queryset = queryset.filter(is_moderated=False)
  321. if not forum.acl['can_hide_threads']:
  322. queryset = queryset.filter(is_hidden=False)
  323. return queryset
  324. def exclude_invisible_postss(user, forum, queryset):
  325. if user.is_authenticated():
  326. if not forum.acl['can_review_moderated_content']:
  327. condition_author = Q(starter_id=user.id)
  328. condition = Q(is_moderated=False)
  329. queryset = queryset.filter(condition_author | condition)
  330. elif not forum.acl['can_review_moderated_content']:
  331. queryset = queryset.filter(is_moderated=False)
  332. return queryset