threads.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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['moderated_forums'] = []
  151. forums_roles = get_forums_roles(roles)
  152. for forum in Forum.objects.all_forums():
  153. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  154. if forum_acl['can_browse']:
  155. acl['forums'][forum.pk] = build_forum_acl(
  156. forum_acl, forum, forums_roles, key_name)
  157. if acl['forums'][forum.pk]['can_review_moderated_content']:
  158. acl['moderated_forums'].append(forum.pk)
  159. return acl
  160. def build_forum_acl(acl, forum, forums_roles, key_name):
  161. forum_roles = forums_roles.get(forum.pk, [])
  162. final_acl = {
  163. 'can_see_all_threads': 0,
  164. 'can_start_threads': 0,
  165. 'can_reply_threads': 0,
  166. 'can_edit_threads': 0,
  167. 'can_edit_posts': 0,
  168. 'can_hide_own_threads': 0,
  169. 'can_hide_own_posts': 0,
  170. 'thread_edit_time': 0,
  171. 'post_edit_time': 0,
  172. 'can_hide_threads': 0,
  173. 'can_hide_posts': 0,
  174. 'can_protect_posts': 0,
  175. 'can_move_posts': 0,
  176. 'can_merge_posts': 0,
  177. 'can_change_threads_labels': 0,
  178. 'can_pin_threads': 0,
  179. 'can_close_threads': 0,
  180. 'can_move_threads': 0,
  181. 'can_merge_threads': 0,
  182. 'can_split_threads': 0,
  183. 'can_review_moderated_content': 0,
  184. 'can_report_content': 0,
  185. 'can_see_reports': 0,
  186. 'can_hide_events': 0,
  187. }
  188. final_acl.update(acl)
  189. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  190. can_see_all_threads=algebra.greater,
  191. can_start_threads=algebra.greater,
  192. can_reply_threads=algebra.greater,
  193. can_edit_threads=algebra.greater,
  194. can_edit_posts=algebra.greater,
  195. can_hide_threads=algebra.greater,
  196. can_hide_posts=algebra.greater,
  197. can_hide_own_threads=algebra.greater,
  198. can_hide_own_posts=algebra.greater,
  199. thread_edit_time=algebra.greater_or_zero,
  200. post_edit_time=algebra.greater_or_zero,
  201. can_protect_posts=algebra.greater,
  202. can_move_posts=algebra.greater,
  203. can_merge_posts=algebra.greater,
  204. can_change_threads_labels=algebra.greater,
  205. can_pin_threads=algebra.greater,
  206. can_close_threads=algebra.greater,
  207. can_move_threads=algebra.greater,
  208. can_merge_threads=algebra.greater,
  209. can_split_threads=algebra.greater,
  210. can_review_moderated_content=algebra.greater,
  211. can_report_content=algebra.greater,
  212. can_see_reports=algebra.greater,
  213. can_hide_events=algebra.greater,
  214. )
  215. return final_acl
  216. """
  217. ACL's for targets
  218. """
  219. def add_acl_to_target(user, target):
  220. if isinstance(target, Forum):
  221. add_acl_to_forum(user, target)
  222. if isinstance(target, Thread):
  223. add_acl_to_thread(user, target)
  224. if isinstance(target, Post):
  225. add_acl_to_post(user, target)
  226. if isinstance(target, Event):
  227. add_acl_to_event(user, target)
  228. def add_acl_to_forum(user, forum):
  229. forum_acl = user.acl['forums'].get(forum.pk, {})
  230. forum.acl.update({
  231. 'can_see_all_threads': 0,
  232. 'can_start_threads': 0,
  233. 'can_reply_threads': 0,
  234. 'can_edit_threads': 0,
  235. 'can_edit_posts': 0,
  236. 'can_hide_own_threads': 0,
  237. 'can_hide_own_posts': 0,
  238. 'thread_edit_time': 0,
  239. 'post_edit_time': 0,
  240. 'can_hide_threads': 0,
  241. 'can_hide_posts': 0,
  242. 'can_protect_posts': 0,
  243. 'can_move_posts': 0,
  244. 'can_merge_posts': 0,
  245. 'can_change_threads_labels': 0,
  246. 'can_pin_threads': 0,
  247. 'can_close_threads': 0,
  248. 'can_move_threads': 0,
  249. 'can_merge_threads': 0,
  250. 'can_split_threads': 0,
  251. 'can_review_moderated_content': 0,
  252. 'can_report_content': 0,
  253. 'can_see_reports': 0,
  254. 'can_hide_events': 0,
  255. })
  256. algebra.sum_acls(forum.acl, acls=[forum_acl],
  257. can_see_all_threads=algebra.greater)
  258. if user.is_authenticated():
  259. algebra.sum_acls(forum.acl, acls=[forum_acl],
  260. can_start_threads=algebra.greater,
  261. can_reply_threads=algebra.greater,
  262. can_edit_threads=algebra.greater,
  263. can_edit_posts=algebra.greater,
  264. can_hide_threads=algebra.greater,
  265. can_hide_posts=algebra.greater,
  266. can_hide_own_threads=algebra.greater,
  267. can_hide_own_posts=algebra.greater,
  268. thread_edit_time=algebra.greater_or_zero,
  269. post_edit_time=algebra.greater_or_zero,
  270. can_protect_posts=algebra.greater,
  271. can_move_posts=algebra.greater,
  272. can_merge_posts=algebra.greater,
  273. can_change_threads_labels=algebra.greater,
  274. can_pin_threads=algebra.greater,
  275. can_close_threads=algebra.greater,
  276. can_move_threads=algebra.greater,
  277. can_merge_threads=algebra.greater,
  278. can_split_threads=algebra.greater,
  279. can_review_moderated_content=algebra.greater,
  280. can_report_content=algebra.greater,
  281. can_see_reports=algebra.greater,
  282. can_hide_events=algebra.greater,
  283. )
  284. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  285. def add_acl_to_thread(user, thread):
  286. forum_acl = user.acl['forums'].get(thread.forum_id, {})
  287. thread.acl.update({
  288. 'can_reply': can_reply_thread(user, thread),
  289. 'can_edit': can_edit_thread(user, thread),
  290. 'can_hide': forum_acl.get('can_hide_threads'),
  291. 'can_change_label': forum_acl.get('can_change_threads_labels') == 2,
  292. 'can_pin': forum_acl.get('can_pin_threads'),
  293. 'can_close': forum_acl.get('can_close_threads'),
  294. 'can_move': forum_acl.get('can_move_threads'),
  295. 'can_review': forum_acl.get('can_review_moderated_content'),
  296. 'can_report': forum_acl.get('can_report_content'),
  297. 'can_see_reports': forum_acl.get('can_see_reports')
  298. })
  299. if can_change_owned_thread(user, thread):
  300. if not forum_acl.get('can_close_threads'):
  301. thread_is_protected = thread.is_closed or thread.forum.is_closed
  302. else:
  303. thread_is_protected = False
  304. if not thread_is_protected:
  305. if not thread.acl['can_change_label']:
  306. can_change_label = forum_acl.get('can_change_threads_labels')
  307. thread.acl['can_change_label'] = can_change_label == 1
  308. if not thread.acl['can_hide']:
  309. if not thread.replies:
  310. can_hide_thread = forum_acl.get('can_hide_own_threads')
  311. thread.acl['can_hide'] = can_hide_thread
  312. def add_acl_to_post(user, post):
  313. forum_acl = user.acl['forums'].get(post.forum_id, {})
  314. post.acl.update({
  315. 'can_reply': can_reply_thread(user, post.thread),
  316. 'can_edit': can_edit_post(user, post),
  317. 'can_see_hidden': forum_acl.get('can_hide_posts'),
  318. 'can_unhide': can_unhide_post(user, post),
  319. 'can_hide': can_hide_post(user, post),
  320. 'can_delete': can_delete_post(user, post),
  321. 'can_protect': forum_acl.get('can_protect_posts'),
  322. 'can_report': forum_acl.get('can_report_content'),
  323. 'can_see_reports': forum_acl.get('can_see_reports'),
  324. 'can_approve': forum_acl.get('can_review_moderated_content'),
  325. })
  326. if not post.is_moderated:
  327. post.acl['can_approve'] = False
  328. if not post.acl['can_see_hidden']:
  329. if user.is_authenticated() and user.id == post.poster_id:
  330. post.acl['can_see_hidden'] = True
  331. else:
  332. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  333. def add_acl_to_event(user, event):
  334. forum_acl = user.acl['forums'].get(event.forum_id, {})
  335. can_hide_events = forum_acl.get('can_hide_events', 0)
  336. event.acl['can_hide'] = can_hide_events > 0
  337. event.acl['can_delete'] = can_hide_events == 2
  338. """
  339. ACL tests
  340. """
  341. def allow_see_thread(user, target):
  342. forum_acl = user.acl['forums'].get(target.forum_id, {})
  343. if not forum_acl.get('can_browse'):
  344. raise Http404()
  345. if user.is_anonymous() or user.pk != target.starter_id:
  346. if not forum_acl.get('can_see_all_threads'):
  347. raise Http404()
  348. if target.is_moderated:
  349. if not forum_acl.get('can_review_moderated_content'):
  350. raise Http404()
  351. if target.is_hidden and not forum_acl.get('can_hide_threads'):
  352. raise Http404()
  353. can_see_thread = return_boolean(allow_see_thread)
  354. def allow_start_thread(user, target):
  355. if user.is_anonymous():
  356. raise PermissionDenied(_("You have to sign in to start threads."))
  357. if target.is_closed and not target.acl['can_close_threads']:
  358. raise PermissionDenied(
  359. _("This forum is closed. You can't start new threads in it."))
  360. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  361. raise PermissionDenied(_("You don't have permission to start "
  362. "new threads in this forum."))
  363. can_start_thread = return_boolean(allow_start_thread)
  364. def allow_reply_thread(user, target):
  365. if user.is_anonymous():
  366. raise PermissionDenied(_("You have to sign in to reply threads."))
  367. forum_acl = target.forum.acl
  368. if not forum_acl['can_close_threads']:
  369. if target.forum.is_closed:
  370. raise PermissionDenied(
  371. _("This forum is closed. You can't reply to threads in it."))
  372. if target.is_closed:
  373. raise PermissionDenied(
  374. _("You can't reply to closed threads in this forum."))
  375. if not forum_acl['can_reply_threads']:
  376. raise PermissionDenied(_("You can't reply to threads in this forum."))
  377. can_reply_thread = return_boolean(allow_reply_thread)
  378. def allow_edit_thread(user, target):
  379. if user.is_anonymous():
  380. raise PermissionDenied(_("You have to sign in to edit threads."))
  381. forum_acl = target.forum.acl
  382. if not forum_acl['can_edit_threads']:
  383. raise PermissionDenied(_("You can't edit threads in this forum."))
  384. if forum_acl['can_edit_threads'] == 1:
  385. if target.starter_id != user.pk:
  386. raise PermissionDenied(
  387. _("You can't edit other users threads in this forum."))
  388. if not forum_acl['can_close_threads']:
  389. if target.forum.is_closed:
  390. raise PermissionDenied(
  391. _("This forum is closed. You can't edit threads in it."))
  392. if target.is_closed:
  393. raise PermissionDenied(
  394. _("You can't edit closed threads in this forum."))
  395. if not has_time_to_edit_thread(user, target):
  396. message = ungettext("You can't edit threads that are "
  397. "older than %(minutes)s minute.",
  398. "You can't edit threads that are "
  399. "older than %(minutes)s minutes.",
  400. forum_acl['thread_edit_time'])
  401. raise PermissionDenied(
  402. message % {'minutes': forum_acl['thread_edit_time']})
  403. can_edit_thread = return_boolean(allow_edit_thread)
  404. def allow_see_post(user, target):
  405. if target.is_moderated:
  406. forum_acl = user.acl['forums'].get(target.forum_id, {})
  407. if not forum_acl.get('can_review_moderated_content'):
  408. if user.is_anonymous() or user.pk != target.poster_id:
  409. raise Http404()
  410. can_see_post = return_boolean(allow_see_post)
  411. def allow_edit_post(user, target):
  412. if user.is_anonymous():
  413. raise PermissionDenied(_("You have to sign in to edit posts."))
  414. forum_acl = target.forum.acl
  415. if not forum_acl['can_edit_posts']:
  416. raise PermissionDenied(_("You can't edit posts in this forum."))
  417. if target.is_hidden and not can_unhide_post(user, target):
  418. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  419. if forum_acl['can_edit_posts'] == 1:
  420. if target.poster_id != user.pk:
  421. raise PermissionDenied(
  422. _("You can't edit other users posts in this forum."))
  423. if not forum_acl['can_close_threads']:
  424. if target.forum.is_closed:
  425. raise PermissionDenied(
  426. _("This forum is closed. You can't edit posts in it."))
  427. if target.thread.is_closed:
  428. raise PermissionDenied(
  429. _("This thread is closed. You can't edit posts in it."))
  430. if target.is_protected and not forum_acl['can_protect_posts']:
  431. raise PermissionDenied(
  432. _("This post is protected. You can't edit it."))
  433. if not has_time_to_edit_post(user, target):
  434. message = ungettext("You can't edit posts that are "
  435. "older than %(minutes)s minute.",
  436. "You can't edit posts that are "
  437. "older than %(minutes)s minutes.",
  438. forum_acl['post_edit_time'])
  439. raise PermissionDenied(
  440. message % {'minutes': forum_acl['post_edit_time']})
  441. can_edit_post = return_boolean(allow_edit_post)
  442. def allow_unhide_post(user, target):
  443. if user.is_anonymous():
  444. raise PermissionDenied(_("You have to sign in to reveal posts."))
  445. forum_acl = target.forum.acl
  446. if not forum_acl['can_hide_posts']:
  447. if not forum_acl['can_hide_own_posts']:
  448. raise PermissionDenied(_("You can't reveal posts in this forum."))
  449. if user.id != target.poster_id:
  450. raise PermissionDenied(
  451. _("You can't reveal other users posts in this forum."))
  452. if not forum_acl['can_close_threads']:
  453. if target.forum.is_closed:
  454. raise PermissionDenied(_("This forum is closed. You can't "
  455. "reveal posts in it."))
  456. if target.thread.is_closed:
  457. raise PermissionDenied(_("This thread is closed. You can't "
  458. "reveal posts in it."))
  459. if target.is_protected and not forum_acl['can_protect_posts']:
  460. raise PermissionDenied(
  461. _("This post is protected. You can't reveal it."))
  462. if has_time_to_edit_post(user, target):
  463. message = ungettext("You can't reveal posts that are "
  464. "older than %(minutes)s minute.",
  465. "You can't reveal posts that are "
  466. "older than %(minutes)s minutes.",
  467. forum_acl['post_edit_time'])
  468. raise PermissionDenied(
  469. message % {'minutes': forum_acl['post_edit_time']})
  470. if target.id == target.thread.first_post_id:
  471. raise PermissionDenied(_("You can't reveal thread's first post."))
  472. if not target.is_hidden:
  473. raise PermissionDenied(_("Only hidden posts can be revealed."))
  474. can_unhide_post = return_boolean(allow_unhide_post)
  475. def allow_hide_post(user, target):
  476. if user.is_anonymous():
  477. raise PermissionDenied(_("You have to sign in to hide posts."))
  478. forum_acl = target.forum.acl
  479. if not forum_acl['can_hide_posts']:
  480. if not forum_acl['can_hide_own_posts']:
  481. raise PermissionDenied(_("You can't hide posts in this forum."))
  482. if user.id != target.poster_id:
  483. raise PermissionDenied(
  484. _("You can't hide other users posts in this forum."))
  485. if not forum_acl['can_close_threads']:
  486. if target.forum.is_closed:
  487. raise PermissionDenied(_("This forum is closed. You can't "
  488. "hide posts in it."))
  489. if target.thread.is_closed:
  490. raise PermissionDenied(_("This thread is closed. You can't "
  491. "hide posts in it."))
  492. if target.is_protected and not forum_acl['can_protect_posts']:
  493. raise PermissionDenied(
  494. _("This post is protected. You can't hide it."))
  495. if has_time_to_edit_post(user, target):
  496. message = ungettext("You can't hide posts that are "
  497. "older than %(minutes)s minute.",
  498. "You can't hide posts that are "
  499. "older than %(minutes)s minutes.",
  500. forum_acl['post_edit_time'])
  501. raise PermissionDenied(
  502. message % {'minutes': forum_acl['post_edit_time']})
  503. if target.id == target.thread.first_post_id:
  504. raise PermissionDenied(_("You can't hide thread's first post."))
  505. if target.is_hidden:
  506. raise PermissionDenied(_("Only visible posts can be hidden."))
  507. can_hide_post = return_boolean(allow_hide_post)
  508. def allow_delete_post(user, target):
  509. if user.is_anonymous():
  510. raise PermissionDenied(_("You have to sign in to delete posts."))
  511. forum_acl = target.forum.acl
  512. if forum_acl['can_hide_posts'] != 2:
  513. if not forum_acl['can_hide_own_posts'] != 2:
  514. raise PermissionDenied(_("You can't delete posts in this forum."))
  515. if user.id != target.poster_id:
  516. raise PermissionDenied(
  517. _("You can't delete other users posts in this forum."))
  518. if not forum_acl['can_close_threads']:
  519. if target.forum.is_closed:
  520. raise PermissionDenied(_("This forum is closed. You can't "
  521. "delete posts from it."))
  522. if target.thread.is_closed:
  523. raise PermissionDenied(_("This thread is closed. You can't "
  524. "delete posts from it."))
  525. if target.is_protected and not forum_acl['can_protect_posts']:
  526. raise PermissionDenied(
  527. _("This post is protected. You can't delete it."))
  528. if has_time_to_edit_post(user, target):
  529. message = ungettext("You can't delete posts that are "
  530. "older than %(minutes)s minute.",
  531. "You can't delete posts that are "
  532. "older than %(minutes)s minutes.",
  533. forum_acl['post_edit_time'])
  534. raise PermissionDenied(
  535. message % {'minutes': forum_acl['post_edit_time']})
  536. if target.id == target.thread.first_post_id:
  537. raise PermissionDenied(_("You can't delete thread's first post."))
  538. can_delete_post = return_boolean(allow_delete_post)
  539. """
  540. Permission check helpers
  541. """
  542. def can_change_owned_thread(user, target):
  543. forum_acl = user.acl['forums'].get(target.forum_id, {})
  544. if user.is_anonymous() or user.pk != target.starter_id:
  545. return False
  546. if target.forum.is_closed or target.is_closed:
  547. return False
  548. if target.first_post.is_protected:
  549. return False
  550. return has_time_to_edit_thread(user, target)
  551. def has_time_to_edit_thread(user, target):
  552. forum_acl = user.acl['forums'].get(target.forum_id, {})
  553. if forum_acl.get('thread_edit_time'):
  554. diff = timezone.now() - target.started_on
  555. diff_minutes = int(diff.total_seconds() / 60)
  556. return diff_minutes < forum_acl.get('thread_edit_time')
  557. else:
  558. return True
  559. def has_time_to_edit_post(user, target):
  560. forum_acl = user.acl['forums'].get(target.forum_id, {})
  561. if forum_acl.get('post_edit_time'):
  562. diff = timezone.now() - target.posted_on
  563. diff_minutes = int(diff.total_seconds() / 60)
  564. return diff_minutes < forum_acl.get('post_edit_time')
  565. else:
  566. return True
  567. """
  568. Queryset helpers
  569. """
  570. def exclude_invisible_threads(queryset, user, forum=None):
  571. if forum:
  572. return exclude_invisible_forum_threads(queryset, user, forum)
  573. else:
  574. return exclude_all_invisible_threads(queryset, user)
  575. def exclude_invisible_forum_threads(queryset, user, forum):
  576. if user.is_authenticated():
  577. condition_author = Q(starter_id=user.id)
  578. can_mod = forum.acl['can_review_moderated_content']
  579. can_hide = forum.acl['can_hide_threads']
  580. if not can_mod and not can_hide:
  581. condition = Q(is_moderated=False) & Q(is_hidden=False)
  582. queryset = queryset.filter(condition_author | condition)
  583. elif not can_mod:
  584. condition = Q(is_moderated=False)
  585. queryset = queryset.filter(condition_author | condition)
  586. elif not can_hide:
  587. condition = Q(is_hidden=False)
  588. queryset = queryset.filter(condition_author | condition)
  589. else:
  590. if not forum.acl['can_review_moderated_content']:
  591. queryset = queryset.filter(is_moderated=False)
  592. if not forum.acl['can_hide_threads']:
  593. queryset = queryset.filter(is_hidden=False)
  594. return queryset
  595. def exclude_all_invisible_threads(queryset, user):
  596. forums_in = []
  597. conditions = None
  598. for forum in Forum.objects.all_forums():
  599. add_acl(user, forum)
  600. condition_forum = Q(forum=forum)
  601. condition_author = Q(starter_id=user.id)
  602. # can see all threads?
  603. if forum.acl['can_see_all_threads']:
  604. can_mod = forum.acl['can_review_moderated_content']
  605. can_hide = forum.acl['can_hide_threads']
  606. if not can_mod or not can_hide:
  607. if not can_mod and not can_hide:
  608. condition = Q(is_moderated=False) & Q(is_hidden=False)
  609. elif not can_mod:
  610. condition = Q(is_moderated=False)
  611. elif not can_hide:
  612. condition = Q(is_hidden=False)
  613. visibility_condition = condition_author | condition
  614. visibility_condition = condition_forum & visibility_condition
  615. else:
  616. # user can see everything so don't bother with rest of routine
  617. forums_in.append(forum.pk)
  618. continue
  619. else:
  620. # show all threads in forum made by user
  621. visibility_condition = condition_forum & condition_author
  622. if conditions:
  623. conditions = conditions | visibility_condition
  624. else:
  625. conditions = visibility_condition
  626. if conditions and forums_in:
  627. return queryset.filter(Q(forum_id__in=forums_in) | conditions)
  628. elif conditions:
  629. return queryset.filter(conditions)
  630. elif forums_in:
  631. return queryset.filter(forum_id__in=forums_in)
  632. else:
  633. return Thread.objects.none()
  634. def exclude_invisible_posts(queryset, user, forum):
  635. if not forum.acl['can_review_moderated_content']:
  636. if user.is_authenticated():
  637. condition_author = Q(poster_id=user.id)
  638. condition = Q(is_moderated=False)
  639. queryset = queryset.filter(condition_author | condition)
  640. else:
  641. queryset = queryset.filter(is_moderated=False)
  642. return queryset