threads.py 33 KB

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