threads.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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. )
  46. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  47. can_reply_threads = forms.YesNoSwitch(label=_("Can reply to threads"))
  48. can_edit_threads = forms.TypedChoiceField(
  49. label=_("Can edit threads"),
  50. coerce=int,
  51. initial=0,
  52. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads")))
  53. )
  54. can_hide_own_threads = forms.TypedChoiceField(
  55. label=_("Can hide own threads"),
  56. help_text=_("Only threads started within time limit and "
  57. "with no replies can be hidden."),
  58. coerce=int,
  59. initial=0,
  60. choices=(
  61. (0, _("No")),
  62. (1, _("Hide threads")),
  63. (2, _("Delete threads"))
  64. )
  65. )
  66. thread_edit_time = forms.IntegerField(
  67. label=_("Time limit for own threads edits, in minutes"),
  68. help_text=_("Enter 0 to don't limit time for editing own threads."),
  69. initial=0,
  70. min_value=0
  71. )
  72. can_hide_threads = forms.TypedChoiceField(
  73. label=_("Can hide all threads"),
  74. coerce=int,
  75. initial=0,
  76. choices=(
  77. (0, _("No")),
  78. (1, _("Hide threads")),
  79. (2, _("Delete threads"))
  80. )
  81. )
  82. can_edit_posts = forms.TypedChoiceField(
  83. label=_("Can edit posts"),
  84. coerce=int,
  85. initial=0,
  86. choices=((0, _("No")), (1, _("Own posts")), (2, _("All posts")))
  87. )
  88. can_hide_own_posts = forms.TypedChoiceField(
  89. label=_("Can hide own posts"),
  90. help_text=_("Only last posts to thread made within "
  91. "edit time limit can be hidden."),
  92. coerce=int,
  93. initial=0,
  94. choices=(
  95. (0, _("No")),
  96. (1, _("Hide posts")),
  97. (2, _("Delete posts"))
  98. )
  99. )
  100. post_edit_time = forms.IntegerField(
  101. label=_("Time limit for own post edits, in minutes"),
  102. help_text=_("Enter 0 to don't limit time for editing own posts."),
  103. initial=0,
  104. min_value=0
  105. )
  106. can_hide_posts = forms.TypedChoiceField(
  107. label=_("Can hide all posts"),
  108. coerce=int,
  109. initial=0,
  110. choices=(
  111. (0, _("No")),
  112. (1, _("Hide posts")),
  113. (2, _("Delete posts"))
  114. )
  115. )
  116. can_protect_posts = forms.YesNoSwitch(
  117. label=_("Can protect posts"),
  118. help_text=_("Only users with this permission can edit protected posts.")
  119. )
  120. can_move_posts = forms.YesNoSwitch(label=_("Can move posts"))
  121. can_merge_posts = forms.YesNoSwitch(label=_("Can merge posts"))
  122. can_change_threads_labels = forms.TypedChoiceField(
  123. label=_("Can change threads labels"),
  124. coerce=int,
  125. initial=0,
  126. choices=(
  127. (0, _("No")),
  128. (1, _("Own threads")),
  129. (2, _("All threads")),
  130. )
  131. )
  132. can_pin_threads = forms.YesNoSwitch(label=_("Can pin threads"))
  133. can_close_threads = forms.YesNoSwitch(label=_("Can close threads"))
  134. can_move_threads = forms.YesNoSwitch(label=_("Can move threads"))
  135. can_merge_threads = forms.YesNoSwitch(label=_("Can merge threads"))
  136. can_split_threads = forms.YesNoSwitch(label=_("Can split threads"))
  137. can_review_moderated_content = forms.YesNoSwitch(
  138. label=_("Can review moderated content"),
  139. help_text=_("Will see and be able to accept moderated content.")
  140. )
  141. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  142. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  143. can_hide_events = forms.TypedChoiceField(
  144. label=_("Can hide events"),
  145. coerce=int,
  146. initial=0,
  147. choices=(
  148. (0, _("No")),
  149. (1, _("Hide events")),
  150. (2, _("Delete events"))
  151. )
  152. )
  153. def change_permissions_form(role):
  154. if isinstance(role, CategoryRole):
  155. return PermissionsForm
  156. else:
  157. return None
  158. """
  159. ACL Builder
  160. """
  161. def build_acl(acl, roles, key_name):
  162. acl['can_review_moderated_content'] = []
  163. acl['can_see_reports'] = []
  164. categories_roles = get_categories_roles(roles)
  165. for category in Category.objects.all_categories():
  166. category_acl = acl['categories'].get(category.pk, {'can_browse': 0})
  167. if category_acl['can_browse']:
  168. acl['categories'][category.pk] = build_category_acl(
  169. category_acl, category, categories_roles, key_name)
  170. if acl['categories'][category.pk]['can_review_moderated_content']:
  171. acl['can_review_moderated_content'].append(category.pk)
  172. if acl['categories'][category.pk]['can_see_reports']:
  173. acl['can_see_reports'].append(category.pk)
  174. return acl
  175. def build_category_acl(acl, category, categories_roles, key_name):
  176. category_roles = categories_roles.get(category.pk, [])
  177. final_acl = {
  178. 'can_see_all_threads': 0,
  179. 'can_start_threads': 0,
  180. 'can_reply_threads': 0,
  181. 'can_edit_threads': 0,
  182. 'can_edit_posts': 0,
  183. 'can_hide_own_threads': 0,
  184. 'can_hide_own_posts': 0,
  185. 'thread_edit_time': 0,
  186. 'post_edit_time': 0,
  187. 'can_hide_threads': 0,
  188. 'can_hide_posts': 0,
  189. 'can_protect_posts': 0,
  190. 'can_move_posts': 0,
  191. 'can_merge_posts': 0,
  192. 'can_pin_threads': 0,
  193. 'can_close_threads': 0,
  194. 'can_move_threads': 0,
  195. 'can_merge_threads': 0,
  196. 'can_split_threads': 0,
  197. 'can_review_moderated_content': 0,
  198. 'can_report_content': 0,
  199. 'can_see_reports': 0,
  200. 'can_hide_events': 0,
  201. }
  202. final_acl.update(acl)
  203. algebra.sum_acls(final_acl, roles=category_roles, key=key_name,
  204. can_see_all_threads=algebra.greater,
  205. can_start_threads=algebra.greater,
  206. can_reply_threads=algebra.greater,
  207. can_edit_threads=algebra.greater,
  208. can_edit_posts=algebra.greater,
  209. can_hide_threads=algebra.greater,
  210. can_hide_posts=algebra.greater,
  211. can_hide_own_threads=algebra.greater,
  212. can_hide_own_posts=algebra.greater,
  213. thread_edit_time=algebra.greater_or_zero,
  214. post_edit_time=algebra.greater_or_zero,
  215. can_protect_posts=algebra.greater,
  216. can_move_posts=algebra.greater,
  217. can_merge_posts=algebra.greater,
  218. can_pin_threads=algebra.greater,
  219. can_close_threads=algebra.greater,
  220. can_move_threads=algebra.greater,
  221. can_merge_threads=algebra.greater,
  222. can_split_threads=algebra.greater,
  223. can_review_moderated_content=algebra.greater,
  224. can_report_content=algebra.greater,
  225. can_see_reports=algebra.greater,
  226. can_hide_events=algebra.greater,
  227. )
  228. return final_acl
  229. """
  230. ACL's for targets
  231. """
  232. def add_acl_to_category(user, category):
  233. category_acl = user.acl['categories'].get(category.pk, {})
  234. category.acl.update({
  235. 'can_see_all_threads': 0,
  236. 'can_start_threads': 0,
  237. 'can_reply_threads': 0,
  238. 'can_edit_threads': 0,
  239. 'can_edit_posts': 0,
  240. 'can_hide_own_threads': 0,
  241. 'can_hide_own_posts': 0,
  242. 'thread_edit_time': 0,
  243. 'post_edit_time': 0,
  244. 'can_hide_threads': 0,
  245. 'can_hide_posts': 0,
  246. 'can_protect_posts': 0,
  247. 'can_move_posts': 0,
  248. 'can_merge_posts': 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(category.acl, acls=[category_acl],
  260. can_see_all_threads=algebra.greater)
  261. if user.is_authenticated():
  262. algebra.sum_acls(category.acl, acls=[category_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_pin_threads=algebra.greater,
  277. can_close_threads=algebra.greater,
  278. can_move_threads=algebra.greater,
  279. can_merge_threads=algebra.greater,
  280. can_split_threads=algebra.greater,
  281. can_review_moderated_content=algebra.greater,
  282. can_report_content=algebra.greater,
  283. can_see_reports=algebra.greater,
  284. can_hide_events=algebra.greater,
  285. )
  286. category.acl['can_see_own_threads'] = not category.acl['can_see_all_threads']
  287. def add_acl_to_thread(user, thread):
  288. category_acl = user.acl['categories'].get(thread.category_id, {})
  289. thread.acl.update({
  290. 'can_reply': can_reply_thread(user, thread),
  291. 'can_edit': can_edit_thread(user, thread),
  292. 'can_hide': category_acl.get('can_hide_threads'),
  293. 'can_pin': category_acl.get('can_pin_threads'),
  294. 'can_close': category_acl.get('can_close_threads'),
  295. 'can_move': category_acl.get('can_move_threads'),
  296. 'can_review': category_acl.get('can_review_moderated_content'),
  297. 'can_report': category_acl.get('can_report_content'),
  298. 'can_see_reports': category_acl.get('can_see_reports')
  299. })
  300. if can_change_owned_thread(user, thread):
  301. if not category_acl.get('can_close_threads'):
  302. thread_is_protected = thread.is_closed or thread.category.is_closed
  303. else:
  304. thread_is_protected = False
  305. if not thread_is_protected and not thread.acl['can_hide']:
  306. if not thread.replies:
  307. can_hide_thread = category_acl.get('can_hide_own_threads')
  308. thread.acl['can_hide'] = can_hide_thread
  309. def add_acl_to_post(user, post):
  310. category_acl = user.acl['categories'].get(post.category_id, {})
  311. post.acl.update({
  312. 'can_reply': can_reply_thread(user, post.thread),
  313. 'can_edit': can_edit_post(user, post),
  314. 'can_see_hidden': category_acl.get('can_hide_posts'),
  315. 'can_unhide': can_unhide_post(user, post),
  316. 'can_hide': can_hide_post(user, post),
  317. 'can_delete': can_delete_post(user, post),
  318. 'can_protect': category_acl.get('can_protect_posts'),
  319. 'can_report': category_acl.get('can_report_content'),
  320. 'can_see_reports': category_acl.get('can_see_reports'),
  321. 'can_approve': category_acl.get('can_review_moderated_content'),
  322. })
  323. if not post.is_moderated:
  324. post.acl['can_approve'] = False
  325. if not post.acl['can_see_hidden']:
  326. if user.is_authenticated() and user.id == post.poster_id:
  327. post.acl['can_see_hidden'] = True
  328. else:
  329. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  330. def add_acl_to_event(user, event):
  331. category_acl = user.acl['categories'].get(event.category_id, {})
  332. can_hide_events = category_acl.get('can_hide_events', 0)
  333. event.acl['can_hide'] = can_hide_events > 0
  334. event.acl['can_delete'] = can_hide_events == 2
  335. def register_with(registry):
  336. registry.acl_annotator(Category, add_acl_to_category)
  337. registry.acl_annotator(Thread, add_acl_to_thread)
  338. registry.acl_annotator(Post, add_acl_to_post)
  339. registry.acl_annotator(Event, add_acl_to_event)
  340. """
  341. ACL tests
  342. """
  343. def allow_see_thread(user, target):
  344. category_acl = user.acl['categories'].get(target.category_id, {})
  345. if not category_acl.get('can_browse'):
  346. raise Http404()
  347. if user.is_anonymous() or user.pk != target.starter_id:
  348. if not category_acl.get('can_see_all_threads'):
  349. raise Http404()
  350. if target.is_moderated:
  351. if not category_acl.get('can_review_moderated_content'):
  352. raise Http404()
  353. if target.is_hidden and not category_acl.get('can_hide_threads'):
  354. raise Http404()
  355. can_see_thread = return_boolean(allow_see_thread)
  356. def allow_start_thread(user, target):
  357. if user.is_anonymous():
  358. raise PermissionDenied(_("You have to sign in to start threads."))
  359. if target.is_closed and not target.acl['can_close_threads']:
  360. raise PermissionDenied(
  361. _("This category is closed. You can't start new threads in it."))
  362. if not user.acl['categories'].get(target.id, {'can_start_threads': False}):
  363. raise PermissionDenied(_("You don't have permission to start "
  364. "new threads in this category."))
  365. can_start_thread = return_boolean(allow_start_thread)
  366. def allow_reply_thread(user, target):
  367. if user.is_anonymous():
  368. raise PermissionDenied(_("You have to sign in to reply threads."))
  369. category_acl = target.category.acl
  370. if not category_acl['can_close_threads']:
  371. if target.category.is_closed:
  372. raise PermissionDenied(
  373. _("This category is closed. You can't reply to threads in it."))
  374. if target.is_closed:
  375. raise PermissionDenied(
  376. _("You can't reply to closed threads in this category."))
  377. if not category_acl['can_reply_threads']:
  378. raise PermissionDenied(_("You can't reply to threads in this category."))
  379. can_reply_thread = return_boolean(allow_reply_thread)
  380. def allow_edit_thread(user, target):
  381. if user.is_anonymous():
  382. raise PermissionDenied(_("You have to sign in to edit threads."))
  383. category_acl = target.category.acl
  384. if not category_acl['can_edit_threads']:
  385. raise PermissionDenied(_("You can't edit threads in this category."))
  386. if category_acl['can_edit_threads'] == 1:
  387. if target.starter_id != user.pk:
  388. raise PermissionDenied(
  389. _("You can't edit other users threads in this category."))
  390. if not category_acl['can_close_threads']:
  391. if target.category.is_closed:
  392. raise PermissionDenied(
  393. _("This category is closed. You can't edit threads in it."))
  394. if target.is_closed:
  395. raise PermissionDenied(
  396. _("You can't edit closed threads in this category."))
  397. if not has_time_to_edit_thread(user, target):
  398. message = ungettext("You can't edit threads that are "
  399. "older than %(minutes)s minute.",
  400. "You can't edit threads that are "
  401. "older than %(minutes)s minutes.",
  402. category_acl['thread_edit_time'])
  403. raise PermissionDenied(
  404. message % {'minutes': category_acl['thread_edit_time']})
  405. can_edit_thread = return_boolean(allow_edit_thread)
  406. def allow_see_post(user, target):
  407. if target.is_moderated:
  408. category_acl = user.acl['categories'].get(target.category_id, {})
  409. if not category_acl.get('can_review_moderated_content'):
  410. if user.is_anonymous() or user.pk != target.poster_id:
  411. raise Http404()
  412. can_see_post = return_boolean(allow_see_post)
  413. def allow_edit_post(user, target):
  414. if user.is_anonymous():
  415. raise PermissionDenied(_("You have to sign in to edit posts."))
  416. category_acl = target.category.acl
  417. if not category_acl['can_edit_posts']:
  418. raise PermissionDenied(_("You can't edit posts in this category."))
  419. if target.is_hidden and not can_unhide_post(user, target):
  420. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  421. if category_acl['can_edit_posts'] == 1:
  422. if target.poster_id != user.pk:
  423. raise PermissionDenied(
  424. _("You can't edit other users posts in this category."))
  425. if not category_acl['can_close_threads']:
  426. if target.category.is_closed:
  427. raise PermissionDenied(
  428. _("This category is closed. You can't edit posts in it."))
  429. if target.thread.is_closed:
  430. raise PermissionDenied(
  431. _("This thread is closed. You can't edit posts in it."))
  432. if target.is_protected and not category_acl['can_protect_posts']:
  433. raise PermissionDenied(
  434. _("This post is protected. You can't edit it."))
  435. if not has_time_to_edit_post(user, target):
  436. message = ungettext("You can't edit posts that are "
  437. "older than %(minutes)s minute.",
  438. "You can't edit posts that are "
  439. "older than %(minutes)s minutes.",
  440. category_acl['post_edit_time'])
  441. raise PermissionDenied(
  442. message % {'minutes': category_acl['post_edit_time']})
  443. can_edit_post = return_boolean(allow_edit_post)
  444. def allow_unhide_post(user, target):
  445. if user.is_anonymous():
  446. raise PermissionDenied(_("You have to sign in to reveal posts."))
  447. category_acl = target.category.acl
  448. if not category_acl['can_hide_posts']:
  449. if not category_acl['can_hide_own_posts']:
  450. raise PermissionDenied(_("You can't reveal posts in this category."))
  451. if user.id != target.poster_id:
  452. raise PermissionDenied(
  453. _("You can't reveal other users posts in this category."))
  454. if not category_acl['can_close_threads']:
  455. if target.category.is_closed:
  456. raise PermissionDenied(_("This category is closed. You can't "
  457. "reveal posts in it."))
  458. if target.thread.is_closed:
  459. raise PermissionDenied(_("This thread is closed. You can't "
  460. "reveal posts in it."))
  461. if target.is_protected and not category_acl['can_protect_posts']:
  462. raise PermissionDenied(
  463. _("This post is protected. You can't reveal it."))
  464. if has_time_to_edit_post(user, target):
  465. message = ungettext("You can't reveal posts that are "
  466. "older than %(minutes)s minute.",
  467. "You can't reveal posts that are "
  468. "older than %(minutes)s minutes.",
  469. category_acl['post_edit_time'])
  470. raise PermissionDenied(
  471. message % {'minutes': category_acl['post_edit_time']})
  472. if target.id == target.thread.first_post_id:
  473. raise PermissionDenied(_("You can't reveal thread's first post."))
  474. if not target.is_hidden:
  475. raise PermissionDenied(_("Only hidden posts can be revealed."))
  476. can_unhide_post = return_boolean(allow_unhide_post)
  477. def allow_hide_post(user, target):
  478. if user.is_anonymous():
  479. raise PermissionDenied(_("You have to sign in to hide posts."))
  480. category_acl = target.category.acl
  481. if not category_acl['can_hide_posts']:
  482. if not category_acl['can_hide_own_posts']:
  483. raise PermissionDenied(_("You can't hide posts in this category."))
  484. if user.id != target.poster_id:
  485. raise PermissionDenied(
  486. _("You can't hide other users posts in this category."))
  487. if not category_acl['can_close_threads']:
  488. if target.category.is_closed:
  489. raise PermissionDenied(_("This category is closed. You can't "
  490. "hide posts in it."))
  491. if target.thread.is_closed:
  492. raise PermissionDenied(_("This thread is closed. You can't "
  493. "hide posts in it."))
  494. if target.is_protected and not category_acl['can_protect_posts']:
  495. raise PermissionDenied(
  496. _("This post is protected. You can't hide it."))
  497. if has_time_to_edit_post(user, target):
  498. message = ungettext("You can't hide posts that are "
  499. "older than %(minutes)s minute.",
  500. "You can't hide posts that are "
  501. "older than %(minutes)s minutes.",
  502. category_acl['post_edit_time'])
  503. raise PermissionDenied(
  504. message % {'minutes': category_acl['post_edit_time']})
  505. if target.id == target.thread.first_post_id:
  506. raise PermissionDenied(_("You can't hide thread's first post."))
  507. if target.is_hidden:
  508. raise PermissionDenied(_("Only visible posts can be hidden."))
  509. can_hide_post = return_boolean(allow_hide_post)
  510. def allow_delete_post(user, target):
  511. if user.is_anonymous():
  512. raise PermissionDenied(_("You have to sign in to delete posts."))
  513. category_acl = target.category.acl
  514. if category_acl['can_hide_posts'] != 2:
  515. if not category_acl['can_hide_own_posts'] != 2:
  516. raise PermissionDenied(
  517. _("You can't delete posts in this category."))
  518. if user.id != target.poster_id:
  519. raise PermissionDenied(
  520. _("You can't delete other users posts in this category."))
  521. if not category_acl['can_close_threads']:
  522. if target.category.is_closed:
  523. raise PermissionDenied(_("This category 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 category_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. category_acl['post_edit_time'])
  537. raise PermissionDenied(
  538. message % {'minutes': category_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. category_acl = user.acl['categories'].get(target.category_id, {})
  547. if user.is_anonymous() or user.pk != target.starter_id:
  548. return False
  549. if target.category.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. category_acl = user.acl['categories'].get(target.category_id, {})
  556. if category_acl.get('thread_edit_time'):
  557. diff = timezone.now() - target.started_on
  558. diff_minutes = int(diff.total_seconds() / 60)
  559. return diff_minutes < category_acl.get('thread_edit_time')
  560. else:
  561. return True
  562. def has_time_to_edit_post(user, target):
  563. category_acl = user.acl['categories'].get(target.category_id, {})
  564. if category_acl.get('post_edit_time'):
  565. diff = timezone.now() - target.posted_on
  566. diff_minutes = int(diff.total_seconds() / 60)
  567. return diff_minutes < category_acl.get('post_edit_time')
  568. else:
  569. return True
  570. """
  571. Queryset helpers
  572. """
  573. def exclude_invisible_category_threads(queryset, user, category):
  574. if user.is_authenticated():
  575. condition_author = Q(starter_id=user.id)
  576. can_mod = category.acl['can_review_moderated_content']
  577. can_hide = category.acl['can_hide_threads']
  578. if not can_mod and not can_hide:
  579. condition = Q(is_moderated=False) & Q(is_hidden=False)
  580. queryset = queryset.filter(condition_author | condition)
  581. elif not can_mod:
  582. condition = Q(is_moderated=False)
  583. queryset = queryset.filter(condition_author | condition)
  584. elif not can_hide:
  585. condition = Q(is_hidden=False)
  586. queryset = queryset.filter(condition_author | condition)
  587. else:
  588. if not category.acl['can_review_moderated_content']:
  589. queryset = queryset.filter(is_moderated=False)
  590. if not category.acl['can_hide_threads']:
  591. queryset = queryset.filter(is_hidden=False)
  592. return queryset
  593. def exclude_invisible_threads(user, categories, queryset):
  594. show_all = []
  595. show_accepted_visible = []
  596. show_accepted = []
  597. show_visible = []
  598. show_owned = []
  599. show_owned_visible = []
  600. for category in categories:
  601. add_acl(user, category)
  602. can_hide = category.acl['can_hide_threads']
  603. if category.acl['can_see_all_threads']:
  604. can_mod = category.acl['can_review_moderated_content']
  605. if can_mod and can_hide:
  606. show_all.append(category)
  607. elif user.is_authenticated():
  608. if not can_mod and not can_hide:
  609. show_accepted_visible.append(category)
  610. elif not can_mod:
  611. show_accepted.append(category)
  612. elif not can_hide:
  613. show_visible.append(category)
  614. else:
  615. show_accepted_visible.append(category)
  616. elif user.is_authenticated():
  617. if can_hide:
  618. show_owned.append(category)
  619. else:
  620. show_owned_visible.append(category)
  621. conditions = None
  622. if show_all:
  623. conditions = Q(category__in=show_all)
  624. if show_accepted_visible:
  625. if user.is_authenticated():
  626. condition = Q(
  627. Q(starter=user) | Q(is_moderated=False),
  628. category__in=show_accepted_visible,
  629. is_hidden=False,
  630. )
  631. else:
  632. condition = Q(
  633. category__in=show_accepted_visible,
  634. is_hidden=False,
  635. is_moderated=False,
  636. )
  637. if conditions:
  638. conditions = conditions | condition
  639. else:
  640. conditions = condition
  641. if show_accepted:
  642. condition = Q(
  643. Q(starter=user) | Q(is_moderated=False),
  644. category__in=show_accepted,
  645. )
  646. if conditions:
  647. conditions = conditions | condition
  648. else:
  649. conditions = condition
  650. if show_visible:
  651. condition = Q(category__in=show_visible, is_hidden=False)
  652. if conditions:
  653. conditions = conditions | condition
  654. else:
  655. conditions = condition
  656. if show_owned:
  657. condition = Q(category__in=show_owned, starter=user)
  658. if conditions:
  659. conditions = conditions | condition
  660. else:
  661. conditions = condition
  662. if show_owned_visible:
  663. condition = Q(
  664. category__in=show_owned_visible,
  665. starter=user,
  666. is_hidden=False,
  667. )
  668. if conditions:
  669. conditions = conditions | condition
  670. else:
  671. conditions = condition
  672. if conditions:
  673. return Thread.objects.filter(conditions)
  674. else:
  675. return Thread.objects.none()
  676. def exclude_invisible_posts(queryset, user, category):
  677. if not category.acl['can_review_moderated_content']:
  678. if user.is_authenticated():
  679. condition_author = Q(poster_id=user.id)
  680. condition = Q(is_moderated=False)
  681. queryset = queryset.filter(condition_author | condition)
  682. else:
  683. queryset = queryset.filter(is_moderated=False)
  684. return queryset