threads.py 38 KB

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