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