threads.py 29 KB

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