threads.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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.acl.models import Role
  9. from misago.categories.models import Category, RoleCategoryACL, CategoryRole
  10. from misago.categories.permissions import get_categories_roles
  11. from misago.core import forms
  12. from misago.threads.models import Thread, Post, Event
  13. __all__ = [
  14. 'register_with',
  15. 'allow_see_thread',
  16. 'can_see_thread',
  17. 'allow_start_thread',
  18. 'can_start_thread',
  19. 'allow_reply_thread',
  20. 'can_reply_thread',
  21. 'allow_edit_thread',
  22. 'can_edit_thread',
  23. 'allow_see_post',
  24. 'can_see_post',
  25. 'allow_edit_post',
  26. 'can_edit_post',
  27. 'allow_unhide_post',
  28. 'can_unhide_post',
  29. 'allow_hide_post',
  30. 'can_hide_post',
  31. 'allow_delete_post',
  32. 'can_delete_post',
  33. 'exclude_invisible_threads',
  34. 'exclude_invisible_posts'
  35. ]
  36. """
  37. Admin Permissions Forms
  38. """
  39. class RolePermissionsForm(forms.Form):
  40. legend = _("Threads")
  41. can_see_unapproved_content_lists = forms.YesNoSwitch(
  42. label=_("Can see unapproved content list"),
  43. help_text=_('Allows access to "unapproved" tab on threads lists for '
  44. "easy listing of threads that are unapproved or contain "
  45. "unapproved posts. Despite the tab being available on all "
  46. "threads lists, it will only display threads belonging to "
  47. "categories in which the user has permission to approve "
  48. "content.")
  49. )
  50. can_see_reported_content_lists = forms.YesNoSwitch(
  51. label=_("Can see reported content list"),
  52. help_text=_('Allows access to "reported" tab on threads lists for '
  53. "easy listing of threads that contain reported posts. "
  54. "Despite the tab being available on all categories "
  55. "threads lists, it will only display threads belonging to "
  56. "categories in which the user has permission to see posts "
  57. "reports.")
  58. )
  59. class CategoryPermissionsForm(forms.Form):
  60. legend = _("Threads")
  61. can_see_all_threads = forms.TypedChoiceField(
  62. label=_("Can see threads"),
  63. coerce=int,
  64. initial=0,
  65. choices=((0, _("Started threads")), (1, _("All threads")))
  66. )
  67. can_start_threads = forms.YesNoSwitch(label=_("Can start threads"))
  68. can_reply_threads = forms.YesNoSwitch(label=_("Can reply to threads"))
  69. can_edit_threads = forms.TypedChoiceField(
  70. label=_("Can edit threads"),
  71. coerce=int,
  72. initial=0,
  73. choices=((0, _("No")), (1, _("Own threads")), (2, _("All threads")))
  74. )
  75. can_hide_own_threads = forms.TypedChoiceField(
  76. label=_("Can hide own threads"),
  77. help_text=_("Only threads started within time limit and "
  78. "with no replies can be hidden."),
  79. coerce=int,
  80. initial=0,
  81. choices=(
  82. (0, _("No")),
  83. (1, _("Hide threads")),
  84. (2, _("Delete threads"))
  85. )
  86. )
  87. thread_edit_time = forms.IntegerField(
  88. label=_("Time limit for own threads edits, in minutes"),
  89. help_text=_("Enter 0 to don't limit time for editing own threads."),
  90. initial=0,
  91. min_value=0
  92. )
  93. can_hide_threads = forms.TypedChoiceField(
  94. label=_("Can hide all threads"),
  95. coerce=int,
  96. initial=0,
  97. choices=(
  98. (0, _("No")),
  99. (1, _("Hide threads")),
  100. (2, _("Delete threads"))
  101. )
  102. )
  103. can_edit_posts = forms.TypedChoiceField(
  104. label=_("Can edit posts"),
  105. coerce=int,
  106. initial=0,
  107. choices=((0, _("No")), (1, _("Own posts")), (2, _("All posts")))
  108. )
  109. can_hide_own_posts = forms.TypedChoiceField(
  110. label=_("Can hide own posts"),
  111. help_text=_("Only last posts to thread made within "
  112. "edit time limit can be hidden."),
  113. coerce=int,
  114. initial=0,
  115. choices=(
  116. (0, _("No")),
  117. (1, _("Hide posts")),
  118. (2, _("Delete posts"))
  119. )
  120. )
  121. post_edit_time = forms.IntegerField(
  122. label=_("Time limit for own post edits, in minutes"),
  123. help_text=_("Enter 0 to don't limit time for editing own posts."),
  124. initial=0,
  125. min_value=0
  126. )
  127. can_hide_posts = forms.TypedChoiceField(
  128. label=_("Can hide all posts"),
  129. coerce=int,
  130. initial=0,
  131. choices=(
  132. (0, _("No")),
  133. (1, _("Hide posts")),
  134. (2, _("Delete posts"))
  135. )
  136. )
  137. can_protect_posts = forms.YesNoSwitch(
  138. label=_("Can protect posts"),
  139. help_text=_("Only users with this permission can edit protected posts.")
  140. )
  141. can_move_posts = forms.YesNoSwitch(label=_("Can move posts"))
  142. can_merge_posts = forms.YesNoSwitch(label=_("Can merge posts"))
  143. can_pin_threads = forms.TypedChoiceField(
  144. label=_("Can pin threads"),
  145. coerce=int,
  146. initial=0,
  147. choices=(
  148. (0, _("No")),
  149. (1, _("Locally")),
  150. (2, _("Globally"))
  151. )
  152. )
  153. can_close_threads = forms.YesNoSwitch(label=_("Can close threads"))
  154. can_move_threads = forms.YesNoSwitch(label=_("Can move threads"))
  155. can_merge_threads = forms.YesNoSwitch(label=_("Can merge threads"))
  156. can_split_threads = forms.YesNoSwitch(label=_("Can split threads"))
  157. can_approve_content = forms.YesNoSwitch(
  158. label=_("Can approve content"),
  159. help_text=_("Will be able to see and approve unapproved content.")
  160. )
  161. can_report_content = forms.YesNoSwitch(label=_("Can report posts"))
  162. can_see_reports = forms.YesNoSwitch(label=_("Can see reports"))
  163. can_hide_events = forms.TypedChoiceField(
  164. label=_("Can hide events"),
  165. coerce=int,
  166. initial=0,
  167. choices=(
  168. (0, _("No")),
  169. (1, _("Hide events")),
  170. (2, _("Delete events"))
  171. )
  172. )
  173. def change_permissions_form(role):
  174. if isinstance(role, Role) and role.special_role != 'anonymous':
  175. return RolePermissionsForm
  176. elif isinstance(role, CategoryRole):
  177. return CategoryPermissionsForm
  178. else:
  179. return None
  180. """
  181. ACL Builder
  182. """
  183. def build_acl(acl, roles, key_name):
  184. acl['can_see_unapproved_content_lists'] = False
  185. acl['can_see_reported_content_lists'] = False
  186. acl['can_approve_content'] = []
  187. acl['can_see_reports'] = []
  188. acl = algebra.sum_acls(acl, roles=roles, key=key_name,
  189. can_see_unapproved_content_lists=algebra.greater,
  190. can_see_reported_content_lists=algebra.greater
  191. )
  192. categories_roles = get_categories_roles(roles)
  193. categories = list(Category.objects.all_categories(include_root=True))
  194. approve_in_categories = []
  195. for category in categories:
  196. category_acl = acl['categories'].get(category.pk, {'can_browse': 0})
  197. if category_acl['can_browse']:
  198. acl['categories'][category.pk] = build_category_acl(
  199. category_acl, category, categories_roles, key_name)
  200. if acl['categories'][category.pk].get('can_see_reports'):
  201. acl['can_see_reports'].append(category.pk)
  202. if acl['categories'][category.pk].get('can_approve_content'):
  203. approve_in_categories.append(category)
  204. return acl
  205. def build_category_acl(acl, category, categories_roles, key_name):
  206. category_roles = categories_roles.get(category.pk, [])
  207. final_acl = {
  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_pin_threads': 0,
  223. 'can_close_threads': 0,
  224. 'can_move_threads': 0,
  225. 'can_merge_threads': 0,
  226. 'can_split_threads': 0,
  227. 'can_approve_content': 0,
  228. 'can_report_content': 0,
  229. 'can_see_reports': 0,
  230. 'can_hide_events': 0,
  231. }
  232. final_acl.update(acl)
  233. algebra.sum_acls(final_acl, roles=category_roles, key=key_name,
  234. can_see_all_threads=algebra.greater,
  235. can_start_threads=algebra.greater,
  236. can_reply_threads=algebra.greater,
  237. can_edit_threads=algebra.greater,
  238. can_edit_posts=algebra.greater,
  239. can_hide_threads=algebra.greater,
  240. can_hide_posts=algebra.greater,
  241. can_hide_own_threads=algebra.greater,
  242. can_hide_own_posts=algebra.greater,
  243. thread_edit_time=algebra.greater_or_zero,
  244. post_edit_time=algebra.greater_or_zero,
  245. can_protect_posts=algebra.greater,
  246. can_move_posts=algebra.greater,
  247. can_merge_posts=algebra.greater,
  248. can_pin_threads=algebra.greater,
  249. can_close_threads=algebra.greater,
  250. can_move_threads=algebra.greater,
  251. can_merge_threads=algebra.greater,
  252. can_split_threads=algebra.greater,
  253. can_approve_content=algebra.greater,
  254. can_report_content=algebra.greater,
  255. can_see_reports=algebra.greater,
  256. can_hide_events=algebra.greater,
  257. )
  258. return final_acl
  259. """
  260. ACL's for targets
  261. """
  262. def add_acl_to_category(user, category):
  263. category_acl = user.acl['categories'].get(category.pk, {})
  264. category.acl.update({
  265. 'can_see_all_threads': 0,
  266. 'can_start_threads': 0,
  267. 'can_reply_threads': 0,
  268. 'can_edit_threads': 0,
  269. 'can_edit_posts': 0,
  270. 'can_hide_own_threads': 0,
  271. 'can_hide_own_posts': 0,
  272. 'thread_edit_time': 0,
  273. 'post_edit_time': 0,
  274. 'can_hide_threads': 0,
  275. 'can_hide_posts': 0,
  276. 'can_protect_posts': 0,
  277. 'can_move_posts': 0,
  278. 'can_merge_posts': 0,
  279. 'can_pin_threads': 0,
  280. 'can_close_threads': 0,
  281. 'can_move_threads': 0,
  282. 'can_merge_threads': 0,
  283. 'can_split_threads': 0,
  284. 'can_approve_content': 0,
  285. 'can_report_content': 0,
  286. 'can_see_reports': 0,
  287. 'can_hide_events': 0,
  288. })
  289. algebra.sum_acls(category.acl, acls=[category_acl],
  290. can_see_all_threads=algebra.greater)
  291. if user.is_authenticated():
  292. algebra.sum_acls(category.acl, acls=[category_acl],
  293. can_start_threads=algebra.greater,
  294. can_reply_threads=algebra.greater,
  295. can_edit_threads=algebra.greater,
  296. can_edit_posts=algebra.greater,
  297. can_hide_threads=algebra.greater,
  298. can_hide_posts=algebra.greater,
  299. can_hide_own_threads=algebra.greater,
  300. can_hide_own_posts=algebra.greater,
  301. thread_edit_time=algebra.greater_or_zero,
  302. post_edit_time=algebra.greater_or_zero,
  303. can_protect_posts=algebra.greater,
  304. can_move_posts=algebra.greater,
  305. can_merge_posts=algebra.greater,
  306. can_pin_threads=algebra.greater,
  307. can_close_threads=algebra.greater,
  308. can_move_threads=algebra.greater,
  309. can_merge_threads=algebra.greater,
  310. can_split_threads=algebra.greater,
  311. can_approve_content=algebra.greater,
  312. can_report_content=algebra.greater,
  313. can_see_reports=algebra.greater,
  314. can_hide_events=algebra.greater,
  315. )
  316. category.acl['can_see_own_threads'] = not category.acl['can_see_all_threads']
  317. def add_acl_to_thread(user, thread):
  318. category_acl = user.acl['categories'].get(thread.category_id, {})
  319. thread.acl.update({
  320. 'can_reply': can_reply_thread(user, thread),
  321. 'can_edit': can_edit_thread(user, thread),
  322. 'can_hide': category_acl.get('can_hide_threads', False),
  323. 'can_pin': category_acl.get('can_pin_threads', 0),
  324. 'can_close': category_acl.get('can_close_threads', False),
  325. 'can_move': category_acl.get('can_move_threads', False),
  326. 'can_approve': category_acl.get('can_approve_content', False),
  327. 'can_report': category_acl.get('can_report_content', False),
  328. 'can_see_reports': category_acl.get('can_see_reports', False),
  329. })
  330. if can_change_owned_thread(user, thread):
  331. if not category_acl.get('can_close_threads'):
  332. thread_is_protected = thread.is_closed or thread.category.is_closed
  333. else:
  334. thread_is_protected = False
  335. if not thread_is_protected and not thread.acl['can_hide']:
  336. if not thread.replies:
  337. can_hide_thread = category_acl.get('can_hide_own_threads')
  338. thread.acl['can_hide'] = can_hide_thread
  339. def add_acl_to_post(user, post):
  340. category_acl = user.acl['categories'].get(post.category_id, {})
  341. post.acl.update({
  342. 'can_reply': can_reply_thread(user, post.thread),
  343. 'can_edit': can_edit_post(user, post),
  344. 'can_see_hidden': category_acl.get('can_hide_posts'),
  345. 'can_unhide': can_unhide_post(user, post),
  346. 'can_hide': can_hide_post(user, post),
  347. 'can_delete': can_delete_post(user, post),
  348. 'can_protect': category_acl.get('can_protect_posts', False),
  349. 'can_report': category_acl.get('can_report_content', False),
  350. 'can_see_reports': category_acl.get('can_see_reports', False),
  351. 'can_approve': category_acl.get('can_approve_content', False),
  352. })
  353. if not post.is_unapproved:
  354. post.acl['can_approve'] = False
  355. if not post.acl['can_see_hidden']:
  356. if user.is_authenticated() and user.id == post.poster_id:
  357. post.acl['can_see_hidden'] = True
  358. else:
  359. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  360. def add_acl_to_event(user, event):
  361. category_acl = user.acl['categories'].get(event.category_id, {})
  362. can_hide_events = category_acl.get('can_hide_events', 0)
  363. event.acl['can_hide'] = can_hide_events > 0
  364. event.acl['can_delete'] = can_hide_events == 2
  365. def register_with(registry):
  366. registry.acl_annotator(Category, add_acl_to_category)
  367. registry.acl_annotator(Thread, add_acl_to_thread)
  368. registry.acl_annotator(Post, add_acl_to_post)
  369. registry.acl_annotator(Event, add_acl_to_event)
  370. """
  371. ACL tests
  372. """
  373. def allow_see_thread(user, target):
  374. category_acl = user.acl['categories'].get(target.category_id, {})
  375. if not category_acl.get('can_browse'):
  376. raise Http404()
  377. if user.is_anonymous() or user.pk != target.starter_id:
  378. if not category_acl.get('can_see_all_threads'):
  379. raise Http404()
  380. if target.is_unapproved:
  381. if not category_acl.get('can_approve_content'):
  382. raise Http404()
  383. if target.is_hidden and not category_acl.get('can_hide_threads'):
  384. raise Http404()
  385. can_see_thread = return_boolean(allow_see_thread)
  386. def allow_start_thread(user, target):
  387. if user.is_anonymous():
  388. raise PermissionDenied(_("You have to sign in to start threads."))
  389. if target.is_closed and not target.acl['can_close_threads']:
  390. raise PermissionDenied(
  391. _("This category is closed. You can't start new threads in it."))
  392. if not user.acl['categories'].get(target.id, {'can_start_threads': False}):
  393. raise PermissionDenied(_("You don't have permission to start "
  394. "new threads in this category."))
  395. can_start_thread = return_boolean(allow_start_thread)
  396. def allow_reply_thread(user, target):
  397. if user.is_anonymous():
  398. raise PermissionDenied(_("You have to sign in to reply threads."))
  399. category_acl = target.category.acl
  400. if not category_acl['can_close_threads']:
  401. if target.category.is_closed:
  402. raise PermissionDenied(
  403. _("This category is closed. You can't reply to threads in it."))
  404. if target.is_closed:
  405. raise PermissionDenied(
  406. _("You can't reply to closed threads in this category."))
  407. if not category_acl['can_reply_threads']:
  408. raise PermissionDenied(_("You can't reply to threads in this category."))
  409. can_reply_thread = return_boolean(allow_reply_thread)
  410. def allow_edit_thread(user, target):
  411. if user.is_anonymous():
  412. raise PermissionDenied(_("You have to sign in to edit threads."))
  413. category_acl = target.category.acl
  414. if not category_acl['can_edit_threads']:
  415. raise PermissionDenied(_("You can't edit threads in this category."))
  416. if category_acl['can_edit_threads'] == 1:
  417. if target.starter_id != user.pk:
  418. raise PermissionDenied(
  419. _("You can't edit other users threads in this category."))
  420. if not category_acl['can_close_threads']:
  421. if target.category.is_closed:
  422. raise PermissionDenied(
  423. _("This category is closed. You can't edit threads in it."))
  424. if target.is_closed:
  425. raise PermissionDenied(
  426. _("You can't edit closed threads in this category."))
  427. if not has_time_to_edit_thread(user, target):
  428. message = ungettext("You can't edit threads that are "
  429. "older than %(minutes)s minute.",
  430. "You can't edit threads that are "
  431. "older than %(minutes)s minutes.",
  432. category_acl['thread_edit_time'])
  433. raise PermissionDenied(
  434. message % {'minutes': category_acl['thread_edit_time']})
  435. can_edit_thread = return_boolean(allow_edit_thread)
  436. def allow_see_post(user, target):
  437. if target.is_unapproved:
  438. category_acl = user.acl['categories'].get(target.category_id, {})
  439. if not category_acl.get('can_approve_content'):
  440. if user.is_anonymous() or user.pk != target.poster_id:
  441. raise Http404()
  442. can_see_post = return_boolean(allow_see_post)
  443. def allow_edit_post(user, target):
  444. if user.is_anonymous():
  445. raise PermissionDenied(_("You have to sign in to edit posts."))
  446. category_acl = target.category.acl
  447. if not category_acl['can_edit_posts']:
  448. raise PermissionDenied(_("You can't edit posts in this category."))
  449. if target.is_hidden and not can_unhide_post(user, target):
  450. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  451. if category_acl['can_edit_posts'] == 1:
  452. if target.poster_id != user.pk:
  453. raise PermissionDenied(
  454. _("You can't edit other users posts in this category."))
  455. if not category_acl['can_close_threads']:
  456. if target.category.is_closed:
  457. raise PermissionDenied(
  458. _("This category is closed. You can't edit posts in it."))
  459. if target.thread.is_closed:
  460. raise PermissionDenied(
  461. _("This thread is closed. You can't edit posts in it."))
  462. if target.is_protected and not category_acl['can_protect_posts']:
  463. raise PermissionDenied(
  464. _("This post is protected. You can't edit it."))
  465. if not has_time_to_edit_post(user, target):
  466. message = ungettext("You can't edit posts that are "
  467. "older than %(minutes)s minute.",
  468. "You can't edit posts that are "
  469. "older than %(minutes)s minutes.",
  470. category_acl['post_edit_time'])
  471. raise PermissionDenied(
  472. message % {'minutes': category_acl['post_edit_time']})
  473. can_edit_post = return_boolean(allow_edit_post)
  474. def allow_unhide_post(user, target):
  475. if user.is_anonymous():
  476. raise PermissionDenied(_("You have to sign in to reveal posts."))
  477. category_acl = target.category.acl
  478. if not category_acl['can_hide_posts']:
  479. if not category_acl['can_hide_own_posts']:
  480. raise PermissionDenied(_("You can't reveal posts in this category."))
  481. if user.id != target.poster_id:
  482. raise PermissionDenied(
  483. _("You can't reveal other users posts in this category."))
  484. if not category_acl['can_close_threads']:
  485. if target.category.is_closed:
  486. raise PermissionDenied(_("This category is closed. You can't "
  487. "reveal posts in it."))
  488. if target.thread.is_closed:
  489. raise PermissionDenied(_("This thread is closed. You can't "
  490. "reveal posts in it."))
  491. if target.is_protected and not category_acl['can_protect_posts']:
  492. raise PermissionDenied(
  493. _("This post is protected. You can't reveal it."))
  494. if has_time_to_edit_post(user, target):
  495. message = ungettext("You can't reveal posts that are "
  496. "older than %(minutes)s minute.",
  497. "You can't reveal posts that are "
  498. "older than %(minutes)s minutes.",
  499. category_acl['post_edit_time'])
  500. raise PermissionDenied(
  501. message % {'minutes': category_acl['post_edit_time']})
  502. if target.id == target.thread.first_post_id:
  503. raise PermissionDenied(_("You can't reveal thread's first post."))
  504. if not target.is_hidden:
  505. raise PermissionDenied(_("Only hidden posts can be revealed."))
  506. can_unhide_post = return_boolean(allow_unhide_post)
  507. def allow_hide_post(user, target):
  508. if user.is_anonymous():
  509. raise PermissionDenied(_("You have to sign in to hide posts."))
  510. category_acl = target.category.acl
  511. if not category_acl['can_hide_posts']:
  512. if not category_acl['can_hide_own_posts']:
  513. raise PermissionDenied(_("You can't hide posts in this category."))
  514. if user.id != target.poster_id:
  515. raise PermissionDenied(
  516. _("You can't hide other users posts in this category."))
  517. if not category_acl['can_close_threads']:
  518. if target.category.is_closed:
  519. raise PermissionDenied(_("This category is closed. You can't "
  520. "hide posts in it."))
  521. if target.thread.is_closed:
  522. raise PermissionDenied(_("This thread is closed. You can't "
  523. "hide posts in it."))
  524. if target.is_protected and not category_acl['can_protect_posts']:
  525. raise PermissionDenied(
  526. _("This post is protected. You can't hide it."))
  527. if has_time_to_edit_post(user, target):
  528. message = ungettext("You can't hide posts that are "
  529. "older than %(minutes)s minute.",
  530. "You can't hide posts that are "
  531. "older than %(minutes)s minutes.",
  532. category_acl['post_edit_time'])
  533. raise PermissionDenied(
  534. message % {'minutes': category_acl['post_edit_time']})
  535. if target.id == target.thread.first_post_id:
  536. raise PermissionDenied(_("You can't hide thread's first post."))
  537. if target.is_hidden:
  538. raise PermissionDenied(_("Only visible posts can be hidden."))
  539. can_hide_post = return_boolean(allow_hide_post)
  540. def allow_delete_post(user, target):
  541. if user.is_anonymous():
  542. raise PermissionDenied(_("You have to sign in to delete posts."))
  543. category_acl = target.category.acl
  544. if category_acl['can_hide_posts'] != 2:
  545. if not category_acl['can_hide_own_posts'] != 2:
  546. raise PermissionDenied(
  547. _("You can't delete posts in this category."))
  548. if user.id != target.poster_id:
  549. raise PermissionDenied(
  550. _("You can't delete other users posts in this category."))
  551. if not category_acl['can_close_threads']:
  552. if target.category.is_closed:
  553. raise PermissionDenied(_("This category is closed. You can't "
  554. "delete posts from it."))
  555. if target.thread.is_closed:
  556. raise PermissionDenied(_("This thread is closed. You can't "
  557. "delete posts from it."))
  558. if target.is_protected and not category_acl['can_protect_posts']:
  559. raise PermissionDenied(
  560. _("This post is protected. You can't delete it."))
  561. if has_time_to_edit_post(user, target):
  562. message = ungettext("You can't delete posts that are "
  563. "older than %(minutes)s minute.",
  564. "You can't delete posts that are "
  565. "older than %(minutes)s minutes.",
  566. category_acl['post_edit_time'])
  567. raise PermissionDenied(
  568. message % {'minutes': category_acl['post_edit_time']})
  569. if target.id == target.thread.first_post_id:
  570. raise PermissionDenied(_("You can't delete thread's first post."))
  571. can_delete_post = return_boolean(allow_delete_post)
  572. """
  573. Permission check helpers
  574. """
  575. def can_change_owned_thread(user, target):
  576. category_acl = user.acl['categories'].get(target.category_id, {})
  577. if user.is_anonymous() or user.pk != target.starter_id:
  578. return False
  579. if target.category.is_closed or target.is_closed:
  580. return False
  581. if target.first_post.is_protected:
  582. return False
  583. return has_time_to_edit_thread(user, target)
  584. def has_time_to_edit_thread(user, target):
  585. category_acl = user.acl['categories'].get(target.category_id, {})
  586. if category_acl.get('thread_edit_time'):
  587. diff = timezone.now() - target.started_on
  588. diff_minutes = int(diff.total_seconds() / 60)
  589. return diff_minutes < category_acl.get('thread_edit_time')
  590. else:
  591. return True
  592. def has_time_to_edit_post(user, target):
  593. category_acl = user.acl['categories'].get(target.category_id, {})
  594. if category_acl.get('post_edit_time'):
  595. diff = timezone.now() - target.posted_on
  596. diff_minutes = int(diff.total_seconds() / 60)
  597. return diff_minutes < category_acl.get('post_edit_time')
  598. else:
  599. return True
  600. """
  601. Queryset helpers
  602. """
  603. def exclude_invisible_category_threads(queryset, user, category):
  604. if user.is_authenticated():
  605. condition_author = Q(starter_id=user.id)
  606. can_mod = category.acl['can_approve_content']
  607. can_hide = category.acl['can_hide_threads']
  608. if not can_mod and not can_hide:
  609. condition = Q(is_unapproved=False) & Q(is_hidden=False)
  610. queryset = queryset.filter(condition_author | condition)
  611. elif not can_mod:
  612. condition = Q(is_unapproved=False)
  613. queryset = queryset.filter(condition_author | condition)
  614. elif not can_hide:
  615. condition = Q(is_hidden=False)
  616. queryset = queryset.filter(condition_author | condition)
  617. else:
  618. if not category.acl['can_approve_content']:
  619. queryset = queryset.filter(is_unapproved=False)
  620. if not category.acl['can_hide_threads']:
  621. queryset = queryset.filter(is_hidden=False)
  622. return queryset
  623. def exclude_invisible_threads(user, categories, queryset):
  624. show_all = []
  625. show_accepted_visible = []
  626. show_accepted = []
  627. show_visible = []
  628. show_owned = []
  629. show_owned_visible = []
  630. for category in categories:
  631. add_acl(user, category)
  632. if not (category.acl['can_see'] and category.acl['can_browse']):
  633. continue
  634. can_hide = category.acl['can_hide_threads']
  635. if category.acl['can_see_all_threads']:
  636. can_mod = category.acl['can_approve_content']
  637. if can_mod and can_hide:
  638. show_all.append(category)
  639. elif user.is_authenticated():
  640. if not can_mod and not can_hide:
  641. show_accepted_visible.append(category)
  642. elif not can_mod:
  643. show_accepted.append(category)
  644. elif not can_hide:
  645. show_visible.append(category)
  646. else:
  647. show_accepted_visible.append(category)
  648. elif user.is_authenticated():
  649. if can_hide:
  650. show_owned.append(category)
  651. else:
  652. show_owned_visible.append(category)
  653. conditions = None
  654. if show_all:
  655. conditions = Q(category__in=show_all)
  656. if show_accepted_visible:
  657. if user.is_authenticated():
  658. condition = Q(
  659. Q(starter=user) | Q(is_unapproved=False),
  660. category__in=show_accepted_visible,
  661. is_hidden=False,
  662. )
  663. else:
  664. condition = Q(
  665. category__in=show_accepted_visible,
  666. is_hidden=False,
  667. is_unapproved=False,
  668. )
  669. if conditions:
  670. conditions = conditions | condition
  671. else:
  672. conditions = condition
  673. if show_accepted:
  674. condition = Q(
  675. Q(starter=user) | Q(is_unapproved=False),
  676. category__in=show_accepted,
  677. )
  678. if conditions:
  679. conditions = conditions | condition
  680. else:
  681. conditions = condition
  682. if show_visible:
  683. condition = Q(category__in=show_visible, is_hidden=False)
  684. if conditions:
  685. conditions = conditions | condition
  686. else:
  687. conditions = condition
  688. if show_owned:
  689. condition = Q(category__in=show_owned, starter=user)
  690. if conditions:
  691. conditions = conditions | condition
  692. else:
  693. conditions = condition
  694. if show_owned_visible:
  695. condition = Q(
  696. category__in=show_owned_visible,
  697. starter=user,
  698. is_hidden=False,
  699. )
  700. if conditions:
  701. conditions = conditions | condition
  702. else:
  703. conditions = condition
  704. if conditions:
  705. return Thread.objects.filter(conditions)
  706. else:
  707. return Thread.objects.none()
  708. def exclude_invisible_posts(queryset, user, category):
  709. if not category.acl['can_approve_content']:
  710. if user.is_authenticated():
  711. condition_author = Q(poster_id=user.id)
  712. condition = Q(is_unapproved=False)
  713. queryset = queryset.filter(condition_author | condition)
  714. else:
  715. queryset = queryset.filter(is_unapproved=False)
  716. return queryset