threads.py 30 KB

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