threads.py 32 KB

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