threads.py 29 KB

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