threads.py 29 KB

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