permissions.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 add_acl, 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_pin_threads = forms.YesNoSwitch(
  99. label=_("Can pin threads"))
  100. can_close_threads = forms.TypedChoiceField(
  101. label=_("Can close threads"),
  102. coerce=int,
  103. initial=0,
  104. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  105. can_move_threads = forms.YesNoSwitch(
  106. label=_("Can move threads"))
  107. can_merge_threads = forms.YesNoSwitch(
  108. label=_("Can merge threads"))
  109. can_split_threads = forms.YesNoSwitch(
  110. label=_("Can split threads"))
  111. can_review_moderated_content = forms.YesNoSwitch(
  112. label=_("Can review moderated content"),
  113. help_text=_("Will see and be able to accept moderated content."))
  114. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  115. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  116. can_hide_events = forms.TypedChoiceField(
  117. label=_("Can hide events"),
  118. coerce=int,
  119. initial=0,
  120. choices=(
  121. (0, _("No")),
  122. (1, _("Hide events")),
  123. (2, _("Delete events"))
  124. ))
  125. def change_permissions_form(role):
  126. if isinstance(role, ForumRole):
  127. return PermissionsForm
  128. else:
  129. return None
  130. """
  131. ACL Builder
  132. """
  133. def build_acl(acl, roles, key_name):
  134. acl['moderated_forums'] = []
  135. forums_roles = get_forums_roles(roles)
  136. for forum in Forum.objects.all_forums():
  137. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  138. if forum_acl['can_browse']:
  139. acl['forums'][forum.pk] = build_forum_acl(
  140. forum_acl, forum, forums_roles, key_name)
  141. return acl
  142. def build_forum_acl(acl, forum, forums_roles, key_name):
  143. forum_roles = forums_roles.get(forum.pk, [])
  144. final_acl = {
  145. 'can_see_all_threads': 0,
  146. 'can_start_threads': 0,
  147. 'can_reply_threads': 0,
  148. 'can_edit_threads': 0,
  149. 'can_edit_replies': 0,
  150. 'can_hide_own_threads': 0,
  151. 'can_hide_own_replies': 0,
  152. 'thread_edit_time': 0,
  153. 'reply_edit_time': 0,
  154. 'can_hide_threads': 0,
  155. 'can_hide_replies': 0,
  156. 'can_protect_posts': 0,
  157. 'can_move_posts': 0,
  158. 'can_merge_posts': 0,
  159. 'can_change_threads_labels': 0,
  160. 'can_pin_threads': 0,
  161. 'can_close_threads': 0,
  162. 'can_move_threads': 0,
  163. 'can_merge_threads': 0,
  164. 'can_split_threads': 0,
  165. 'can_review_moderated_content': 0,
  166. 'can_report_content': 0,
  167. 'can_see_reports': 0,
  168. 'can_can_hide_events': 0,
  169. }
  170. final_acl.update(acl)
  171. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  172. can_see_all_threads=algebra.greater,
  173. can_start_threads=algebra.greater,
  174. can_reply_threads=algebra.greater,
  175. can_edit_threads=algebra.greater,
  176. can_edit_replies=algebra.greater,
  177. can_hide_threads=algebra.greater,
  178. can_hide_replies=algebra.greater,
  179. can_hide_own_threads=algebra.greater,
  180. can_hide_own_replies=algebra.greater,
  181. thread_edit_time=algebra.greater_or_zero,
  182. reply_edit_time=algebra.greater_or_zero,
  183. can_protect_posts=algebra.greater,
  184. can_move_posts=algebra.greater,
  185. can_merge_posts=algebra.greater,
  186. can_change_threads_labels=algebra.greater,
  187. can_pin_threads=algebra.greater,
  188. can_close_threads=algebra.greater,
  189. can_move_threads=algebra.greater,
  190. can_merge_threads=algebra.greater,
  191. can_split_threads=algebra.greater,
  192. can_review_moderated_content=algebra.greater,
  193. can_report_content=algebra.greater,
  194. can_see_reports=algebra.greater,
  195. can_can_hide_events=algebra.greater,
  196. )
  197. return final_acl
  198. """
  199. ACL's for targets
  200. """
  201. def add_acl_to_target(user, target):
  202. if isinstance(target, Forum):
  203. add_acl_to_forum(user, target)
  204. if isinstance(target, Thread):
  205. add_acl_to_thread(user, target)
  206. if isinstance(target, Post):
  207. add_acl_to_post(user, target)
  208. if isinstance(target, Event):
  209. add_acl_to_event(user, target)
  210. def add_acl_to_forum(user, forum):
  211. forum_acl = user.acl['forums'].get(forum.pk, {})
  212. forum.acl.update({
  213. 'can_see_all_threads': 0,
  214. 'can_start_threads': 0,
  215. 'can_reply_threads': 0,
  216. 'can_edit_threads': 0,
  217. 'can_edit_replies': 0,
  218. 'can_hide_own_threads': 0,
  219. 'can_hide_own_replies': 0,
  220. 'thread_edit_time': 0,
  221. 'reply_edit_time': 0,
  222. 'can_hide_threads': 0,
  223. 'can_hide_replies': 0,
  224. 'can_protect_posts': 0,
  225. 'can_move_posts': 0,
  226. 'can_merge_posts': 0,
  227. 'can_change_threads_labels': 0,
  228. 'can_pin_threads': 0,
  229. 'can_close_threads': 0,
  230. 'can_move_threads': 0,
  231. 'can_merge_threads': 0,
  232. 'can_split_threads': 0,
  233. 'can_review_moderated_content': 0,
  234. 'can_report_content': 0,
  235. 'can_see_reports': 0,
  236. 'can_can_hide_events': 0,
  237. })
  238. algebra.sum_acls(forum.acl, acls=[forum_acl],
  239. can_see_all_threads=algebra.greater)
  240. if user.is_authenticated():
  241. algebra.sum_acls(forum.acl, acls=[forum_acl],
  242. can_start_threads=algebra.greater,
  243. can_reply_threads=algebra.greater,
  244. can_edit_threads=algebra.greater,
  245. can_edit_replies=algebra.greater,
  246. can_hide_threads=algebra.greater,
  247. can_hide_replies=algebra.greater,
  248. can_hide_own_threads=algebra.greater,
  249. can_hide_own_replies=algebra.greater,
  250. thread_edit_time=algebra.greater_or_zero,
  251. reply_edit_time=algebra.greater_or_zero,
  252. can_protect_posts=algebra.greater,
  253. can_move_posts=algebra.greater,
  254. can_merge_posts=algebra.greater,
  255. can_change_threads_labels=algebra.greater,
  256. can_pin_threads=algebra.greater,
  257. can_close_threads=algebra.greater,
  258. can_move_threads=algebra.greater,
  259. can_merge_threads=algebra.greater,
  260. can_split_threads=algebra.greater,
  261. can_review_moderated_content=algebra.greater,
  262. can_report_content=algebra.greater,
  263. can_see_reports=algebra.greater,
  264. can_can_hide_events=algebra.greater,
  265. )
  266. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  267. def add_acl_to_thread(user, thread):
  268. pass
  269. def add_acl_to_post(user, post):
  270. pass
  271. def add_acl_to_event(user, event):
  272. forum_acl = user.acl['forums'].get(event.forum_id, {})
  273. can_hide_events = forum_acl.get('can_can_hide_events', 0)
  274. event.acl['can_hide'] = can_hide_events > 0
  275. event.acl['can_delete'] = can_hide_events == 2
  276. """
  277. ACL tests
  278. """
  279. def allow_see_thread(user, target):
  280. forum_acl = user.acl['forums'].get(target.forum_id, {})
  281. if not forum_acl.get('can_see_all_threads'):
  282. if user.is_anonymous() or user.pk != target.starter_id:
  283. message = _("You can't see other users threads in this forum.")
  284. raise PermissionDenied(user)
  285. can_see_thread = return_boolean(allow_see_thread)
  286. def allow_start_thread(user, target):
  287. if target.is_closed:
  288. message = _("This forum is closed. You can't start new threads in it.")
  289. raise PermissionDenied(message)
  290. if user.is_anonymous():
  291. raise PermissionDenied(_("You have to sign in to start new thread."))
  292. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  293. raise PermissionDenied(_("You don't have permission to start "
  294. "new threads in this forum."))
  295. can_start_thread = return_boolean(allow_start_thread)
  296. """
  297. Queryset helpers
  298. """
  299. def exclude_invisible_threads(queryset, user, forum=None):
  300. if forum:
  301. return exclude_invisible_forum_threads(queryset, user, forum)
  302. else:
  303. return exclude_all_invisible_threads(queryset, user)
  304. def exclude_invisible_forum_threads(queryset, user, forum):
  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_all_invisible_threads(queryset, user):
  325. forums_in = []
  326. conditions = None
  327. for forum in Forum.objects.all_forums():
  328. add_acl(user, forum)
  329. condition_forum = Q(forum=forum)
  330. condition_author = Q(starter_id=user.id)
  331. # can see all threads?
  332. if forum.acl['can_see_all_threads']:
  333. can_mod = forum.acl['can_review_moderated_content']
  334. can_hide = forum.acl['can_hide_threads']
  335. if not can_mod or not can_hide:
  336. if not can_mod and not can_hide:
  337. condition = Q(is_moderated=False) & Q(is_hidden=False)
  338. elif not can_mod:
  339. condition = Q(is_moderated=False)
  340. elif not can_hide:
  341. condition = Q(is_hidden=False)
  342. visibility_condition = condition_author | condition
  343. visibility_condition = condition_forum & visibility_condition
  344. else:
  345. # user can see everything so don't bother with rest of routine
  346. forums_in.append(forum.pk)
  347. continue
  348. else:
  349. # show all threads in forum made by user
  350. visibility_condition = condition_forum & condition_author
  351. if conditions:
  352. conditions = conditions | visibility_condition
  353. else:
  354. conditions = visibility_condition
  355. if conditions and forums_in:
  356. return queryset.filter(Q(forum_id__in=forums_in) | conditions)
  357. elif conditions:
  358. return queryset.filter(conditions)
  359. elif forums_in:
  360. return queryset.filter(forum_id__in=forums_in)
  361. else:
  362. return Thread.objects.none()
  363. def exclude_invisible_posts(queryset, user, forum):
  364. if user.is_authenticated():
  365. if not forum.acl['can_review_moderated_content']:
  366. condition_author = Q(starter_id=user.id)
  367. condition = Q(is_moderated=False)
  368. queryset = queryset.filter(condition_author | condition)
  369. elif not forum.acl['can_review_moderated_content']:
  370. queryset = queryset.filter(is_moderated=False)
  371. return queryset