threads.py 29 KB

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