threads.py 30 KB

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