permissions.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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.core import forms
  9. from misago.forums.models import Forum, RoleForumACL, ForumRole
  10. from misago.forums.permissions import get_forums_roles
  11. from misago.threads.models import Thread, Post, Event
  12. """
  13. Admin Permissions Form
  14. """
  15. class PermissionsForm(forms.Form):
  16. legend = _("Threads")
  17. can_see_all_threads = forms.TypedChoiceField(
  18. label=_("Can see threads"),
  19. coerce=int,
  20. initial=0,
  21. choices=((0, _("Started threads")), (1, _("All threads"))))
  22. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  23. can_reply_threads = forms.YesNoSwitch(label=_("Can reply to threads"))
  24. can_edit_threads = forms.TypedChoiceField(
  25. label=_("Can edit threads"),
  26. coerce=int,
  27. initial=0,
  28. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  29. can_hide_own_threads = forms.TypedChoiceField(
  30. label=_("Can hide own threads"),
  31. help_text=_("Only threads started within time limit and "
  32. "with no replies can be hidden."),
  33. coerce=int,
  34. initial=0,
  35. choices=(
  36. (0, _("No")),
  37. (1, _("Hide threads")),
  38. (2, _("Delete threads"))
  39. ))
  40. thread_edit_time = forms.IntegerField(
  41. label=_("Time limit for own threads edits, in minutes"),
  42. help_text=_("Enter 0 to don't limit time for editing own threads."),
  43. initial=0,
  44. min_value=0)
  45. can_hide_threads = forms.TypedChoiceField(
  46. label=_("Can hide all threads"),
  47. coerce=int,
  48. initial=0,
  49. choices=(
  50. (0, _("No")),
  51. (1, _("Hide threads")),
  52. (2, _("Delete threads"))
  53. ))
  54. can_edit_posts = forms.TypedChoiceField(
  55. label=_("Can edit posts"),
  56. coerce=int,
  57. initial=0,
  58. choices=((0, _("No")), (1, _("Own posts")), (2, _("All posts"))))
  59. can_hide_own_posts = forms.TypedChoiceField(
  60. label=_("Can hide own posts"),
  61. help_text=_("Only last posts to thread made within "
  62. "edit time limit can be hidden."),
  63. coerce=int,
  64. initial=0,
  65. choices=(
  66. (0, _("No")),
  67. (1, _("Hide posts")),
  68. (2, _("Delete posts"))
  69. ))
  70. post_edit_time = forms.IntegerField(
  71. label=_("Time limit for own post edits, in minutes"),
  72. help_text=_("Enter 0 to don't limit time for editing own posts."),
  73. initial=0,
  74. min_value=0)
  75. can_hide_posts = forms.TypedChoiceField(
  76. label=_("Can hide all posts"),
  77. coerce=int,
  78. initial=0,
  79. choices=(
  80. (0, _("No")),
  81. (1, _("Hide posts")),
  82. (2, _("Delete posts"))
  83. ))
  84. can_protect_posts = forms.YesNoSwitch(
  85. label=_("Can protect posts"),
  86. help_text=_("Only users with this permission "
  87. "can edit protected posts."))
  88. can_move_posts = forms.YesNoSwitch(
  89. label=_("Can move posts"))
  90. can_merge_posts = forms.YesNoSwitch(
  91. label=_("Can merge posts"))
  92. can_change_threads_labels = forms.TypedChoiceField(
  93. label=_("Can change threads labels"), coerce=int, initial=0,
  94. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads"))))
  95. can_pin_threads = forms.YesNoSwitch(
  96. label=_("Can pin threads"))
  97. can_close_threads = forms.YesNoSwitch(label=_("Can close threads"))
  98. can_move_threads = forms.YesNoSwitch(
  99. label=_("Can move threads"))
  100. can_merge_threads = forms.YesNoSwitch(
  101. label=_("Can merge threads"))
  102. can_split_threads = forms.YesNoSwitch(
  103. label=_("Can split threads"))
  104. can_review_moderated_content = forms.YesNoSwitch(
  105. label=_("Can review moderated content"),
  106. help_text=_("Will see and be able to accept moderated content."))
  107. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  108. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  109. can_hide_events = forms.TypedChoiceField(
  110. label=_("Can hide events"),
  111. coerce=int,
  112. initial=0,
  113. choices=(
  114. (0, _("No")),
  115. (1, _("Hide events")),
  116. (2, _("Delete events"))
  117. ))
  118. def change_permissions_form(role):
  119. if isinstance(role, ForumRole):
  120. return PermissionsForm
  121. else:
  122. return None
  123. """
  124. ACL Builder
  125. """
  126. def build_acl(acl, roles, key_name):
  127. acl['moderated_forums'] = []
  128. forums_roles = get_forums_roles(roles)
  129. for forum in Forum.objects.all_forums():
  130. forum_acl = acl['forums'].get(forum.pk, {'can_browse': 0})
  131. if forum_acl['can_browse']:
  132. acl['forums'][forum.pk] = build_forum_acl(
  133. forum_acl, forum, forums_roles, key_name)
  134. if acl['forums'][forum.pk]['can_review_moderated_content']:
  135. acl['moderated_forums'].append(forum.pk)
  136. return acl
  137. def build_forum_acl(acl, forum, forums_roles, key_name):
  138. forum_roles = forums_roles.get(forum.pk, [])
  139. final_acl = {
  140. 'can_see_all_threads': 0,
  141. 'can_start_threads': 0,
  142. 'can_reply_threads': 0,
  143. 'can_edit_threads': 0,
  144. 'can_edit_posts': 0,
  145. 'can_hide_own_threads': 0,
  146. 'can_hide_own_posts': 0,
  147. 'thread_edit_time': 0,
  148. 'post_edit_time': 0,
  149. 'can_hide_threads': 0,
  150. 'can_hide_posts': 0,
  151. 'can_protect_posts': 0,
  152. 'can_move_posts': 0,
  153. 'can_merge_posts': 0,
  154. 'can_change_threads_labels': 0,
  155. 'can_pin_threads': 0,
  156. 'can_close_threads': 0,
  157. 'can_move_threads': 0,
  158. 'can_merge_threads': 0,
  159. 'can_split_threads': 0,
  160. 'can_review_moderated_content': 0,
  161. 'can_report_content': 0,
  162. 'can_see_reports': 0,
  163. 'can_hide_events': 0,
  164. }
  165. final_acl.update(acl)
  166. algebra.sum_acls(final_acl, roles=forum_roles, key=key_name,
  167. can_see_all_threads=algebra.greater,
  168. can_start_threads=algebra.greater,
  169. can_reply_threads=algebra.greater,
  170. can_edit_threads=algebra.greater,
  171. can_edit_posts=algebra.greater,
  172. can_hide_threads=algebra.greater,
  173. can_hide_posts=algebra.greater,
  174. can_hide_own_threads=algebra.greater,
  175. can_hide_own_posts=algebra.greater,
  176. thread_edit_time=algebra.greater_or_zero,
  177. post_edit_time=algebra.greater_or_zero,
  178. can_protect_posts=algebra.greater,
  179. can_move_posts=algebra.greater,
  180. can_merge_posts=algebra.greater,
  181. can_change_threads_labels=algebra.greater,
  182. can_pin_threads=algebra.greater,
  183. can_close_threads=algebra.greater,
  184. can_move_threads=algebra.greater,
  185. can_merge_threads=algebra.greater,
  186. can_split_threads=algebra.greater,
  187. can_review_moderated_content=algebra.greater,
  188. can_report_content=algebra.greater,
  189. can_see_reports=algebra.greater,
  190. can_hide_events=algebra.greater,
  191. )
  192. return final_acl
  193. """
  194. ACL's for targets
  195. """
  196. def add_acl_to_target(user, target):
  197. if isinstance(target, Forum):
  198. add_acl_to_forum(user, target)
  199. if isinstance(target, Thread):
  200. add_acl_to_thread(user, target)
  201. if isinstance(target, Post):
  202. add_acl_to_post(user, target)
  203. if isinstance(target, Event):
  204. add_acl_to_event(user, target)
  205. def add_acl_to_forum(user, forum):
  206. forum_acl = user.acl['forums'].get(forum.pk, {})
  207. forum.acl.update({
  208. 'can_see_all_threads': 0,
  209. 'can_start_threads': 0,
  210. 'can_reply_threads': 0,
  211. 'can_edit_threads': 0,
  212. 'can_edit_posts': 0,
  213. 'can_hide_own_threads': 0,
  214. 'can_hide_own_posts': 0,
  215. 'thread_edit_time': 0,
  216. 'post_edit_time': 0,
  217. 'can_hide_threads': 0,
  218. 'can_hide_posts': 0,
  219. 'can_protect_posts': 0,
  220. 'can_move_posts': 0,
  221. 'can_merge_posts': 0,
  222. 'can_change_threads_labels': 0,
  223. 'can_pin_threads': 0,
  224. 'can_close_threads': 0,
  225. 'can_move_threads': 0,
  226. 'can_merge_threads': 0,
  227. 'can_split_threads': 0,
  228. 'can_review_moderated_content': 0,
  229. 'can_report_content': 0,
  230. 'can_see_reports': 0,
  231. 'can_hide_events': 0,
  232. })
  233. algebra.sum_acls(forum.acl, acls=[forum_acl],
  234. can_see_all_threads=algebra.greater)
  235. if user.is_authenticated():
  236. algebra.sum_acls(forum.acl, acls=[forum_acl],
  237. can_start_threads=algebra.greater,
  238. can_reply_threads=algebra.greater,
  239. can_edit_threads=algebra.greater,
  240. can_edit_posts=algebra.greater,
  241. can_hide_threads=algebra.greater,
  242. can_hide_posts=algebra.greater,
  243. can_hide_own_threads=algebra.greater,
  244. can_hide_own_posts=algebra.greater,
  245. thread_edit_time=algebra.greater_or_zero,
  246. post_edit_time=algebra.greater_or_zero,
  247. can_protect_posts=algebra.greater,
  248. can_move_posts=algebra.greater,
  249. can_merge_posts=algebra.greater,
  250. can_change_threads_labels=algebra.greater,
  251. can_pin_threads=algebra.greater,
  252. can_close_threads=algebra.greater,
  253. can_move_threads=algebra.greater,
  254. can_merge_threads=algebra.greater,
  255. can_split_threads=algebra.greater,
  256. can_review_moderated_content=algebra.greater,
  257. can_report_content=algebra.greater,
  258. can_see_reports=algebra.greater,
  259. can_hide_events=algebra.greater,
  260. )
  261. forum.acl['can_see_own_threads'] = not forum.acl['can_see_all_threads']
  262. def add_acl_to_thread(user, thread):
  263. forum_acl = user.acl['forums'].get(thread.forum_id, {})
  264. thread.acl.update({
  265. 'can_reply': can_reply_thread(user, thread),
  266. 'can_edit': can_edit_thread(user, thread),
  267. 'can_hide': forum_acl.get('can_hide_threads'),
  268. 'can_change_label': forum_acl.get('can_change_threads_labels') == 2,
  269. 'can_pin': forum_acl.get('can_pin_threads'),
  270. 'can_close': forum_acl.get('can_close_threads'),
  271. 'can_move': forum_acl.get('can_move_threads'),
  272. 'can_review': forum_acl.get('can_review_moderated_content'),
  273. 'can_report': forum_acl.get('can_report_content'),
  274. 'can_see_reports': forum_acl.get('can_see_reports')
  275. })
  276. if can_change_owned_thread(user, thread):
  277. if not forum_acl.get('can_close_threads'):
  278. thread_is_protected = thread.is_closed or thread.forum.is_closed
  279. else:
  280. thread_is_protected = False
  281. if not thread_is_protected:
  282. if not thread.acl['can_change_label']:
  283. can_change_label = forum_acl.get('can_change_threads_labels')
  284. thread.acl['can_change_label'] = can_change_label == 1
  285. if not thread.acl['can_hide']:
  286. if not thread.replies:
  287. can_hide_thread = forum_acl.get('can_hide_own_threads')
  288. thread.acl['can_hide'] = can_hide_thread
  289. def add_acl_to_post(user, post):
  290. forum_acl = user.acl['forums'].get(post.forum_id, {})
  291. post.acl.update({
  292. 'can_reply': can_reply_thread(user, post.thread),
  293. 'can_edit': can_edit_post(user, post),
  294. 'can_see_hidden': forum_acl.get('can_hide_posts'),
  295. 'can_unhide': can_unhide_post(user, post),
  296. 'can_hide': can_hide_post(user, post),
  297. 'can_delete': can_delete_post(user, post),
  298. 'can_protect': forum_acl.get('can_protect_posts'),
  299. 'can_report': forum_acl.get('can_report_content'),
  300. 'can_see_reports': forum_acl.get('can_see_reports'),
  301. 'can_approve': forum_acl.get('can_review_moderated_content'),
  302. })
  303. if not post.is_moderated:
  304. post.acl['can_approve'] = False
  305. if not post.acl['can_see_hidden']:
  306. if user.is_authenticated() and user.id == post.poster_id:
  307. post.acl['can_see_hidden'] = True
  308. else:
  309. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  310. def add_acl_to_event(user, event):
  311. forum_acl = user.acl['forums'].get(event.forum_id, {})
  312. can_hide_events = forum_acl.get('can_hide_events', 0)
  313. event.acl['can_hide'] = can_hide_events > 0
  314. event.acl['can_delete'] = can_hide_events == 2
  315. """
  316. ACL tests
  317. """
  318. def allow_see_thread(user, target):
  319. forum_acl = user.acl['forums'].get(target.forum_id, {})
  320. if not forum_acl.get('can_browse'):
  321. raise Http404()
  322. if user.is_anonymous() or user.pk != target.starter_id:
  323. if not forum_acl.get('can_see_all_threads'):
  324. raise Http404()
  325. if target.is_moderated:
  326. if not forum_acl.get('can_review_moderated_content'):
  327. raise Http404()
  328. if target.is_hidden and not forum_acl.get('can_hide_threads'):
  329. raise Http404()
  330. can_see_thread = return_boolean(allow_see_thread)
  331. def allow_start_thread(user, target):
  332. if user.is_anonymous():
  333. raise PermissionDenied(_("You have to sign in to start threads."))
  334. if target.is_closed and not target.acl['can_close_threads']:
  335. raise PermissionDenied(
  336. _("This forum is closed. You can't start new threads in it."))
  337. if not user.acl['forums'].get(target.id, {'can_start_threads': False}):
  338. raise PermissionDenied(_("You don't have permission to start "
  339. "new threads in this forum."))
  340. can_start_thread = return_boolean(allow_start_thread)
  341. def allow_reply_thread(user, target):
  342. if user.is_anonymous():
  343. raise PermissionDenied(_("You have to sign in to reply threads."))
  344. forum_acl = target.forum.acl
  345. if not forum_acl['can_close_threads']:
  346. if target.forum.is_closed:
  347. raise PermissionDenied(
  348. _("This forum is closed. You can't reply to threads in it."))
  349. if target.is_closed:
  350. raise PermissionDenied(
  351. _("You can't reply to closed threads in this forum."))
  352. if not forum_acl['can_reply_threads']:
  353. raise PermissionDenied(_("You can't reply to threads in this forum."))
  354. can_reply_thread = return_boolean(allow_reply_thread)
  355. def allow_edit_thread(user, target):
  356. if user.is_anonymous():
  357. raise PermissionDenied(_("You have to sign in to edit threads."))
  358. forum_acl = target.forum.acl
  359. if not forum_acl['can_edit_threads']:
  360. raise PermissionDenied(_("You can't edit threads in this forum."))
  361. if forum_acl['can_edit_threads'] == 1:
  362. if target.starter_id != user.pk:
  363. raise PermissionDenied(
  364. _("You can't edit other users threads in this forum."))
  365. if not forum_acl['can_close_threads']:
  366. if target.forum.is_closed:
  367. raise PermissionDenied(
  368. _("This forum is closed. You can't edit threads in it."))
  369. if target.is_closed:
  370. raise PermissionDenied(
  371. _("You can't edit closed threads in this forum."))
  372. if not has_time_to_edit_thread(user, target):
  373. message = ungettext("You can't edit threads that are "
  374. "older than %(minutes)s minute.",
  375. "You can't edit threads that are "
  376. "older than %(minutes)s minutes.",
  377. forum_acl['thread_edit_time'])
  378. raise PermissionDenied(
  379. message % {'minutes': forum_acl['thread_edit_time']})
  380. can_edit_thread = return_boolean(allow_edit_thread)
  381. def allow_see_post(user, target):
  382. if target.is_moderated:
  383. forum_acl = user.acl['forums'].get(target.forum_id, {})
  384. if not forum_acl.get('can_review_moderated_content'):
  385. if user.is_anonymous() or user.pk != target.poster_id:
  386. raise Http404()
  387. can_see_post = return_boolean(allow_see_post)
  388. def allow_edit_post(user, target):
  389. if user.is_anonymous():
  390. raise PermissionDenied(_("You have to sign in to edit posts."))
  391. forum_acl = target.forum.acl
  392. if not forum_acl['can_edit_posts']:
  393. raise PermissionDenied(_("You can't edit posts in this forum."))
  394. if target.is_hidden and not can_unhide_post(user, target):
  395. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  396. if forum_acl['can_edit_posts'] == 1:
  397. if target.poster_id != user.pk:
  398. raise PermissionDenied(
  399. _("You can't edit other users posts in this forum."))
  400. if not forum_acl['can_close_threads']:
  401. if target.forum.is_closed:
  402. raise PermissionDenied(
  403. _("This forum is closed. You can't edit posts in it."))
  404. if target.thread.is_closed:
  405. raise PermissionDenied(
  406. _("This thread is closed. You can't edit posts in it."))
  407. if target.is_protected and not forum_acl['can_protect_posts']:
  408. raise PermissionDenied(
  409. _("This post is protected. You can't edit it."))
  410. if not has_time_to_edit_post(user, target):
  411. message = ungettext("You can't edit posts that are "
  412. "older than %(minutes)s minute.",
  413. "You can't edit posts that are "
  414. "older than %(minutes)s minutes.",
  415. forum_acl['post_edit_time'])
  416. raise PermissionDenied(
  417. message % {'minutes': forum_acl['post_edit_time']})
  418. can_edit_post = return_boolean(allow_edit_post)
  419. def allow_unhide_post(user, target):
  420. if user.is_anonymous():
  421. raise PermissionDenied(_("You have to sign in to reveal posts."))
  422. forum_acl = target.forum.acl
  423. if not forum_acl['can_hide_posts']:
  424. if not forum_acl['can_hide_own_posts']:
  425. raise PermissionDenied(_("You can't reveal posts in this forum."))
  426. if user.id != target.poster_id:
  427. raise PermissionDenied(
  428. _("You can't reveal other users posts in this forum."))
  429. if not forum_acl['can_close_threads']:
  430. if target.forum.is_closed:
  431. raise PermissionDenied(_("This forum is closed. You can't "
  432. "reveal posts in it."))
  433. if target.thread.is_closed:
  434. raise PermissionDenied(_("This thread is closed. You can't "
  435. "reveal posts in it."))
  436. if target.is_protected and not forum_acl['can_protect_posts']:
  437. raise PermissionDenied(
  438. _("This post is protected. You can't reveal it."))
  439. if has_time_to_edit_post(user, target):
  440. message = ungettext("You can't reveal posts that are "
  441. "older than %(minutes)s minute.",
  442. "You can't reveal posts that are "
  443. "older than %(minutes)s minutes.",
  444. forum_acl['post_edit_time'])
  445. raise PermissionDenied(
  446. message % {'minutes': forum_acl['post_edit_time']})
  447. if target.id == target.thread.first_post_id:
  448. raise PermissionDenied(_("You can't reveal thread's first post."))
  449. if not target.is_hidden:
  450. raise PermissionDenied(_("Only hidden posts can be revealed."))
  451. can_unhide_post = return_boolean(allow_unhide_post)
  452. def allow_hide_post(user, target):
  453. if user.is_anonymous():
  454. raise PermissionDenied(_("You have to sign in to hide posts."))
  455. forum_acl = target.forum.acl
  456. if not forum_acl['can_hide_posts']:
  457. if not forum_acl['can_hide_own_posts']:
  458. raise PermissionDenied(_("You can't hide posts in this forum."))
  459. if user.id != target.poster_id:
  460. raise PermissionDenied(
  461. _("You can't hide other users posts in this forum."))
  462. if not forum_acl['can_close_threads']:
  463. if target.forum.is_closed:
  464. raise PermissionDenied(_("This forum is closed. You can't "
  465. "hide posts in it."))
  466. if target.thread.is_closed:
  467. raise PermissionDenied(_("This thread is closed. You can't "
  468. "hide posts in it."))
  469. if target.is_protected and not forum_acl['can_protect_posts']:
  470. raise PermissionDenied(
  471. _("This post is protected. You can't hide it."))
  472. if has_time_to_edit_post(user, target):
  473. message = ungettext("You can't hide posts that are "
  474. "older than %(minutes)s minute.",
  475. "You can't hide posts that are "
  476. "older than %(minutes)s minutes.",
  477. forum_acl['post_edit_time'])
  478. raise PermissionDenied(
  479. message % {'minutes': forum_acl['post_edit_time']})
  480. if target.id == target.thread.first_post_id:
  481. raise PermissionDenied(_("You can't hide thread's first post."))
  482. if target.is_hidden:
  483. raise PermissionDenied(_("Only visible posts can be hidden."))
  484. can_hide_post = return_boolean(allow_hide_post)
  485. def allow_delete_post(user, target):
  486. if user.is_anonymous():
  487. raise PermissionDenied(_("You have to sign in to delete posts."))
  488. forum_acl = target.forum.acl
  489. if forum_acl['can_hide_posts'] != 2:
  490. if not forum_acl['can_hide_own_posts'] != 2:
  491. raise PermissionDenied(_("You can't delete posts in this forum."))
  492. if user.id != target.poster_id:
  493. raise PermissionDenied(
  494. _("You can't delete other users posts in this forum."))
  495. if not forum_acl['can_close_threads']:
  496. if target.forum.is_closed:
  497. raise PermissionDenied(_("This forum is closed. You can't "
  498. "delete posts from it."))
  499. if target.thread.is_closed:
  500. raise PermissionDenied(_("This thread is closed. You can't "
  501. "delete posts from it."))
  502. if target.is_protected and not forum_acl['can_protect_posts']:
  503. raise PermissionDenied(
  504. _("This post is protected. You can't delete it."))
  505. if has_time_to_edit_post(user, target):
  506. message = ungettext("You can't delete posts that are "
  507. "older than %(minutes)s minute.",
  508. "You can't delete posts that are "
  509. "older than %(minutes)s minutes.",
  510. forum_acl['post_edit_time'])
  511. raise PermissionDenied(
  512. message % {'minutes': forum_acl['post_edit_time']})
  513. if target.id == target.thread.first_post_id:
  514. raise PermissionDenied(_("You can't delete thread's first post."))
  515. can_delete_post = return_boolean(allow_delete_post)
  516. """
  517. Permission check helpers
  518. """
  519. def can_change_owned_thread(user, target):
  520. forum_acl = user.acl['forums'].get(target.forum_id, {})
  521. if user.is_anonymous() or user.pk != target.starter_id:
  522. return False
  523. if target.forum.is_closed or target.is_closed:
  524. return False
  525. if target.first_post.is_protected:
  526. return False
  527. return has_time_to_edit_thread(user, target)
  528. def has_time_to_edit_thread(user, target):
  529. forum_acl = user.acl['forums'].get(target.forum_id, {})
  530. if forum_acl.get('thread_edit_time'):
  531. diff = timezone.now() - target.started_on
  532. diff_minutes = int(diff.total_seconds() / 60)
  533. return diff_minutes < forum_acl.get('thread_edit_time')
  534. else:
  535. return True
  536. def has_time_to_edit_post(user, target):
  537. forum_acl = user.acl['forums'].get(target.forum_id, {})
  538. if forum_acl.get('post_edit_time'):
  539. diff = timezone.now() - target.posted_on
  540. diff_minutes = int(diff.total_seconds() / 60)
  541. return diff_minutes < forum_acl.get('post_edit_time')
  542. else:
  543. return True
  544. """
  545. Queryset helpers
  546. """
  547. def exclude_invisible_threads(queryset, user, forum=None):
  548. if forum:
  549. return exclude_invisible_forum_threads(queryset, user, forum)
  550. else:
  551. return exclude_all_invisible_threads(queryset, user)
  552. def exclude_invisible_forum_threads(queryset, user, forum):
  553. if user.is_authenticated():
  554. condition_author = Q(starter_id=user.id)
  555. can_mod = forum.acl['can_review_moderated_content']
  556. can_hide = forum.acl['can_hide_threads']
  557. if not can_mod and not can_hide:
  558. condition = Q(is_moderated=False) & Q(is_hidden=False)
  559. queryset = queryset.filter(condition_author | condition)
  560. elif not can_mod:
  561. condition = Q(is_moderated=False)
  562. queryset = queryset.filter(condition_author | condition)
  563. elif not can_hide:
  564. condition = Q(is_hidden=False)
  565. queryset = queryset.filter(condition_author | condition)
  566. else:
  567. if not forum.acl['can_review_moderated_content']:
  568. queryset = queryset.filter(is_moderated=False)
  569. if not forum.acl['can_hide_threads']:
  570. queryset = queryset.filter(is_hidden=False)
  571. return queryset
  572. def exclude_all_invisible_threads(queryset, user):
  573. forums_in = []
  574. conditions = None
  575. for forum in Forum.objects.all_forums():
  576. add_acl(user, forum)
  577. condition_forum = Q(forum=forum)
  578. condition_author = Q(starter_id=user.id)
  579. # can see all threads?
  580. if forum.acl['can_see_all_threads']:
  581. can_mod = forum.acl['can_review_moderated_content']
  582. can_hide = forum.acl['can_hide_threads']
  583. if not can_mod or not can_hide:
  584. if not can_mod and not can_hide:
  585. condition = Q(is_moderated=False) & Q(is_hidden=False)
  586. elif not can_mod:
  587. condition = Q(is_moderated=False)
  588. elif not can_hide:
  589. condition = Q(is_hidden=False)
  590. visibility_condition = condition_author | condition
  591. visibility_condition = condition_forum & visibility_condition
  592. else:
  593. # user can see everything so don't bother with rest of routine
  594. forums_in.append(forum.pk)
  595. continue
  596. else:
  597. # show all threads in forum made by user
  598. visibility_condition = condition_forum & condition_author
  599. if conditions:
  600. conditions = conditions | visibility_condition
  601. else:
  602. conditions = visibility_condition
  603. if conditions and forums_in:
  604. return queryset.filter(Q(forum_id__in=forums_in) | conditions)
  605. elif conditions:
  606. return queryset.filter(conditions)
  607. elif forums_in:
  608. return queryset.filter(forum_id__in=forums_in)
  609. else:
  610. return Thread.objects.none()
  611. def exclude_invisible_posts(queryset, user, forum):
  612. if not forum.acl['can_review_moderated_content']:
  613. if user.is_authenticated():
  614. condition_author = Q(poster_id=user.id)
  615. condition = Q(is_moderated=False)
  616. queryset = queryset.filter(condition_author | condition)
  617. else:
  618. queryset = queryset.filter(is_moderated=False)
  619. return queryset