threads.py 34 KB

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