threads.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. from django.core.exceptions import PermissionDenied
  2. from django.db.models import Q
  3. from django.http import Http404
  4. from django.utils import timezone
  5. from django.utils.translation import ungettext, ugettext_lazy as _
  6. from misago.acl import add_acl, algebra
  7. from misago.acl.decorators import return_boolean
  8. from misago.core import forms
  9. from misago.forums.models import Forum, RoleForumACL, ForumRole
  10. from misago.forums.permissions import get_forums_roles
  11. from misago.threads.models import Thread, Post, Event
  12. __all__ = [
  13. 'add_acl_to_target',
  14. 'allow_see_thread',
  15. 'can_see_thread',
  16. 'allow_start_thread',
  17. 'can_start_thread',
  18. 'allow_reply_thread',
  19. 'can_reply_thread',
  20. 'allow_edit_thread',
  21. 'can_edit_thread',
  22. 'allow_see_post',
  23. 'can_see_post',
  24. 'allow_edit_post',
  25. 'can_edit_post',
  26. 'allow_unhide_post',
  27. 'can_unhide_post',
  28. 'allow_hide_post',
  29. 'can_hide_post',
  30. 'allow_delete_post',
  31. 'can_delete_post',
  32. 'exclude_invisible_threads',
  33. 'exclude_invisible_posts'
  34. ]
  35. """
  36. Admin Permissions Form
  37. """
  38. class PermissionsForm(forms.Form):
  39. legend = _("Threads")
  40. can_see_all_threads = forms.TypedChoiceField(
  41. label=_("Can see threads"),
  42. coerce=int,
  43. initial=0,
  44. choices=((0, _("Started threads")), (1, _("All threads"))))
  45. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  46. can_reply_threads = forms.YesNoSwitch(label=_("Can reply to threads"))
  47. can_edit_threads = forms.TypedChoiceField(
  48. label=_("Can edit threads"),
  49. coerce=int,
  50. initial=0,
  51. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  52. can_hide_own_threads = forms.TypedChoiceField(
  53. label=_("Can hide own threads"),
  54. help_text=_("Only threads started within time limit and "
  55. "with no replies can be hidden."),
  56. coerce=int,
  57. initial=0,
  58. choices=(
  59. (0, _("No")),
  60. (1, _("Hide threads")),
  61. (2, _("Delete threads"))
  62. ))
  63. thread_edit_time = forms.IntegerField(
  64. label=_("Time limit for own threads edits, in minutes"),
  65. help_text=_("Enter 0 to don't limit time for editing own threads."),
  66. initial=0,
  67. min_value=0)
  68. can_hide_threads = forms.TypedChoiceField(
  69. label=_("Can hide all threads"),
  70. coerce=int,
  71. initial=0,
  72. choices=(
  73. (0, _("No")),
  74. (1, _("Hide threads")),
  75. (2, _("Delete threads"))
  76. ))
  77. can_edit_posts = forms.TypedChoiceField(
  78. label=_("Can edit posts"),
  79. coerce=int,
  80. initial=0,
  81. choices=((0, _("No")), (1, _("Own posts")), (2, _("All posts"))))
  82. can_hide_own_posts = forms.TypedChoiceField(
  83. label=_("Can hide own posts"),
  84. help_text=_("Only last posts to thread made within "
  85. "edit time limit can be hidden."),
  86. coerce=int,
  87. initial=0,
  88. choices=(
  89. (0, _("No")),
  90. (1, _("Hide posts")),
  91. (2, _("Delete posts"))
  92. ))
  93. post_edit_time = forms.IntegerField(
  94. label=_("Time limit for own post edits, in minutes"),
  95. help_text=_("Enter 0 to don't limit time for editing own posts."),
  96. initial=0,
  97. min_value=0)
  98. can_hide_posts = forms.TypedChoiceField(
  99. label=_("Can hide all posts"),
  100. coerce=int,
  101. initial=0,
  102. choices=(
  103. (0, _("No")),
  104. (1, _("Hide posts")),
  105. (2, _("Delete posts"))
  106. ))
  107. can_protect_posts = forms.YesNoSwitch(
  108. label=_("Can protect posts"),
  109. help_text=_("Only users with this permission "
  110. "can edit protected posts."))
  111. can_move_posts = forms.YesNoSwitch(
  112. label=_("Can move posts"))
  113. can_merge_posts = forms.YesNoSwitch(
  114. label=_("Can merge posts"))
  115. can_change_threads_labels = forms.TypedChoiceField(
  116. label=_("Can change threads labels"), coerce=int, initial=0,
  117. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  118. can_pin_threads = forms.YesNoSwitch(
  119. label=_("Can pin threads"))
  120. can_close_threads = forms.YesNoSwitch(label=_("Can close threads"))
  121. can_move_threads = forms.YesNoSwitch(
  122. label=_("Can move threads"))
  123. can_merge_threads = forms.YesNoSwitch(
  124. label=_("Can merge threads"))
  125. can_split_threads = forms.YesNoSwitch(
  126. label=_("Can split threads"))
  127. can_review_moderated_content = forms.YesNoSwitch(
  128. label=_("Can review moderated content"),
  129. help_text=_("Will see and be able to accept moderated content."))
  130. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  131. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  132. can_hide_events = forms.TypedChoiceField(
  133. label=_("Can hide events"),
  134. coerce=int,
  135. initial=0,
  136. choices=(
  137. (0, _("No")),
  138. (1, _("Hide events")),
  139. (2, _("Delete events"))
  140. ))
  141. def change_permissions_form(role):
  142. if isinstance(role, ForumRole):
  143. return PermissionsForm
  144. else:
  145. return None
  146. """
  147. ACL Builder
  148. """
  149. def build_acl(acl, roles, key_name):
  150. acl['can_review_moderated_content'] = []
  151. acl['can_see_reports'] = []
  152. forums_roles = get_forums_roles(roles)
  153. for forum in Forum.objects.all_forums():
  154. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  155. if forum_acl['can_browse']:
  156. acl['forums'][forum.pk] = build_forum_acl(
  157. forum_acl, forum, forums_roles, key_name)
  158. if acl['forums'][forum.pk]['can_review_moderated_content']:
  159. acl['can_review_moderated_content'].append(forum.pk)
  160. if acl['forums'][forum.pk]['can_see_reports']:
  161. acl['can_see_reports'].append(forum.pk)
  162. return acl
  163. def build_forum_acl(acl, forum, forums_roles, key_name):
  164. forum_roles = forums_roles.get(forum.pk, [])
  165. final_acl = {
  166. 'can_see_all_threads': 0,
  167. 'can_start_threads': 0,
  168. 'can_reply_threads': 0,
  169. 'can_edit_threads': 0,
  170. 'can_edit_posts': 0,
  171. 'can_hide_own_threads': 0,
  172. 'can_hide_own_posts': 0,
  173. 'thread_edit_time': 0,
  174. 'post_edit_time': 0,
  175. 'can_hide_threads': 0,
  176. 'can_hide_posts': 0,
  177. 'can_protect_posts': 0,
  178. 'can_move_posts': 0,
  179. 'can_merge_posts': 0,
  180. 'can_change_threads_labels': 0,
  181. 'can_pin_threads': 0,
  182. 'can_close_threads': 0,
  183. 'can_move_threads': 0,
  184. 'can_merge_threads': 0,
  185. 'can_split_threads': 0,
  186. 'can_review_moderated_content': 0,
  187. 'can_report_content': 0,
  188. 'can_see_reports': 0,
  189. 'can_hide_events': 0,
  190. }
  191. final_acl.update(acl)
  192. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  193. can_see_all_threads=algebra.greater,
  194. can_start_threads=algebra.greater,
  195. can_reply_threads=algebra.greater,
  196. can_edit_threads=algebra.greater,
  197. can_edit_posts=algebra.greater,
  198. can_hide_threads=algebra.greater,
  199. can_hide_posts=algebra.greater,
  200. can_hide_own_threads=algebra.greater,
  201. can_hide_own_posts=algebra.greater,
  202. thread_edit_time=algebra.greater_or_zero,
  203. post_edit_time=algebra.greater_or_zero,
  204. can_protect_posts=algebra.greater,
  205. can_move_posts=algebra.greater,
  206. can_merge_posts=algebra.greater,
  207. can_change_threads_labels=algebra.greater,
  208. can_pin_threads=algebra.greater,
  209. can_close_threads=algebra.greater,
  210. can_move_threads=algebra.greater,
  211. can_merge_threads=algebra.greater,
  212. can_split_threads=algebra.greater,
  213. can_review_moderated_content=algebra.greater,
  214. can_report_content=algebra.greater,
  215. can_see_reports=algebra.greater,
  216. can_hide_events=algebra.greater,
  217. )
  218. return final_acl
  219. """
  220. ACL's for targets
  221. """
  222. def add_acl_to_target(user, target):
  223. if isinstance(target, Forum):
  224. add_acl_to_forum(user, target)
  225. if isinstance(target, Thread):
  226. add_acl_to_thread(user, target)
  227. if isinstance(target, Post):
  228. add_acl_to_post(user, target)
  229. if isinstance(target, Event):
  230. add_acl_to_event(user, target)
  231. def add_acl_to_forum(user, forum):
  232. forum_acl = user.acl['forums'].get(forum.pk, {})
  233. forum.acl.update({
  234. 'can_see_all_threads': 0,
  235. 'can_start_threads': 0,
  236. 'can_reply_threads': 0,
  237. 'can_edit_threads': 0,
  238. 'can_edit_posts': 0,
  239. 'can_hide_own_threads': 0,
  240. 'can_hide_own_posts': 0,
  241. 'thread_edit_time': 0,
  242. 'post_edit_time': 0,
  243. 'can_hide_threads': 0,
  244. 'can_hide_posts': 0,
  245. 'can_protect_posts': 0,
  246. 'can_move_posts': 0,
  247. 'can_merge_posts': 0,
  248. 'can_change_threads_labels': 0,
  249. 'can_pin_threads': 0,
  250. 'can_close_threads': 0,
  251. 'can_move_threads': 0,
  252. 'can_merge_threads': 0,
  253. 'can_split_threads': 0,
  254. 'can_review_moderated_content': 0,
  255. 'can_report_content': 0,
  256. 'can_see_reports': 0,
  257. 'can_hide_events': 0,
  258. })
  259. algebra.sum_acls(forum.acl, acls=[forum_acl],
  260. can_see_all_threads=algebra.greater)
  261. if user.is_authenticated():
  262. algebra.sum_acls(forum.acl, acls=[forum_acl],
  263. can_start_threads=algebra.greater,
  264. can_reply_threads=algebra.greater,
  265. can_edit_threads=algebra.greater,
  266. can_edit_posts=algebra.greater,
  267. can_hide_threads=algebra.greater,
  268. can_hide_posts=algebra.greater,
  269. can_hide_own_threads=algebra.greater,
  270. can_hide_own_posts=algebra.greater,
  271. thread_edit_time=algebra.greater_or_zero,
  272. post_edit_time=algebra.greater_or_zero,
  273. can_protect_posts=algebra.greater,
  274. can_move_posts=algebra.greater,
  275. can_merge_posts=algebra.greater,
  276. can_change_threads_labels=algebra.greater,
  277. can_pin_threads=algebra.greater,
  278. can_close_threads=algebra.greater,
  279. can_move_threads=algebra.greater,
  280. can_merge_threads=algebra.greater,
  281. can_split_threads=algebra.greater,
  282. can_review_moderated_content=algebra.greater,
  283. can_report_content=algebra.greater,
  284. can_see_reports=algebra.greater,
  285. can_hide_events=algebra.greater,
  286. )
  287. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  288. def add_acl_to_thread(user, thread):
  289. forum_acl = user.acl['forums'].get(thread.forum_id, {})
  290. thread.acl.update({
  291. 'can_reply': can_reply_thread(user, thread),
  292. 'can_edit': can_edit_thread(user, thread),
  293. 'can_hide': forum_acl.get('can_hide_threads'),
  294. 'can_change_label': forum_acl.get('can_change_threads_labels') == 2,
  295. 'can_pin': forum_acl.get('can_pin_threads'),
  296. 'can_close': forum_acl.get('can_close_threads'),
  297. 'can_move': forum_acl.get('can_move_threads'),
  298. 'can_review': forum_acl.get('can_review_moderated_content'),
  299. 'can_report': forum_acl.get('can_report_content'),
  300. 'can_see_reports': forum_acl.get('can_see_reports')
  301. })
  302. if can_change_owned_thread(user, thread):
  303. if not forum_acl.get('can_close_threads'):
  304. thread_is_protected = thread.is_closed or thread.forum.is_closed
  305. else:
  306. thread_is_protected = False
  307. if not thread_is_protected:
  308. if not thread.acl['can_change_label']:
  309. can_change_label = forum_acl.get('can_change_threads_labels')
  310. thread.acl['can_change_label'] = can_change_label == 1
  311. if not thread.acl['can_hide']:
  312. if not thread.replies:
  313. can_hide_thread = forum_acl.get('can_hide_own_threads')
  314. thread.acl['can_hide'] = can_hide_thread
  315. def add_acl_to_post(user, post):
  316. forum_acl = user.acl['forums'].get(post.forum_id, {})
  317. post.acl.update({
  318. 'can_reply': can_reply_thread(user, post.thread),
  319. 'can_edit': can_edit_post(user, post),
  320. 'can_see_hidden': forum_acl.get('can_hide_posts'),
  321. 'can_unhide': can_unhide_post(user, post),
  322. 'can_hide': can_hide_post(user, post),
  323. 'can_delete': can_delete_post(user, post),
  324. 'can_protect': forum_acl.get('can_protect_posts'),
  325. 'can_report': forum_acl.get('can_report_content'),
  326. 'can_see_reports': forum_acl.get('can_see_reports'),
  327. 'can_approve': forum_acl.get('can_review_moderated_content'),
  328. })
  329. if not post.is_moderated:
  330. post.acl['can_approve'] = False
  331. if not post.acl['can_see_hidden']:
  332. if user.is_authenticated() and user.id == post.poster_id:
  333. post.acl['can_see_hidden'] = True
  334. else:
  335. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  336. def add_acl_to_event(user, event):
  337. forum_acl = user.acl['forums'].get(event.forum_id, {})
  338. can_hide_events = forum_acl.get('can_hide_events', 0)
  339. event.acl['can_hide'] = can_hide_events > 0
  340. event.acl['can_delete'] = can_hide_events == 2
  341. """
  342. ACL tests
  343. """
  344. def allow_see_thread(user, target):
  345. forum_acl = user.acl['forums'].get(target.forum_id, {})
  346. if not forum_acl.get('can_browse'):
  347. raise Http404()
  348. if user.is_anonymous() or user.pk != target.starter_id:
  349. if not forum_acl.get('can_see_all_threads'):
  350. raise Http404()
  351. if target.is_moderated:
  352. if not forum_acl.get('can_review_moderated_content'):
  353. raise Http404()
  354. if target.is_hidden and not forum_acl.get('can_hide_threads'):
  355. raise Http404()
  356. can_see_thread = return_boolean(allow_see_thread)
  357. def allow_start_thread(user, target):
  358. if user.is_anonymous():
  359. raise PermissionDenied(_("You have to sign in to start threads."))
  360. if target.is_closed and not target.acl['can_close_threads']:
  361. raise PermissionDenied(
  362. _("This forum is closed. You can't start new threads in it."))
  363. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  364. raise PermissionDenied(_("You don't have permission to start "
  365. "new threads in this forum."))
  366. can_start_thread = return_boolean(allow_start_thread)
  367. def allow_reply_thread(user, target):
  368. if user.is_anonymous():
  369. raise PermissionDenied(_("You have to sign in to reply threads."))
  370. forum_acl = target.forum.acl
  371. if not forum_acl['can_close_threads']:
  372. if target.forum.is_closed:
  373. raise PermissionDenied(
  374. _("This forum is closed. You can't reply to threads in it."))
  375. if target.is_closed:
  376. raise PermissionDenied(
  377. _("You can't reply to closed threads in this forum."))
  378. if not forum_acl['can_reply_threads']:
  379. raise PermissionDenied(_("You can't reply to threads in this forum."))
  380. can_reply_thread = return_boolean(allow_reply_thread)
  381. def allow_edit_thread(user, target):
  382. if user.is_anonymous():
  383. raise PermissionDenied(_("You have to sign in to edit threads."))
  384. forum_acl = target.forum.acl
  385. if not forum_acl['can_edit_threads']:
  386. raise PermissionDenied(_("You can't edit threads in this forum."))
  387. if forum_acl['can_edit_threads'] == 1:
  388. if target.starter_id != user.pk:
  389. raise PermissionDenied(
  390. _("You can't edit other users threads in this forum."))
  391. if not forum_acl['can_close_threads']:
  392. if target.forum.is_closed:
  393. raise PermissionDenied(
  394. _("This forum is closed. You can't edit threads in it."))
  395. if target.is_closed:
  396. raise PermissionDenied(
  397. _("You can't edit closed threads in this forum."))
  398. if not has_time_to_edit_thread(user, target):
  399. message = ungettext("You can't edit threads that are "
  400. "older than %(minutes)s minute.",
  401. "You can't edit threads that are "
  402. "older than %(minutes)s minutes.",
  403. forum_acl['thread_edit_time'])
  404. raise PermissionDenied(
  405. message % {'minutes': forum_acl['thread_edit_time']})
  406. can_edit_thread = return_boolean(allow_edit_thread)
  407. def allow_see_post(user, target):
  408. if target.is_moderated:
  409. forum_acl = user.acl['forums'].get(target.forum_id, {})
  410. if not forum_acl.get('can_review_moderated_content'):
  411. if user.is_anonymous() or user.pk != target.poster_id:
  412. raise Http404()
  413. can_see_post = return_boolean(allow_see_post)
  414. def allow_edit_post(user, target):
  415. if user.is_anonymous():
  416. raise PermissionDenied(_("You have to sign in to edit posts."))
  417. forum_acl = target.forum.acl
  418. if not forum_acl['can_edit_posts']:
  419. raise PermissionDenied(_("You can't edit posts in this forum."))
  420. if target.is_hidden and not can_unhide_post(user, target):
  421. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  422. if forum_acl['can_edit_posts'] == 1:
  423. if target.poster_id != user.pk:
  424. raise PermissionDenied(
  425. _("You can't edit other users posts in this forum."))
  426. if not forum_acl['can_close_threads']:
  427. if target.forum.is_closed:
  428. raise PermissionDenied(
  429. _("This forum is closed. You can't edit posts in it."))
  430. if target.thread.is_closed:
  431. raise PermissionDenied(
  432. _("This thread is closed. You can't edit posts in it."))
  433. if target.is_protected and not forum_acl['can_protect_posts']:
  434. raise PermissionDenied(
  435. _("This post is protected. You can't edit it."))
  436. if not has_time_to_edit_post(user, target):
  437. message = ungettext("You can't edit posts that are "
  438. "older than %(minutes)s minute.",
  439. "You can't edit posts that are "
  440. "older than %(minutes)s minutes.",
  441. forum_acl['post_edit_time'])
  442. raise PermissionDenied(
  443. message % {'minutes': forum_acl['post_edit_time']})
  444. can_edit_post = return_boolean(allow_edit_post)
  445. def allow_unhide_post(user, target):
  446. if user.is_anonymous():
  447. raise PermissionDenied(_("You have to sign in to reveal posts."))
  448. forum_acl = target.forum.acl
  449. if not forum_acl['can_hide_posts']:
  450. if not forum_acl['can_hide_own_posts']:
  451. raise PermissionDenied(_("You can't reveal posts in this forum."))
  452. if user.id != target.poster_id:
  453. raise PermissionDenied(
  454. _("You can't reveal other users posts in this forum."))
  455. if not forum_acl['can_close_threads']:
  456. if target.forum.is_closed:
  457. raise PermissionDenied(_("This forum is closed. You can't "
  458. "reveal posts in it."))
  459. if target.thread.is_closed:
  460. raise PermissionDenied(_("This thread is closed. You can't "
  461. "reveal posts in it."))
  462. if target.is_protected and not forum_acl['can_protect_posts']:
  463. raise PermissionDenied(
  464. _("This post is protected. You can't reveal it."))
  465. if has_time_to_edit_post(user, target):
  466. message = ungettext("You can't reveal posts that are "
  467. "older than %(minutes)s minute.",
  468. "You can't reveal posts that are "
  469. "older than %(minutes)s minutes.",
  470. forum_acl['post_edit_time'])
  471. raise PermissionDenied(
  472. message % {'minutes': forum_acl['post_edit_time']})
  473. if target.id == target.thread.first_post_id:
  474. raise PermissionDenied(_("You can't reveal thread's first post."))
  475. if not target.is_hidden:
  476. raise PermissionDenied(_("Only hidden posts can be revealed."))
  477. can_unhide_post = return_boolean(allow_unhide_post)
  478. def allow_hide_post(user, target):
  479. if user.is_anonymous():
  480. raise PermissionDenied(_("You have to sign in to hide posts."))
  481. forum_acl = target.forum.acl
  482. if not forum_acl['can_hide_posts']:
  483. if not forum_acl['can_hide_own_posts']:
  484. raise PermissionDenied(_("You can't hide posts in this forum."))
  485. if user.id != target.poster_id:
  486. raise PermissionDenied(
  487. _("You can't hide other users posts in this forum."))
  488. if not forum_acl['can_close_threads']:
  489. if target.forum.is_closed:
  490. raise PermissionDenied(_("This forum is closed. You can't "
  491. "hide posts in it."))
  492. if target.thread.is_closed:
  493. raise PermissionDenied(_("This thread is closed. You can't "
  494. "hide posts in it."))
  495. if target.is_protected and not forum_acl['can_protect_posts']:
  496. raise PermissionDenied(
  497. _("This post is protected. You can't hide it."))
  498. if has_time_to_edit_post(user, target):
  499. message = ungettext("You can't hide posts that are "
  500. "older than %(minutes)s minute.",
  501. "You can't hide posts that are "
  502. "older than %(minutes)s minutes.",
  503. forum_acl['post_edit_time'])
  504. raise PermissionDenied(
  505. message % {'minutes': forum_acl['post_edit_time']})
  506. if target.id == target.thread.first_post_id:
  507. raise PermissionDenied(_("You can't hide thread's first post."))
  508. if target.is_hidden:
  509. raise PermissionDenied(_("Only visible posts can be hidden."))
  510. can_hide_post = return_boolean(allow_hide_post)
  511. def allow_delete_post(user, target):
  512. if user.is_anonymous():
  513. raise PermissionDenied(_("You have to sign in to delete posts."))
  514. forum_acl = target.forum.acl
  515. if forum_acl['can_hide_posts'] != 2:
  516. if not forum_acl['can_hide_own_posts'] != 2:
  517. raise PermissionDenied(_("You can't delete posts in this forum."))
  518. if user.id != target.poster_id:
  519. raise PermissionDenied(
  520. _("You can't delete other users posts in this forum."))
  521. if not forum_acl['can_close_threads']:
  522. if target.forum.is_closed:
  523. raise PermissionDenied(_("This forum is closed. You can't "
  524. "delete posts from it."))
  525. if target.thread.is_closed:
  526. raise PermissionDenied(_("This thread is closed. You can't "
  527. "delete posts from it."))
  528. if target.is_protected and not forum_acl['can_protect_posts']:
  529. raise PermissionDenied(
  530. _("This post is protected. You can't delete it."))
  531. if has_time_to_edit_post(user, target):
  532. message = ungettext("You can't delete posts that are "
  533. "older than %(minutes)s minute.",
  534. "You can't delete posts that are "
  535. "older than %(minutes)s minutes.",
  536. forum_acl['post_edit_time'])
  537. raise PermissionDenied(
  538. message % {'minutes': forum_acl['post_edit_time']})
  539. if target.id == target.thread.first_post_id:
  540. raise PermissionDenied(_("You can't delete thread's first post."))
  541. can_delete_post = return_boolean(allow_delete_post)
  542. """
  543. Permission check helpers
  544. """
  545. def can_change_owned_thread(user, target):
  546. forum_acl = user.acl['forums'].get(target.forum_id, {})
  547. if user.is_anonymous() or user.pk != target.starter_id:
  548. return False
  549. if target.forum.is_closed or target.is_closed:
  550. return False
  551. if target.first_post.is_protected:
  552. return False
  553. return has_time_to_edit_thread(user, target)
  554. def has_time_to_edit_thread(user, target):
  555. forum_acl = user.acl['forums'].get(target.forum_id, {})
  556. if forum_acl.get('thread_edit_time'):
  557. diff = timezone.now() - target.started_on
  558. diff_minutes = int(diff.total_seconds() / 60)
  559. return diff_minutes < forum_acl.get('thread_edit_time')
  560. else:
  561. return True
  562. def has_time_to_edit_post(user, target):
  563. forum_acl = user.acl['forums'].get(target.forum_id, {})
  564. if forum_acl.get('post_edit_time'):
  565. diff = timezone.now() - target.posted_on
  566. diff_minutes = int(diff.total_seconds() / 60)
  567. return diff_minutes < forum_acl.get('post_edit_time')
  568. else:
  569. return True
  570. """
  571. Queryset helpers
  572. """
  573. def exclude_invisible_threads(queryset, user, forum=None):
  574. if forum:
  575. return exclude_invisible_forum_threads(queryset, user, forum)
  576. else:
  577. return exclude_all_invisible_threads(queryset, user)
  578. def exclude_invisible_forum_threads(queryset, user, forum):
  579. if user.is_authenticated():
  580. condition_author = Q(starter_id=user.id)
  581. can_mod = forum.acl['can_review_moderated_content']
  582. can_hide = forum.acl['can_hide_threads']
  583. if not can_mod and not can_hide:
  584. condition = Q(is_moderated=False) & Q(is_hidden=False)
  585. queryset = queryset.filter(condition_author | condition)
  586. elif not can_mod:
  587. condition = Q(is_moderated=False)
  588. queryset = queryset.filter(condition_author | condition)
  589. elif not can_hide:
  590. condition = Q(is_hidden=False)
  591. queryset = queryset.filter(condition_author | condition)
  592. else:
  593. if not forum.acl['can_review_moderated_content']:
  594. queryset = queryset.filter(is_moderated=False)
  595. if not forum.acl['can_hide_threads']:
  596. queryset = queryset.filter(is_hidden=False)
  597. return queryset
  598. def exclude_all_invisible_threads(queryset, user):
  599. forums_in = []
  600. conditions = None
  601. for forum in Forum.objects.all_forums():
  602. add_acl(user, forum)
  603. condition_forum = Q(forum=forum)
  604. condition_author = Q(starter_id=user.id)
  605. # can see all threads?
  606. if forum.acl['can_see_all_threads']:
  607. can_mod = forum.acl['can_review_moderated_content']
  608. can_hide = forum.acl['can_hide_threads']
  609. if not can_mod or not can_hide:
  610. if not can_mod and not can_hide:
  611. condition = Q(is_moderated=False) & Q(is_hidden=False)
  612. elif not can_mod:
  613. condition = Q(is_moderated=False)
  614. elif not can_hide:
  615. condition = Q(is_hidden=False)
  616. visibility_condition = condition_author | condition
  617. visibility_condition = condition_forum & visibility_condition
  618. else:
  619. # user can see everything so don't bother with rest of routine
  620. forums_in.append(forum.pk)
  621. continue
  622. else:
  623. # show all threads in forum made by user
  624. visibility_condition = condition_forum & condition_author
  625. if conditions:
  626. conditions = conditions | visibility_condition
  627. else:
  628. conditions = visibility_condition
  629. if conditions and forums_in:
  630. return queryset.filter(Q(forum_id__in=forums_in) | conditions)
  631. elif conditions:
  632. return queryset.filter(conditions)
  633. elif forums_in:
  634. return queryset.filter(forum_id__in=forums_in)
  635. else:
  636. return Thread.objects.none()
  637. def exclude_invisible_posts(queryset, user, forum):
  638. if not forum.acl['can_review_moderated_content']:
  639. if user.is_authenticated():
  640. condition_author = Q(poster_id=user.id)
  641. condition = Q(is_moderated=False)
  642. queryset = queryset.filter(condition_author | condition)
  643. else:
  644. queryset = queryset.filter(is_moderated=False)
  645. return queryset