threads.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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.categories.models import Category, RoleCategoryACL, CategoryRole
  9. from misago.categories.permissions import get_categories_roles
  10. from misago.core import forms
  11. from misago.threads.models import Thread, Post, Event
  12. __all__ = [
  13. 'register_with',
  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, CategoryRole):
  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. categories_roles = get_categories_roles(roles)
  153. for category in Category.objects.all_categories():
  154. category_acl = acl['categories'].get(category.pk, {'can_browse': 0})
  155. if category_acl['can_browse']:
  156. acl['categories'][category.pk] = build_category_acl(
  157. category_acl, category, categories_roles, key_name)
  158. if acl['categories'][category.pk]['can_review_moderated_content']:
  159. acl['can_review_moderated_content'].append(category.pk)
  160. if acl['categories'][category.pk]['can_see_reports']:
  161. acl['can_see_reports'].append(category.pk)
  162. return acl
  163. def build_category_acl(acl, category, categories_roles, key_name):
  164. category_roles = categories_roles.get(category.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=category_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_category(user, category):
  223. category_acl = user.acl['categories'].get(category.pk, {})
  224. category.acl.update({
  225. 'can_see_all_threads': 0,
  226. 'can_start_threads': 0,
  227. 'can_reply_threads': 0,
  228. 'can_edit_threads': 0,
  229. 'can_edit_posts': 0,
  230. 'can_hide_own_threads': 0,
  231. 'can_hide_own_posts': 0,
  232. 'thread_edit_time': 0,
  233. 'post_edit_time': 0,
  234. 'can_hide_threads': 0,
  235. 'can_hide_posts': 0,
  236. 'can_protect_posts': 0,
  237. 'can_move_posts': 0,
  238. 'can_merge_posts': 0,
  239. 'can_change_threads_labels': 0,
  240. 'can_pin_threads': 0,
  241. 'can_close_threads': 0,
  242. 'can_move_threads': 0,
  243. 'can_merge_threads': 0,
  244. 'can_split_threads': 0,
  245. 'can_review_moderated_content': 0,
  246. 'can_report_content': 0,
  247. 'can_see_reports': 0,
  248. 'can_hide_events': 0,
  249. })
  250. algebra.sum_acls(category.acl, acls=[category_acl],
  251. can_see_all_threads=algebra.greater)
  252. if user.is_authenticated():
  253. algebra.sum_acls(category.acl, acls=[category_acl],
  254. can_start_threads=algebra.greater,
  255. can_reply_threads=algebra.greater,
  256. can_edit_threads=algebra.greater,
  257. can_edit_posts=algebra.greater,
  258. can_hide_threads=algebra.greater,
  259. can_hide_posts=algebra.greater,
  260. can_hide_own_threads=algebra.greater,
  261. can_hide_own_posts=algebra.greater,
  262. thread_edit_time=algebra.greater_or_zero,
  263. post_edit_time=algebra.greater_or_zero,
  264. can_protect_posts=algebra.greater,
  265. can_move_posts=algebra.greater,
  266. can_merge_posts=algebra.greater,
  267. can_change_threads_labels=algebra.greater,
  268. can_pin_threads=algebra.greater,
  269. can_close_threads=algebra.greater,
  270. can_move_threads=algebra.greater,
  271. can_merge_threads=algebra.greater,
  272. can_split_threads=algebra.greater,
  273. can_review_moderated_content=algebra.greater,
  274. can_report_content=algebra.greater,
  275. can_see_reports=algebra.greater,
  276. can_hide_events=algebra.greater,
  277. )
  278. category.acl['can_see_own_threads'] = not category.acl['can_see_all_threads']
  279. def add_acl_to_thread(user, thread):
  280. category_acl = user.acl['categories'].get(thread.category_id, {})
  281. thread.acl.update({
  282. 'can_reply': can_reply_thread(user, thread),
  283. 'can_edit': can_edit_thread(user, thread),
  284. 'can_hide': category_acl.get('can_hide_threads'),
  285. 'can_change_label': category_acl.get('can_change_threads_labels') == 2,
  286. 'can_pin': category_acl.get('can_pin_threads'),
  287. 'can_close': category_acl.get('can_close_threads'),
  288. 'can_move': category_acl.get('can_move_threads'),
  289. 'can_review': category_acl.get('can_review_moderated_content'),
  290. 'can_report': category_acl.get('can_report_content'),
  291. 'can_see_reports': category_acl.get('can_see_reports')
  292. })
  293. if can_change_owned_thread(user, thread):
  294. if not category_acl.get('can_close_threads'):
  295. thread_is_protected = thread.is_closed or thread.category.is_closed
  296. else:
  297. thread_is_protected = False
  298. if not thread_is_protected:
  299. if not thread.acl['can_change_label']:
  300. can_change_label = category_acl.get('can_change_threads_labels')
  301. thread.acl['can_change_label'] = can_change_label == 1
  302. if not thread.acl['can_hide']:
  303. if not thread.replies:
  304. can_hide_thread = category_acl.get('can_hide_own_threads')
  305. thread.acl['can_hide'] = can_hide_thread
  306. def add_acl_to_post(user, post):
  307. category_acl = user.acl['categories'].get(post.category_id, {})
  308. post.acl.update({
  309. 'can_reply': can_reply_thread(user, post.thread),
  310. 'can_edit': can_edit_post(user, post),
  311. 'can_see_hidden': category_acl.get('can_hide_posts'),
  312. 'can_unhide': can_unhide_post(user, post),
  313. 'can_hide': can_hide_post(user, post),
  314. 'can_delete': can_delete_post(user, post),
  315. 'can_protect': category_acl.get('can_protect_posts'),
  316. 'can_report': category_acl.get('can_report_content'),
  317. 'can_see_reports': category_acl.get('can_see_reports'),
  318. 'can_approve': category_acl.get('can_review_moderated_content'),
  319. })
  320. if not post.is_moderated:
  321. post.acl['can_approve'] = False
  322. if not post.acl['can_see_hidden']:
  323. if user.is_authenticated() and user.id == post.poster_id:
  324. post.acl['can_see_hidden'] = True
  325. else:
  326. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  327. def add_acl_to_event(user, event):
  328. category_acl = user.acl['categories'].get(event.category_id, {})
  329. can_hide_events = category_acl.get('can_hide_events', 0)
  330. event.acl['can_hide'] = can_hide_events > 0
  331. event.acl['can_delete'] = can_hide_events == 2
  332. def register_with(registry):
  333. registry.acl_annotator(Category, add_acl_to_category)
  334. registry.acl_annotator(Thread, add_acl_to_thread)
  335. registry.acl_annotator(Post, add_acl_to_post)
  336. registry.acl_annotator(Event, add_acl_to_event)
  337. """
  338. ACL tests
  339. """
  340. def allow_see_thread(user, target):
  341. category_acl = user.acl['categories'].get(target.category_id, {})
  342. if not category_acl.get('can_browse'):
  343. raise Http404()
  344. if user.is_anonymous() or user.pk != target.starter_id:
  345. if not category_acl.get('can_see_all_threads'):
  346. raise Http404()
  347. if target.is_moderated:
  348. if not category_acl.get('can_review_moderated_content'):
  349. raise Http404()
  350. if target.is_hidden and not category_acl.get('can_hide_threads'):
  351. raise Http404()
  352. can_see_thread = return_boolean(allow_see_thread)
  353. def allow_start_thread(user, target):
  354. if user.is_anonymous():
  355. raise PermissionDenied(_("You have to sign in to start threads."))
  356. if target.is_closed and not target.acl['can_close_threads']:
  357. raise PermissionDenied(
  358. _("This category is closed. You can't start new threads in it."))
  359. if not user.acl['categories'].get(target.id, {'can_start_threads': False}):
  360. raise PermissionDenied(_("You don't have permission to start "
  361. "new threads in this category."))
  362. can_start_thread = return_boolean(allow_start_thread)
  363. def allow_reply_thread(user, target):
  364. if user.is_anonymous():
  365. raise PermissionDenied(_("You have to sign in to reply threads."))
  366. category_acl = target.category.acl
  367. if not category_acl['can_close_threads']:
  368. if target.category.is_closed:
  369. raise PermissionDenied(
  370. _("This category is closed. You can't reply to threads in it."))
  371. if target.is_closed:
  372. raise PermissionDenied(
  373. _("You can't reply to closed threads in this category."))
  374. if not category_acl['can_reply_threads']:
  375. raise PermissionDenied(
  376. _("You can't reply to threads in this category."))
  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. category_acl = target.category.acl
  382. if not category_acl['can_edit_threads']:
  383. raise PermissionDenied(_("You can't edit threads in this category."))
  384. if category_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 category."))
  388. if not category_acl['can_close_threads']:
  389. if target.category.is_closed:
  390. raise PermissionDenied(
  391. _("This category 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 category."))
  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. category_acl['thread_edit_time'])
  401. raise PermissionDenied(
  402. message % {'minutes': category_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. category_acl = user.acl['categories'].get(target.category_id, {})
  407. if not category_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. category_acl = target.category.acl
  415. if not category_acl['can_edit_posts']:
  416. raise PermissionDenied(_("You can't edit posts in this category."))
  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 category_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 category."))
  423. if not category_acl['can_close_threads']:
  424. if target.category.is_closed:
  425. raise PermissionDenied(
  426. _("This category 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 category_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. category_acl['post_edit_time'])
  439. raise PermissionDenied(
  440. message % {'minutes': category_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. category_acl = target.category.acl
  446. if not category_acl['can_hide_posts']:
  447. if not category_acl['can_hide_own_posts']:
  448. raise PermissionDenied(
  449. _("You can't reveal posts in this category."))
  450. if user.id != target.poster_id:
  451. raise PermissionDenied(
  452. _("You can't reveal other users posts in this category."))
  453. if not category_acl['can_close_threads']:
  454. if target.category.is_closed:
  455. raise PermissionDenied(_("This category is closed. You can't "
  456. "reveal posts in it."))
  457. if target.thread.is_closed:
  458. raise PermissionDenied(_("This thread is closed. You can't "
  459. "reveal posts in it."))
  460. if target.is_protected and not category_acl['can_protect_posts']:
  461. raise PermissionDenied(
  462. _("This post is protected. You can't reveal it."))
  463. if has_time_to_edit_post(user, target):
  464. message = ungettext("You can't reveal posts that are "
  465. "older than %(minutes)s minute.",
  466. "You can't reveal posts that are "
  467. "older than %(minutes)s minutes.",
  468. category_acl['post_edit_time'])
  469. raise PermissionDenied(
  470. message % {'minutes': category_acl['post_edit_time']})
  471. if target.id == target.thread.first_post_id:
  472. raise PermissionDenied(_("You can't reveal thread's first post."))
  473. if not target.is_hidden:
  474. raise PermissionDenied(_("Only hidden posts can be revealed."))
  475. can_unhide_post = return_boolean(allow_unhide_post)
  476. def allow_hide_post(user, target):
  477. if user.is_anonymous():
  478. raise PermissionDenied(_("You have to sign in to hide posts."))
  479. category_acl = target.category.acl
  480. if not category_acl['can_hide_posts']:
  481. if not category_acl['can_hide_own_posts']:
  482. raise PermissionDenied(_("You can't hide posts in this category."))
  483. if user.id != target.poster_id:
  484. raise PermissionDenied(
  485. _("You can't hide other users posts in this category."))
  486. if not category_acl['can_close_threads']:
  487. if target.category.is_closed:
  488. raise PermissionDenied(_("This category is closed. You can't "
  489. "hide posts in it."))
  490. if target.thread.is_closed:
  491. raise PermissionDenied(_("This thread is closed. You can't "
  492. "hide posts in it."))
  493. if target.is_protected and not category_acl['can_protect_posts']:
  494. raise PermissionDenied(
  495. _("This post is protected. You can't hide it."))
  496. if has_time_to_edit_post(user, target):
  497. message = ungettext("You can't hide posts that are "
  498. "older than %(minutes)s minute.",
  499. "You can't hide posts that are "
  500. "older than %(minutes)s minutes.",
  501. category_acl['post_edit_time'])
  502. raise PermissionDenied(
  503. message % {'minutes': category_acl['post_edit_time']})
  504. if target.id == target.thread.first_post_id:
  505. raise PermissionDenied(_("You can't hide thread's first post."))
  506. if target.is_hidden:
  507. raise PermissionDenied(_("Only visible posts can be hidden."))
  508. can_hide_post = return_boolean(allow_hide_post)
  509. def allow_delete_post(user, target):
  510. if user.is_anonymous():
  511. raise PermissionDenied(_("You have to sign in to delete posts."))
  512. category_acl = target.category.acl
  513. if category_acl['can_hide_posts'] != 2:
  514. if not category_acl['can_hide_own_posts'] != 2:
  515. raise PermissionDenied(
  516. _("You can't delete posts in this category."))
  517. if user.id != target.poster_id:
  518. raise PermissionDenied(
  519. _("You can't delete other users posts in this category."))
  520. if not category_acl['can_close_threads']:
  521. if target.category.is_closed:
  522. raise PermissionDenied(_("This category is closed. You can't "
  523. "delete posts from it."))
  524. if target.thread.is_closed:
  525. raise PermissionDenied(_("This thread is closed. You can't "
  526. "delete posts from it."))
  527. if target.is_protected and not category_acl['can_protect_posts']:
  528. raise PermissionDenied(
  529. _("This post is protected. You can't delete it."))
  530. if has_time_to_edit_post(user, target):
  531. message = ungettext("You can't delete posts that are "
  532. "older than %(minutes)s minute.",
  533. "You can't delete posts that are "
  534. "older than %(minutes)s minutes.",
  535. category_acl['post_edit_time'])
  536. raise PermissionDenied(
  537. message % {'minutes': category_acl['post_edit_time']})
  538. if target.id == target.thread.first_post_id:
  539. raise PermissionDenied(_("You can't delete thread's first post."))
  540. can_delete_post = return_boolean(allow_delete_post)
  541. """
  542. Permission check helpers
  543. """
  544. def can_change_owned_thread(user, target):
  545. category_acl = user.acl['categories'].get(target.category_id, {})
  546. if user.is_anonymous() or user.pk != target.starter_id:
  547. return False
  548. if target.category.is_closed or target.is_closed:
  549. return False
  550. if target.first_post.is_protected:
  551. return False
  552. return has_time_to_edit_thread(user, target)
  553. def has_time_to_edit_thread(user, target):
  554. category_acl = user.acl['categories'].get(target.category_id, {})
  555. if category_acl.get('thread_edit_time'):
  556. diff = timezone.now() - target.started_on
  557. diff_minutes = int(diff.total_seconds() / 60)
  558. return diff_minutes < category_acl.get('thread_edit_time')
  559. else:
  560. return True
  561. def has_time_to_edit_post(user, target):
  562. category_acl = user.acl['categories'].get(target.category_id, {})
  563. if category_acl.get('post_edit_time'):
  564. diff = timezone.now() - target.posted_on
  565. diff_minutes = int(diff.total_seconds() / 60)
  566. return diff_minutes < category_acl.get('post_edit_time')
  567. else:
  568. return True
  569. """
  570. Queryset helpers
  571. """
  572. def exclude_invisible_threads(queryset, user, category=None):
  573. if category:
  574. return exclude_invisible_category_threads(queryset, user, category)
  575. else:
  576. return exclude_all_invisible_threads(queryset, user)
  577. def exclude_invisible_category_threads(queryset, user, category):
  578. if user.is_authenticated():
  579. condition_author = Q(starter_id=user.id)
  580. can_mod = category.acl['can_review_moderated_content']
  581. can_hide = category.acl['can_hide_threads']
  582. if not can_mod and not can_hide:
  583. condition = Q(is_moderated=False) & Q(is_hidden=False)
  584. queryset = queryset.filter(condition_author | condition)
  585. elif not can_mod:
  586. condition = Q(is_moderated=False)
  587. queryset = queryset.filter(condition_author | condition)
  588. elif not can_hide:
  589. condition = Q(is_hidden=False)
  590. queryset = queryset.filter(condition_author | condition)
  591. else:
  592. if not category.acl['can_review_moderated_content']:
  593. queryset = queryset.filter(is_moderated=False)
  594. if not category.acl['can_hide_threads']:
  595. queryset = queryset.filter(is_hidden=False)
  596. return queryset
  597. def exclude_all_invisible_threads(queryset, user):
  598. categories_in = []
  599. conditions = None
  600. for category in Category.objects.all_categories():
  601. add_acl(user, category)
  602. condition_category = Q(category=category)
  603. condition_author = Q(starter_id=user.id)
  604. # can see all threads?
  605. if category.acl['can_see_all_threads']:
  606. can_mod = category.acl['can_review_moderated_content']
  607. can_hide = category.acl['can_hide_threads']
  608. if not can_mod or not can_hide:
  609. if not can_mod and not can_hide:
  610. condition = Q(is_moderated=False) & Q(is_hidden=False)
  611. elif not can_mod:
  612. condition = Q(is_moderated=False)
  613. elif not can_hide:
  614. condition = Q(is_hidden=False)
  615. visibility_condition = condition_author | condition
  616. visibility_condition = condition_category & visibility_condition
  617. else:
  618. # user can see everything so don't bother with rest of routine
  619. categories_in.append(category.pk)
  620. continue
  621. else:
  622. # show all threads in category made by user
  623. visibility_condition = condition_category & condition_author
  624. if conditions:
  625. conditions = conditions | visibility_condition
  626. else:
  627. conditions = visibility_condition
  628. if conditions and categories_in:
  629. return queryset.filter(Q(category_id__in=categories_in) | conditions)
  630. elif conditions:
  631. return queryset.filter(conditions)
  632. elif categories_in:
  633. return queryset.filter(category_id__in=categories_in)
  634. else:
  635. return Thread.objects.none()
  636. def exclude_invisible_posts(queryset, user, category):
  637. if not category.acl['can_review_moderated_content']:
  638. if user.is_authenticated():
  639. condition_author = Q(poster_id=user.id)
  640. condition = Q(is_moderated=False)
  641. queryset = queryset.filter(condition_author | condition)
  642. else:
  643. queryset = queryset.filter(is_moderated=False)
  644. return queryset