threads.py 36 KB

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