threads.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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.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_announce_threads': 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_announce_threads=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_announce_threads': 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_announce_threads=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_announce': category_acl.get('can_announce_threads'),
  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 and not thread.acl['can_hide']:
  312. if not thread.replies:
  313. can_hide_thread = category_acl.get('can_hide_own_threads')
  314. thread.acl['can_hide'] = can_hide_thread
  315. def add_acl_to_post(user, post):
  316. category_acl = user.acl['categories'].get(post.category_id, {})
  317. post.acl.update({
  318. 'can_reply': can_reply_thread(user, post.thread),
  319. 'can_edit': can_edit_post(user, post),
  320. 'can_see_hidden': category_acl.get('can_hide_posts'),
  321. 'can_unhide': can_unhide_post(user, post),
  322. 'can_hide': can_hide_post(user, post),
  323. 'can_delete': can_delete_post(user, post),
  324. 'can_protect': category_acl.get('can_protect_posts'),
  325. 'can_report': category_acl.get('can_report_content'),
  326. 'can_see_reports': category_acl.get('can_see_reports'),
  327. 'can_approve': category_acl.get('can_review_moderated_content'),
  328. })
  329. if not post.is_moderated:
  330. post.acl['can_approve'] = False
  331. if not post.acl['can_see_hidden']:
  332. if user.is_authenticated() and user.id == post.poster_id:
  333. post.acl['can_see_hidden'] = True
  334. else:
  335. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  336. def add_acl_to_event(user, event):
  337. category_acl = user.acl['categories'].get(event.category_id, {})
  338. can_hide_events = category_acl.get('can_hide_events', 0)
  339. event.acl['can_hide'] = can_hide_events > 0
  340. event.acl['can_delete'] = can_hide_events == 2
  341. def register_with(registry):
  342. registry.acl_annotator(Category, add_acl_to_category)
  343. registry.acl_annotator(Thread, add_acl_to_thread)
  344. registry.acl_annotator(Post, add_acl_to_post)
  345. registry.acl_annotator(Event, add_acl_to_event)
  346. """
  347. ACL tests
  348. """
  349. def allow_see_thread(user, target):
  350. category_acl = user.acl['categories'].get(target.category_id, {})
  351. if not category_acl.get('can_browse'):
  352. raise Http404()
  353. if user.is_anonymous() or user.pk != target.starter_id:
  354. if not category_acl.get('can_see_all_threads'):
  355. raise Http404()
  356. if target.is_moderated:
  357. if not category_acl.get('can_review_moderated_content'):
  358. raise Http404()
  359. if target.is_hidden and not category_acl.get('can_hide_threads'):
  360. raise Http404()
  361. can_see_thread = return_boolean(allow_see_thread)
  362. def allow_start_thread(user, target):
  363. if user.is_anonymous():
  364. raise PermissionDenied(_("You have to sign in to start threads."))
  365. if target.is_closed and not target.acl['can_close_threads']:
  366. raise PermissionDenied(
  367. _("This category is closed. You can't start new threads in it."))
  368. if not user.acl['categories'].get(target.id, {'can_start_threads': False}):
  369. raise PermissionDenied(_("You don't have permission to start "
  370. "new threads in this category."))
  371. can_start_thread = return_boolean(allow_start_thread)
  372. def allow_reply_thread(user, target):
  373. if user.is_anonymous():
  374. raise PermissionDenied(_("You have to sign in to reply threads."))
  375. category_acl = target.category.acl
  376. if not category_acl['can_close_threads']:
  377. if target.category.is_closed:
  378. raise PermissionDenied(
  379. _("This category is closed. You can't reply to threads in it."))
  380. if target.is_closed:
  381. raise PermissionDenied(
  382. _("You can't reply to closed threads in this category."))
  383. if not category_acl['can_reply_threads']:
  384. raise PermissionDenied(_("You can't reply to threads in this category."))
  385. can_reply_thread = return_boolean(allow_reply_thread)
  386. def allow_edit_thread(user, target):
  387. if user.is_anonymous():
  388. raise PermissionDenied(_("You have to sign in to edit threads."))
  389. category_acl = target.category.acl
  390. if not category_acl['can_edit_threads']:
  391. raise PermissionDenied(_("You can't edit threads in this category."))
  392. if category_acl['can_edit_threads'] == 1:
  393. if target.starter_id != user.pk:
  394. raise PermissionDenied(
  395. _("You can't edit other users threads in this category."))
  396. if not category_acl['can_close_threads']:
  397. if target.category.is_closed:
  398. raise PermissionDenied(
  399. _("This category is closed. You can't edit threads in it."))
  400. if target.is_closed:
  401. raise PermissionDenied(
  402. _("You can't edit closed threads in this category."))
  403. if not has_time_to_edit_thread(user, target):
  404. message = ungettext("You can't edit threads that are "
  405. "older than %(minutes)s minute.",
  406. "You can't edit threads that are "
  407. "older than %(minutes)s minutes.",
  408. category_acl['thread_edit_time'])
  409. raise PermissionDenied(
  410. message % {'minutes': category_acl['thread_edit_time']})
  411. can_edit_thread = return_boolean(allow_edit_thread)
  412. def allow_see_post(user, target):
  413. if target.is_moderated:
  414. category_acl = user.acl['categories'].get(target.category_id, {})
  415. if not category_acl.get('can_review_moderated_content'):
  416. if user.is_anonymous() or user.pk != target.poster_id:
  417. raise Http404()
  418. can_see_post = return_boolean(allow_see_post)
  419. def allow_edit_post(user, target):
  420. if user.is_anonymous():
  421. raise PermissionDenied(_("You have to sign in to edit posts."))
  422. category_acl = target.category.acl
  423. if not category_acl['can_edit_posts']:
  424. raise PermissionDenied(_("You can't edit posts in this category."))
  425. if target.is_hidden and not can_unhide_post(user, target):
  426. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  427. if category_acl['can_edit_posts'] == 1:
  428. if target.poster_id != user.pk:
  429. raise PermissionDenied(
  430. _("You can't edit other users posts in this category."))
  431. if not category_acl['can_close_threads']:
  432. if target.category.is_closed:
  433. raise PermissionDenied(
  434. _("This category is closed. You can't edit posts in it."))
  435. if target.thread.is_closed:
  436. raise PermissionDenied(
  437. _("This thread is closed. You can't edit posts in it."))
  438. if target.is_protected and not category_acl['can_protect_posts']:
  439. raise PermissionDenied(
  440. _("This post is protected. You can't edit it."))
  441. if not has_time_to_edit_post(user, target):
  442. message = ungettext("You can't edit posts that are "
  443. "older than %(minutes)s minute.",
  444. "You can't edit posts that are "
  445. "older than %(minutes)s minutes.",
  446. category_acl['post_edit_time'])
  447. raise PermissionDenied(
  448. message % {'minutes': category_acl['post_edit_time']})
  449. can_edit_post = return_boolean(allow_edit_post)
  450. def allow_unhide_post(user, target):
  451. if user.is_anonymous():
  452. raise PermissionDenied(_("You have to sign in to reveal posts."))
  453. category_acl = target.category.acl
  454. if not category_acl['can_hide_posts']:
  455. if not category_acl['can_hide_own_posts']:
  456. raise PermissionDenied(_("You can't reveal posts in this category."))
  457. if user.id != target.poster_id:
  458. raise PermissionDenied(
  459. _("You can't reveal other users posts in this category."))
  460. if not category_acl['can_close_threads']:
  461. if target.category.is_closed:
  462. raise PermissionDenied(_("This category is closed. You can't "
  463. "reveal posts in it."))
  464. if target.thread.is_closed:
  465. raise PermissionDenied(_("This thread is closed. You can't "
  466. "reveal posts in it."))
  467. if target.is_protected and not category_acl['can_protect_posts']:
  468. raise PermissionDenied(
  469. _("This post is protected. You can't reveal it."))
  470. if has_time_to_edit_post(user, target):
  471. message = ungettext("You can't reveal posts that are "
  472. "older than %(minutes)s minute.",
  473. "You can't reveal posts that are "
  474. "older than %(minutes)s minutes.",
  475. category_acl['post_edit_time'])
  476. raise PermissionDenied(
  477. message % {'minutes': category_acl['post_edit_time']})
  478. if target.id == target.thread.first_post_id:
  479. raise PermissionDenied(_("You can't reveal thread's first post."))
  480. if not target.is_hidden:
  481. raise PermissionDenied(_("Only hidden posts can be revealed."))
  482. can_unhide_post = return_boolean(allow_unhide_post)
  483. def allow_hide_post(user, target):
  484. if user.is_anonymous():
  485. raise PermissionDenied(_("You have to sign in to hide posts."))
  486. category_acl = target.category.acl
  487. if not category_acl['can_hide_posts']:
  488. if not category_acl['can_hide_own_posts']:
  489. raise PermissionDenied(_("You can't hide posts in this category."))
  490. if user.id != target.poster_id:
  491. raise PermissionDenied(
  492. _("You can't hide other users posts in this category."))
  493. if not category_acl['can_close_threads']:
  494. if target.category.is_closed:
  495. raise PermissionDenied(_("This category is closed. You can't "
  496. "hide posts in it."))
  497. if target.thread.is_closed:
  498. raise PermissionDenied(_("This thread is closed. You can't "
  499. "hide posts in it."))
  500. if target.is_protected and not category_acl['can_protect_posts']:
  501. raise PermissionDenied(
  502. _("This post is protected. You can't hide it."))
  503. if has_time_to_edit_post(user, target):
  504. message = ungettext("You can't hide posts that are "
  505. "older than %(minutes)s minute.",
  506. "You can't hide posts that are "
  507. "older than %(minutes)s minutes.",
  508. category_acl['post_edit_time'])
  509. raise PermissionDenied(
  510. message % {'minutes': category_acl['post_edit_time']})
  511. if target.id == target.thread.first_post_id:
  512. raise PermissionDenied(_("You can't hide thread's first post."))
  513. if target.is_hidden:
  514. raise PermissionDenied(_("Only visible posts can be hidden."))
  515. can_hide_post = return_boolean(allow_hide_post)
  516. def allow_delete_post(user, target):
  517. if user.is_anonymous():
  518. raise PermissionDenied(_("You have to sign in to delete posts."))
  519. category_acl = target.category.acl
  520. if category_acl['can_hide_posts'] != 2:
  521. if not category_acl['can_hide_own_posts'] != 2:
  522. raise PermissionDenied(
  523. _("You can't delete posts in this category."))
  524. if user.id != target.poster_id:
  525. raise PermissionDenied(
  526. _("You can't delete other users posts in this category."))
  527. if not category_acl['can_close_threads']:
  528. if target.category.is_closed:
  529. raise PermissionDenied(_("This category is closed. You can't "
  530. "delete posts from it."))
  531. if target.thread.is_closed:
  532. raise PermissionDenied(_("This thread is closed. You can't "
  533. "delete posts from it."))
  534. if target.is_protected and not category_acl['can_protect_posts']:
  535. raise PermissionDenied(
  536. _("This post is protected. You can't delete it."))
  537. if has_time_to_edit_post(user, target):
  538. message = ungettext("You can't delete posts that are "
  539. "older than %(minutes)s minute.",
  540. "You can't delete posts that are "
  541. "older than %(minutes)s minutes.",
  542. category_acl['post_edit_time'])
  543. raise PermissionDenied(
  544. message % {'minutes': category_acl['post_edit_time']})
  545. if target.id == target.thread.first_post_id:
  546. raise PermissionDenied(_("You can't delete thread's first post."))
  547. can_delete_post = return_boolean(allow_delete_post)
  548. """
  549. Permission check helpers
  550. """
  551. def can_change_owned_thread(user, target):
  552. category_acl = user.acl['categories'].get(target.category_id, {})
  553. if user.is_anonymous() or user.pk != target.starter_id:
  554. return False
  555. if target.category.is_closed or target.is_closed:
  556. return False
  557. if target.first_post.is_protected:
  558. return False
  559. return has_time_to_edit_thread(user, target)
  560. def has_time_to_edit_thread(user, target):
  561. category_acl = user.acl['categories'].get(target.category_id, {})
  562. if category_acl.get('thread_edit_time'):
  563. diff = timezone.now() - target.started_on
  564. diff_minutes = int(diff.total_seconds() / 60)
  565. return diff_minutes < category_acl.get('thread_edit_time')
  566. else:
  567. return True
  568. def has_time_to_edit_post(user, target):
  569. category_acl = user.acl['categories'].get(target.category_id, {})
  570. if category_acl.get('post_edit_time'):
  571. diff = timezone.now() - target.posted_on
  572. diff_minutes = int(diff.total_seconds() / 60)
  573. return diff_minutes < category_acl.get('post_edit_time')
  574. else:
  575. return True
  576. """
  577. Queryset helpers
  578. """
  579. def exclude_invisible_category_threads(queryset, user, category):
  580. if user.is_authenticated():
  581. condition_author = Q(starter_id=user.id)
  582. can_mod = category.acl['can_review_moderated_content']
  583. can_hide = category.acl['can_hide_threads']
  584. if not can_mod and not can_hide:
  585. condition = Q(is_moderated=False) & Q(is_hidden=False)
  586. queryset = queryset.filter(condition_author | condition)
  587. elif not can_mod:
  588. condition = Q(is_moderated=False)
  589. queryset = queryset.filter(condition_author | condition)
  590. elif not can_hide:
  591. condition = Q(is_hidden=False)
  592. queryset = queryset.filter(condition_author | condition)
  593. else:
  594. if not category.acl['can_review_moderated_content']:
  595. queryset = queryset.filter(is_moderated=False)
  596. if not category.acl['can_hide_threads']:
  597. queryset = queryset.filter(is_hidden=False)
  598. return queryset
  599. def exclude_invisible_threads(user, categories, queryset):
  600. show_all = []
  601. show_accepted_visible = []
  602. show_accepted = []
  603. show_visible = []
  604. show_owned = []
  605. show_owned_visible = []
  606. for category in categories:
  607. add_acl(user, category)
  608. can_hide = category.acl['can_hide_threads']
  609. if category.acl['can_see_all_threads']:
  610. can_mod = category.acl['can_review_moderated_content']
  611. if can_mod and can_hide:
  612. show_all.append(category)
  613. elif user.is_authenticated():
  614. if not can_mod and not can_hide:
  615. show_accepted_visible.append(category)
  616. elif not can_mod:
  617. show_accepted.append(category)
  618. elif not can_hide:
  619. show_visible.append(category)
  620. else:
  621. show_accepted_visible.append(category)
  622. elif user.is_authenticated():
  623. if can_hide:
  624. show_owned.append(category)
  625. else:
  626. show_owned_visible.append(category)
  627. conditions = None
  628. if show_all:
  629. conditions = Q(category__in=show_all)
  630. if show_accepted_visible:
  631. if user.is_authenticated():
  632. condition = Q(
  633. Q(starter=user) | Q(is_moderated=False),
  634. category__in=show_accepted_visible,
  635. is_hidden=False,
  636. )
  637. else:
  638. condition = Q(
  639. category__in=show_accepted_visible,
  640. is_hidden=False,
  641. is_moderated=False,
  642. )
  643. if conditions:
  644. conditions = conditions | condition
  645. else:
  646. conditions = condition
  647. if show_accepted:
  648. condition = Q(
  649. Q(starter=user) | Q(is_moderated=False),
  650. category__in=show_accepted,
  651. )
  652. if conditions:
  653. conditions = conditions | condition
  654. else:
  655. conditions = condition
  656. if show_visible:
  657. condition = Q(category__in=show_visible, is_hidden=False)
  658. if conditions:
  659. conditions = conditions | condition
  660. else:
  661. conditions = condition
  662. if show_owned:
  663. condition = Q(category__in=show_owned, starter=user)
  664. if conditions:
  665. conditions = conditions | condition
  666. else:
  667. conditions = condition
  668. if show_owned_visible:
  669. condition = Q(
  670. category__in=show_owned_visible,
  671. starter=user,
  672. is_hidden=False,
  673. )
  674. if conditions:
  675. conditions = conditions | condition
  676. else:
  677. conditions = condition
  678. if conditions:
  679. return Thread.objects.filter(conditions)
  680. else:
  681. return Thread.objects.none()
  682. def exclude_invisible_posts(queryset, user, category):
  683. if not category.acl['can_review_moderated_content']:
  684. if user.is_authenticated():
  685. condition_author = Q(poster_id=user.id)
  686. condition = Q(is_moderated=False)
  687. queryset = queryset.filter(condition_author | condition)
  688. else:
  689. queryset = queryset.filter(is_moderated=False)
  690. return queryset