threads.py 31 KB

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