threads.py 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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. def change_permissions_form(role):
  215. if isinstance(role, Role) and role.special_role != 'anonymous':
  216. return RolePermissionsForm
  217. elif isinstance(role, CategoryRole):
  218. return CategoryPermissionsForm
  219. else:
  220. return None
  221. def build_acl(acl, roles, key_name):
  222. acl.update({
  223. 'can_see_unapproved_content_lists': False,
  224. 'can_see_reported_content_lists': False,
  225. 'can_omit_flood_protection': False,
  226. 'can_approve_content': [],
  227. 'can_see_reports': [],
  228. })
  229. acl = algebra.sum_acls(
  230. acl,
  231. roles=roles,
  232. key=key_name,
  233. can_see_unapproved_content_lists=algebra.greater,
  234. can_see_reported_content_lists=algebra.greater,
  235. can_omit_flood_protection=algebra.greater
  236. )
  237. categories_roles = get_categories_roles(roles)
  238. categories = list(Category.objects.all_categories(include_root=True))
  239. for category in categories:
  240. category_acl = acl['categories'].get(category.pk, {'can_browse': 0})
  241. if category_acl['can_browse']:
  242. category_acl = acl['categories'][category.pk] = build_category_acl(
  243. category_acl, category, categories_roles, key_name
  244. )
  245. if category_acl.get('can_approve_content'):
  246. acl['can_approve_content'].append(category.pk)
  247. if category_acl.get('can_see_reports'):
  248. acl['can_see_reports'].append(category.pk)
  249. return acl
  250. def build_category_acl(acl, category, categories_roles, key_name):
  251. category_roles = categories_roles.get(category.pk, [])
  252. final_acl = {
  253. 'can_see_all_threads': 0,
  254. 'can_start_threads': 0,
  255. 'can_reply_threads': 0,
  256. 'can_edit_threads': 0,
  257. 'can_edit_posts': 0,
  258. 'can_hide_own_threads': 0,
  259. 'can_hide_own_posts': 0,
  260. 'thread_edit_time': 0,
  261. 'post_edit_time': 0,
  262. 'can_hide_threads': 0,
  263. 'can_hide_posts': 0,
  264. 'can_protect_posts': 0,
  265. 'can_move_posts': 0,
  266. 'can_merge_posts': 0,
  267. 'can_pin_threads': 0,
  268. 'can_close_threads': 0,
  269. 'can_move_threads': 0,
  270. 'can_merge_threads': 0,
  271. 'can_approve_content': 0,
  272. 'can_report_content': 0,
  273. 'can_see_reports': 0,
  274. 'can_see_posts_likes': 0,
  275. 'can_like_posts': 0,
  276. 'can_hide_events': 0,
  277. }
  278. final_acl.update(acl)
  279. algebra.sum_acls(
  280. final_acl,
  281. roles=category_roles,
  282. key=key_name,
  283. can_see_all_threads=algebra.greater,
  284. can_start_threads=algebra.greater,
  285. can_reply_threads=algebra.greater,
  286. can_edit_threads=algebra.greater,
  287. can_edit_posts=algebra.greater,
  288. can_hide_threads=algebra.greater,
  289. can_hide_posts=algebra.greater,
  290. can_hide_own_threads=algebra.greater,
  291. can_hide_own_posts=algebra.greater,
  292. thread_edit_time=algebra.greater_or_zero,
  293. post_edit_time=algebra.greater_or_zero,
  294. can_protect_posts=algebra.greater,
  295. can_move_posts=algebra.greater,
  296. can_merge_posts=algebra.greater,
  297. can_pin_threads=algebra.greater,
  298. can_close_threads=algebra.greater,
  299. can_move_threads=algebra.greater,
  300. can_merge_threads=algebra.greater,
  301. can_approve_content=algebra.greater,
  302. can_report_content=algebra.greater,
  303. can_see_reports=algebra.greater,
  304. can_see_posts_likes=algebra.greater,
  305. can_like_posts=algebra.greater,
  306. can_hide_events=algebra.greater,
  307. )
  308. return final_acl
  309. def add_acl_to_category(user, category):
  310. category_acl = user.acl_cache['categories'].get(category.pk, {})
  311. category.acl.update({
  312. 'can_see_all_threads': 0,
  313. 'can_see_own_threads': 0,
  314. 'can_start_threads': 0,
  315. 'can_reply_threads': 0,
  316. 'can_edit_threads': 0,
  317. 'can_edit_posts': 0,
  318. 'can_hide_own_threads': 0,
  319. 'can_hide_own_posts': 0,
  320. 'thread_edit_time': 0,
  321. 'post_edit_time': 0,
  322. 'can_hide_threads': 0,
  323. 'can_hide_posts': 0,
  324. 'can_protect_posts': 0,
  325. 'can_move_posts': 0,
  326. 'can_merge_posts': 0,
  327. 'can_pin_threads': 0,
  328. 'can_close_threads': 0,
  329. 'can_move_threads': 0,
  330. 'can_merge_threads': 0,
  331. 'can_approve_content': 0,
  332. 'can_report_content': 0,
  333. 'can_see_reports': 0,
  334. 'can_see_posts_likes': 0,
  335. 'can_like_posts': 0,
  336. 'can_hide_events': 0,
  337. })
  338. algebra.sum_acls(
  339. category.acl,
  340. acls=[category_acl],
  341. can_see_all_threads=algebra.greater,
  342. can_see_posts_likes=algebra.greater,
  343. )
  344. if user.is_authenticated:
  345. algebra.sum_acls(
  346. category.acl,
  347. acls=[category_acl],
  348. can_start_threads=algebra.greater,
  349. can_reply_threads=algebra.greater,
  350. can_edit_threads=algebra.greater,
  351. can_edit_posts=algebra.greater,
  352. can_hide_threads=algebra.greater,
  353. can_hide_posts=algebra.greater,
  354. can_hide_own_threads=algebra.greater,
  355. can_hide_own_posts=algebra.greater,
  356. thread_edit_time=algebra.greater_or_zero,
  357. post_edit_time=algebra.greater_or_zero,
  358. can_protect_posts=algebra.greater,
  359. can_move_posts=algebra.greater,
  360. can_merge_posts=algebra.greater,
  361. can_pin_threads=algebra.greater,
  362. can_close_threads=algebra.greater,
  363. can_move_threads=algebra.greater,
  364. can_merge_threads=algebra.greater,
  365. can_approve_content=algebra.greater,
  366. can_report_content=algebra.greater,
  367. can_see_reports=algebra.greater,
  368. can_like_posts=algebra.greater,
  369. can_hide_events=algebra.greater,
  370. )
  371. category.acl['can_see_own_threads'] = not category.acl['can_see_all_threads']
  372. def add_acl_to_thread(user, thread):
  373. category_acl = user.acl_cache['categories'].get(thread.category_id, {})
  374. thread.acl.update({
  375. 'can_reply': can_reply_thread(user, thread),
  376. 'can_edit': can_edit_thread(user, thread),
  377. 'can_hide': category_acl.get('can_hide_threads', False),
  378. 'can_pin': 0,
  379. 'can_close': category_acl.get('can_close_threads', False),
  380. 'can_move': False,
  381. 'can_merge': False,
  382. 'can_move_posts': False,
  383. 'can_merge_posts': False,
  384. 'can_approve': category_acl.get('can_approve_content', False),
  385. 'can_see_reports': category_acl.get('can_see_reports', False),
  386. })
  387. if not category_acl.get('can_close_threads'):
  388. thread_is_protected = thread.is_closed or thread.category.is_closed
  389. else:
  390. thread_is_protected = False
  391. if (can_change_owned_thread(user, thread) and not thread_is_protected
  392. and not thread.replies and not thread.acl['can_hide']):
  393. can_hide_thread = category_acl.get('can_hide_own_threads')
  394. thread.acl['can_hide'] = can_hide_thread
  395. if not thread_is_protected:
  396. thread.acl['can_pin'] = category_acl.get('can_pin_threads', 0)
  397. thread.acl['can_move'] = category_acl.get('can_move_threads', False)
  398. thread.acl['can_merge'] = category_acl.get('can_merge_threads', False)
  399. thread.acl['can_move_posts'] = category_acl.get('can_move_posts', False)
  400. thread.acl['can_merge_posts'] = category_acl.get('can_merge_posts', False)
  401. def add_acl_to_post(user, post):
  402. if post.is_event:
  403. add_acl_to_event(user, post)
  404. else:
  405. add_acl_to_reply(user, post)
  406. def add_acl_to_event(user, event):
  407. if user.is_authenticated:
  408. category_acl = user.acl_cache['categories'].get(event.category_id, {})
  409. can_hide_events = category_acl.get('can_hide_events', 0)
  410. else:
  411. can_hide_events = 0
  412. event.acl.update({
  413. 'can_see_hidden': can_hide_events > 0,
  414. 'can_hide': can_hide_events > 0,
  415. 'can_delete': can_hide_events == 2,
  416. })
  417. def add_acl_to_reply(user, post):
  418. category_acl = user.acl_cache['categories'].get(post.category_id, {})
  419. post.acl.update({
  420. 'can_reply': can_reply_thread(user, post.thread),
  421. 'can_edit': can_edit_post(user, post),
  422. 'can_see_hidden': post.is_first_post or category_acl.get('can_hide_posts'),
  423. 'can_unhide': can_unhide_post(user, post),
  424. 'can_hide': can_hide_post(user, post),
  425. 'can_delete': can_delete_post(user, post),
  426. 'can_protect': can_protect_post(user, post),
  427. 'can_approve': can_approve_post(user, post),
  428. 'can_move': can_move_post(user, post),
  429. 'can_report': category_acl.get('can_report_content', False),
  430. 'can_see_reports': category_acl.get('can_see_reports', False),
  431. 'can_see_likes': category_acl.get('can_see_posts_likes', 0),
  432. 'can_like': False,
  433. })
  434. if not post.acl['can_see_hidden']:
  435. post.acl['can_see_hidden'] = post.id == post.thread.first_post_id
  436. if user.is_authenticated and post.acl['can_see_likes']:
  437. post.acl['can_like'] = category_acl.get('can_like_posts', False)
  438. def register_with(registry):
  439. registry.acl_annotator(Category, add_acl_to_category)
  440. registry.acl_annotator(Thread, add_acl_to_thread)
  441. registry.acl_annotator(Post, add_acl_to_post)
  442. def allow_see_thread(user, target):
  443. category_acl = user.acl_cache['categories'].get(
  444. target.category_id, {
  445. 'can_see': False,
  446. 'can_browse': False,
  447. }
  448. )
  449. if not (category_acl['can_see'] and category_acl['can_browse']):
  450. raise Http404()
  451. if target.is_hidden and (user.is_anonymous or not category_acl['can_hide_threads']):
  452. raise Http404()
  453. if user.is_anonymous or user.pk != target.starter_id:
  454. if not category_acl['can_see_all_threads']:
  455. raise Http404()
  456. if target.is_unapproved and not category_acl['can_approve_content']:
  457. raise Http404()
  458. can_see_thread = return_boolean(allow_see_thread)
  459. def allow_start_thread(user, target):
  460. if user.is_anonymous:
  461. raise PermissionDenied(_("You have to sign in to start threads."))
  462. category_acl = user.acl_cache['categories'].get(
  463. target.pk, {
  464. 'can_close_threads': False,
  465. 'can_start_threads': False,
  466. }
  467. )
  468. if target.is_closed and not category_acl['can_close_threads']:
  469. raise PermissionDenied(_("This category is closed. You can't start new threads in it."))
  470. if not category_acl['can_start_threads']:
  471. raise PermissionDenied(
  472. _("You don't have permission to start new threads in this category.")
  473. )
  474. can_start_thread = return_boolean(allow_start_thread)
  475. def allow_reply_thread(user, target):
  476. if user.is_anonymous:
  477. raise PermissionDenied(_("You have to sign in to reply threads."))
  478. category_acl = user.acl_cache['categories'].get(
  479. target.category_id, {
  480. 'can_close_threads': False,
  481. 'can_reply_threads': False,
  482. }
  483. )
  484. if not category_acl['can_close_threads']:
  485. if target.category.is_closed:
  486. raise PermissionDenied(_("This category is closed. You can't reply to threads in it."))
  487. if target.is_closed:
  488. raise PermissionDenied(_("You can't reply to closed threads in this category."))
  489. if not category_acl['can_reply_threads']:
  490. raise PermissionDenied(_("You can't reply to threads in this category."))
  491. can_reply_thread = return_boolean(allow_reply_thread)
  492. def allow_edit_thread(user, target):
  493. if user.is_anonymous:
  494. raise PermissionDenied(_("You have to sign in to edit threads."))
  495. category_acl = user.acl_cache['categories'].get(
  496. target.category_id, {'can_edit_threads': False}
  497. )
  498. if not category_acl['can_edit_threads']:
  499. raise PermissionDenied(_("You can't edit threads in this category."))
  500. if category_acl['can_edit_threads'] == 1:
  501. if target.starter_id != user.pk:
  502. raise PermissionDenied(_("You can't edit other users threads in this category."))
  503. if not category_acl['can_close_threads']:
  504. if target.category.is_closed:
  505. raise PermissionDenied(_("This category is closed. You can't edit threads in it."))
  506. if target.is_closed:
  507. raise PermissionDenied(_("You can't edit closed threads in this category."))
  508. if not has_time_to_edit_thread(user, target):
  509. message = ungettext(
  510. "You can't edit threads that are older than %(minutes)s minute.",
  511. "You can't edit threads that are older than %(minutes)s minutes.",
  512. category_acl['thread_edit_time']
  513. )
  514. raise PermissionDenied(message % {'minutes': category_acl['thread_edit_time']})
  515. can_edit_thread = return_boolean(allow_edit_thread)
  516. def allow_see_post(user, target):
  517. category_acl = user.acl_cache['categories'].get(
  518. target.category_id, {
  519. 'can_approve_content': False,
  520. 'can_hide_events': False,
  521. }
  522. )
  523. if not target.is_event and target.is_unapproved:
  524. if user.is_anonymous:
  525. raise Http404()
  526. if not category_acl['can_approve_content'] and user.id != target.poster_id:
  527. raise Http404()
  528. if target.is_event and target.is_hidden and not category_acl['can_hide_events']:
  529. raise Http404()
  530. can_see_post = return_boolean(allow_see_post)
  531. def allow_edit_post(user, target):
  532. if user.is_anonymous:
  533. raise PermissionDenied(_("You have to sign in to edit posts."))
  534. if target.is_event:
  535. raise PermissionDenied(_("Events can't be edited."))
  536. category_acl = user.acl_cache['categories'].get(target.category_id, {'can_edit_posts': False})
  537. if not category_acl['can_edit_posts']:
  538. raise PermissionDenied(_("You can't edit posts in this category."))
  539. if not category_acl['can_close_threads']:
  540. if target.category.is_closed:
  541. raise PermissionDenied(_("This category is closed. You can't edit posts in it."))
  542. if target.thread.is_closed:
  543. raise PermissionDenied(_("This thread is closed. You can't edit posts in it."))
  544. if target.is_hidden and not target.is_first_post and not category_acl['can_hide_posts']:
  545. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  546. if category_acl['can_edit_posts'] == 1:
  547. if target.poster_id != user.pk:
  548. raise PermissionDenied(_("You can't edit other users posts in this category."))
  549. if target.is_protected and not category_acl['can_protect_posts']:
  550. raise PermissionDenied(_("This post is protected. You can't edit it."))
  551. if not has_time_to_edit_post(user, target):
  552. message = ungettext(
  553. "You can't edit posts that are older than %(minutes)s minute.",
  554. "You can't edit posts that are older than %(minutes)s minutes.",
  555. category_acl['post_edit_time'],
  556. )
  557. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  558. can_edit_post = return_boolean(allow_edit_post)
  559. def allow_unhide_post(user, target):
  560. if user.is_anonymous:
  561. raise PermissionDenied(_("You have to sign in to reveal posts."))
  562. category_acl = user.acl_cache['categories'].get(
  563. target.category_id, {
  564. 'can_hide_posts': 0,
  565. 'can_hide_own_posts': 0,
  566. }
  567. )
  568. if not category_acl['can_hide_posts']:
  569. if not category_acl['can_hide_own_posts']:
  570. raise PermissionDenied(_("You can't reveal posts in this category."))
  571. if user.id != target.poster_id:
  572. raise PermissionDenied(_("You can't reveal other users posts in this category."))
  573. if not category_acl['can_close_threads']:
  574. if target.category.is_closed:
  575. raise PermissionDenied(_("This category is closed. You can't reveal posts in it."))
  576. if target.thread.is_closed:
  577. raise PermissionDenied(_("This thread is closed. You can't reveal posts in it."))
  578. if target.is_protected and not category_acl['can_protect_posts']:
  579. raise PermissionDenied(_("This post is protected. You can't reveal it."))
  580. if not has_time_to_edit_post(user, target):
  581. message = ungettext(
  582. "You can't reveal posts that are older than %(minutes)s minute.",
  583. "You can't reveal posts that are older than %(minutes)s minutes.",
  584. category_acl['post_edit_time'],
  585. )
  586. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  587. if target.is_first_post:
  588. raise PermissionDenied(_("You can't reveal thread's first post."))
  589. can_unhide_post = return_boolean(allow_unhide_post)
  590. def allow_hide_post(user, target):
  591. if user.is_anonymous:
  592. raise PermissionDenied(_("You have to sign in to hide posts."))
  593. category_acl = user.acl_cache['categories'].get(
  594. target.category_id, {
  595. 'can_hide_posts': 0,
  596. 'can_hide_own_posts': 0,
  597. }
  598. )
  599. if not category_acl['can_hide_posts']:
  600. if not category_acl['can_hide_own_posts']:
  601. raise PermissionDenied(_("You can't hide posts in this category."))
  602. if user.id != target.poster_id:
  603. raise PermissionDenied(_("You can't hide other users posts in this category."))
  604. if not category_acl['can_close_threads']:
  605. if target.category.is_closed:
  606. raise PermissionDenied(_("This category is closed. You can't hide posts in it."))
  607. if target.thread.is_closed:
  608. raise PermissionDenied(_("This thread is closed. You can't hide posts in it."))
  609. if target.is_protected and not category_acl['can_protect_posts']:
  610. raise PermissionDenied(_("This post is protected. You can't hide it."))
  611. if not has_time_to_edit_post(user, target):
  612. message = ungettext(
  613. "You can't hide posts that are older than %(minutes)s minute.",
  614. "You can't hide posts that are older than %(minutes)s minutes.",
  615. category_acl['post_edit_time'],
  616. )
  617. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  618. if target.is_first_post:
  619. raise PermissionDenied(_("You can't hide thread's first post."))
  620. can_hide_post = return_boolean(allow_hide_post)
  621. def allow_delete_post(user, target):
  622. if user.is_anonymous:
  623. raise PermissionDenied(_("You have to sign in to delete posts."))
  624. category_acl = user.acl_cache['categories'].get(
  625. target.category_id, {
  626. 'can_hide_posts': 0,
  627. 'can_hide_own_posts': 0,
  628. }
  629. )
  630. if category_acl['can_hide_posts'] != 2:
  631. if category_acl['can_hide_own_posts'] != 2:
  632. raise PermissionDenied(_("You can't delete posts in this category."))
  633. if user.id != target.poster_id:
  634. raise PermissionDenied(_("You can't delete other users posts in this category."))
  635. if not category_acl['can_close_threads']:
  636. if target.category.is_closed:
  637. raise PermissionDenied(_("This category is closed. You can't delete posts in it."))
  638. if target.thread.is_closed:
  639. raise PermissionDenied(_("This thread is closed. You can't delete posts in it."))
  640. if target.is_protected and not category_acl['can_protect_posts']:
  641. raise PermissionDenied(_("This post is protected. You can't delete it."))
  642. if not has_time_to_edit_post(user, target):
  643. message = ungettext(
  644. "You can't delete posts that are older than %(minutes)s minute.",
  645. "You can't delete posts that are older than %(minutes)s minutes.",
  646. category_acl['post_edit_time'],
  647. )
  648. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  649. if target.is_first_post:
  650. raise PermissionDenied(_("You can't delete thread's first post."))
  651. can_delete_post = return_boolean(allow_delete_post)
  652. def allow_protect_post(user, target):
  653. if user.is_anonymous:
  654. raise PermissionDenied(_("You have to sign in to protect posts."))
  655. category_acl = user.acl_cache['categories'].get(
  656. target.category_id, {'can_protect_posts': False}
  657. )
  658. if not category_acl['can_protect_posts']:
  659. raise PermissionDenied(_("You can't protect posts in this category."))
  660. if not can_edit_post(user, target):
  661. raise PermissionDenied(_("You can't protect posts you can't edit."))
  662. can_protect_post = return_boolean(allow_protect_post)
  663. def allow_approve_post(user, target):
  664. if user.is_anonymous:
  665. raise PermissionDenied(_("You have to sign in to approve posts."))
  666. category_acl = user.acl_cache['categories'].get(
  667. target.category_id, {'can_approve_content': False}
  668. )
  669. if not category_acl['can_approve_content']:
  670. raise PermissionDenied(_("You can't approve posts in this category."))
  671. if target.is_first_post:
  672. raise PermissionDenied(_("You can't approve thread's first post."))
  673. if not target.is_first_post and not category_acl['can_hide_posts'] and target.is_hidden:
  674. raise PermissionDenied(_("You can't approve posts the content you can't see."))
  675. can_approve_post = return_boolean(allow_approve_post)
  676. def allow_move_post(user, target):
  677. if user.is_anonymous:
  678. raise PermissionDenied(_("You have to sign in to move posts."))
  679. category_acl = user.acl_cache['categories'].get(target.category_id, {'can_move_posts': False})
  680. if not category_acl['can_move_posts']:
  681. raise PermissionDenied(_("You can't move posts in this category."))
  682. if target.is_event:
  683. raise PermissionDenied(_("Events can't be moved."))
  684. if target.is_first_post:
  685. raise PermissionDenied(_("You can't move thread's first post."))
  686. if not category_acl['can_hide_posts'] and target.is_hidden:
  687. raise PermissionDenied(_("You can't move posts the content you can't see."))
  688. can_move_post = return_boolean(allow_move_post)
  689. def allow_delete_event(user, target):
  690. if user.is_anonymous:
  691. raise PermissionDenied(_("You have to sign in to delete events."))
  692. category_acl = user.acl_cache['categories'].get(target.category_id)
  693. if not category_acl or category_acl['can_hide_events'] != 2:
  694. raise PermissionDenied(_("You can't delete events in this category."))
  695. can_delete_event = return_boolean(allow_delete_event)
  696. def can_change_owned_thread(user, target):
  697. if user.is_anonymous or user.pk != target.starter_id:
  698. return False
  699. if target.category.is_closed or target.is_closed:
  700. return False
  701. return has_time_to_edit_thread(user, target)
  702. def has_time_to_edit_thread(user, target):
  703. edit_time = user.acl_cache['categories'].get(target.category_id, {}).get('thread_edit_time', 0)
  704. if edit_time:
  705. diff = timezone.now() - target.started_on
  706. diff_minutes = int(diff.total_seconds() / 60)
  707. return diff_minutes < edit_time
  708. else:
  709. return True
  710. def has_time_to_edit_post(user, target):
  711. edit_time = user.acl_cache['categories'].get(target.category_id, {}).get('post_edit_time', 0)
  712. if edit_time:
  713. diff = timezone.now() - target.posted_on
  714. diff_minutes = int(diff.total_seconds() / 60)
  715. return diff_minutes < edit_time
  716. else:
  717. return True
  718. def exclude_invisible_threads(user, categories, queryset):
  719. show_all = []
  720. show_accepted_visible = []
  721. show_accepted = []
  722. show_visible = []
  723. show_owned = []
  724. show_owned_visible = []
  725. for category in categories:
  726. add_acl(user, category)
  727. if not (category.acl['can_see'] and category.acl['can_browse']):
  728. continue
  729. can_hide = category.acl['can_hide_threads']
  730. if category.acl['can_see_all_threads']:
  731. can_mod = category.acl['can_approve_content']
  732. if can_mod and can_hide:
  733. show_all.append(category)
  734. elif user.is_authenticated:
  735. if not can_mod and not can_hide:
  736. show_accepted_visible.append(category)
  737. elif not can_mod:
  738. show_accepted.append(category)
  739. elif not can_hide:
  740. show_visible.append(category)
  741. else:
  742. show_accepted_visible.append(category)
  743. elif user.is_authenticated:
  744. if can_hide:
  745. show_owned.append(category)
  746. else:
  747. show_owned_visible.append(category)
  748. conditions = None
  749. if show_all:
  750. conditions = Q(category__in=show_all)
  751. if show_accepted_visible:
  752. if user.is_authenticated:
  753. condition = Q(
  754. Q(starter=user) | Q(is_unapproved=False),
  755. category__in=show_accepted_visible,
  756. is_hidden=False,
  757. )
  758. else:
  759. condition = Q(
  760. category__in=show_accepted_visible,
  761. is_hidden=False,
  762. is_unapproved=False,
  763. )
  764. if conditions:
  765. conditions = conditions | condition
  766. else:
  767. conditions = condition
  768. if show_accepted:
  769. condition = Q(
  770. Q(starter=user) | Q(is_unapproved=False),
  771. category__in=show_accepted,
  772. )
  773. if conditions:
  774. conditions = conditions | condition
  775. else:
  776. conditions = condition
  777. if show_visible:
  778. condition = Q(category__in=show_visible, is_hidden=False)
  779. if conditions:
  780. conditions = conditions | condition
  781. else:
  782. conditions = condition
  783. if show_owned:
  784. condition = Q(category__in=show_owned, starter=user)
  785. if conditions:
  786. conditions = conditions | condition
  787. else:
  788. conditions = condition
  789. if show_owned_visible:
  790. condition = Q(
  791. category__in=show_owned_visible,
  792. starter=user,
  793. is_hidden=False,
  794. )
  795. if conditions:
  796. conditions = conditions | condition
  797. else:
  798. conditions = condition
  799. if conditions:
  800. return queryset.filter(conditions)
  801. else:
  802. return Thread.objects.none()
  803. def exclude_invisible_posts(user, category, queryset):
  804. if not category.acl['can_approve_content']:
  805. if user.is_authenticated:
  806. queryset = queryset.filter(Q(is_unapproved=False) | Q(poster=user))
  807. else:
  808. queryset = queryset.exclude(is_unapproved=True)
  809. if not category.acl['can_hide_events']:
  810. queryset = queryset.exclude(is_event=True, is_hidden=True)
  811. return queryset