threads.py 30 KB

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