threads.py 32 KB

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