threads.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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. 'can_see': False,
  417. 'can_browse': False
  418. })
  419. if not (category_acl['can_see'] and category_acl['can_browse']):
  420. raise Http404()
  421. if target.is_hidden and (user.is_anonymous() or not category_acl['can_hide_threads']):
  422. raise Http404()
  423. if user.is_anonymous() or user.pk != target.starter_id:
  424. if not category_acl['can_see_all_threads']:
  425. raise Http404()
  426. if target.is_unapproved and not category_acl['can_approve_content']:
  427. raise Http404()
  428. can_see_thread = return_boolean(allow_see_thread)
  429. def allow_start_thread(user, target):
  430. if user.is_anonymous():
  431. raise PermissionDenied(_("You have to sign in to start threads."))
  432. category_acl = user.acl['categories'].get(target.pk, {
  433. 'can_close_threads': False,
  434. 'can_start_threads': False
  435. })
  436. if target.is_closed and not category_acl['can_close_threads']:
  437. raise PermissionDenied(_("This category is closed. You can't start new threads in it."))
  438. if not category_acl['can_start_threads']:
  439. raise PermissionDenied(_("You don't have permission to start new threads in this category."))
  440. can_start_thread = return_boolean(allow_start_thread)
  441. def allow_reply_thread(user, target):
  442. if user.is_anonymous():
  443. raise PermissionDenied(_("You have to sign in to reply threads."))
  444. category_acl = user.acl['categories'].get(target.category_id, {
  445. 'can_close_threads': False,
  446. 'can_reply_threads': False
  447. })
  448. if not category_acl['can_close_threads']:
  449. if target.category.is_closed:
  450. raise PermissionDenied(_("This category is closed. You can't reply to threads in it."))
  451. if target.is_closed:
  452. raise PermissionDenied(_("You can't reply to closed threads in this category."))
  453. if not category_acl['can_reply_threads']:
  454. raise PermissionDenied(_("You can't reply to threads in this category."))
  455. can_reply_thread = return_boolean(allow_reply_thread)
  456. def allow_edit_thread(user, target):
  457. if user.is_anonymous():
  458. raise PermissionDenied(_("You have to sign in to edit threads."))
  459. category_acl = user.acl['categories'].get(target.category_id, {
  460. 'can_edit_threads': False
  461. })
  462. if not category_acl['can_edit_threads']:
  463. raise PermissionDenied(_("You can't edit threads in this category."))
  464. if category_acl['can_edit_threads'] == 1:
  465. if target.starter_id != user.pk:
  466. raise PermissionDenied(_("You can't edit other users threads in this category."))
  467. if not category_acl['can_close_threads']:
  468. if target.category.is_closed:
  469. raise PermissionDenied(_("This category is closed. You can't edit threads in it."))
  470. if target.is_closed:
  471. raise PermissionDenied(_("You can't edit closed threads in this category."))
  472. if not has_time_to_edit_thread(user, target):
  473. message = ungettext(
  474. "You can't edit threads that are older than %(minutes)s minute.",
  475. "You can't edit threads that are older than %(minutes)s minutes.",
  476. category_acl['thread_edit_time'])
  477. raise PermissionDenied(message % {'minutes': category_acl['thread_edit_time']})
  478. can_edit_thread = return_boolean(allow_edit_thread)
  479. def allow_edit_post(user, target):
  480. if user.is_anonymous():
  481. raise PermissionDenied(_("You have to sign in to edit posts."))
  482. if target.is_event:
  483. raise PermissionDenied(_("Events can't be edited."))
  484. category_acl = user.acl['categories'].get(target.category_id, {
  485. 'can_edit_posts': False
  486. })
  487. if not category_acl['can_edit_posts']:
  488. raise PermissionDenied(_("You can't edit posts in this category."))
  489. if not category_acl['can_close_threads']:
  490. if target.category.is_closed:
  491. raise PermissionDenied(_("This category is closed. You can't edit posts in it."))
  492. if target.thread.is_closed:
  493. raise PermissionDenied(_("This thread is closed. You can't edit posts in it."))
  494. if target.is_hidden and not target.is_first_post and not category_acl['can_hide_posts']:
  495. raise PermissionDenied(_("This post is hidden, you can't edit it."))
  496. if category_acl['can_edit_posts'] == 1:
  497. if target.poster_id != user.pk:
  498. raise PermissionDenied(_("You can't edit other users posts in this category."))
  499. if target.is_protected and not category_acl['can_protect_posts']:
  500. raise PermissionDenied(_("This post is protected. You can't edit it."))
  501. if not has_time_to_edit_post(user, target):
  502. message = ungettext(
  503. "You can't edit posts that are older than %(minutes)s minute.",
  504. "You can't edit posts that are older than %(minutes)s minutes.",
  505. category_acl['post_edit_time'])
  506. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  507. can_edit_post = return_boolean(allow_edit_post)
  508. def allow_unhide_post(user, target):
  509. if user.is_anonymous():
  510. raise PermissionDenied(_("You have to sign in to reveal posts."))
  511. category_acl = user.acl['categories'].get(target.category_id, {
  512. 'can_hide_posts': 0,
  513. 'can_hide_own_posts': 0
  514. })
  515. if not category_acl['can_hide_posts']:
  516. if not category_acl['can_hide_own_posts']:
  517. raise PermissionDenied(_("You can't reveal posts in this category."))
  518. if user.id != target.poster_id:
  519. raise PermissionDenied(_("You can't reveal other users posts in this category."))
  520. if not category_acl['can_close_threads']:
  521. if target.category.is_closed:
  522. raise PermissionDenied(_("This category is closed. You can't reveal posts in it."))
  523. if target.thread.is_closed:
  524. raise PermissionDenied(_("This thread is closed. You can't reveal posts in it."))
  525. if target.is_protected and not category_acl['can_protect_posts']:
  526. raise PermissionDenied(_("This post is protected. You can't reveal it."))
  527. if not has_time_to_edit_post(user, target):
  528. message = ungettext(
  529. "You can't reveal posts that are older than %(minutes)s minute.",
  530. "You can't reveal posts that are older than %(minutes)s minutes.",
  531. category_acl['post_edit_time'])
  532. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  533. if target.is_first_post:
  534. raise PermissionDenied(_("You can't reveal thread's first post."))
  535. can_unhide_post = return_boolean(allow_unhide_post)
  536. def allow_hide_post(user, target):
  537. if user.is_anonymous():
  538. raise PermissionDenied(_("You have to sign in to hide posts."))
  539. category_acl = user.acl['categories'].get(target.category_id, {
  540. 'can_hide_posts': 0,
  541. 'can_hide_own_posts': 0
  542. })
  543. if not category_acl['can_hide_posts']:
  544. if not category_acl['can_hide_own_posts']:
  545. raise PermissionDenied(_("You can't hide posts in this category."))
  546. if user.id != target.poster_id:
  547. raise PermissionDenied(_("You can't hide other users posts in this category."))
  548. if not category_acl['can_close_threads']:
  549. if target.category.is_closed:
  550. raise PermissionDenied(_("This category is closed. You can't hide posts in it."))
  551. if target.thread.is_closed:
  552. raise PermissionDenied(_("This thread is closed. You can't hide posts in it."))
  553. if target.is_protected and not category_acl['can_protect_posts']:
  554. raise PermissionDenied(_("This post is protected. You can't hide it."))
  555. if not has_time_to_edit_post(user, target):
  556. message = ungettext(
  557. "You can't hide posts that are older than %(minutes)s minute.",
  558. "You can't hide posts that are older than %(minutes)s minutes.",
  559. category_acl['post_edit_time'])
  560. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  561. if target.is_first_post:
  562. raise PermissionDenied(_("You can't hide thread's first post."))
  563. can_hide_post = return_boolean(allow_hide_post)
  564. def allow_delete_post(user, target):
  565. if user.is_anonymous():
  566. raise PermissionDenied(_("You have to sign in to delete posts."))
  567. category_acl = user.acl['categories'].get(target.category_id, {
  568. 'can_hide_posts': 0,
  569. 'can_hide_own_posts': 0
  570. })
  571. if category_acl['can_hide_posts'] != 2:
  572. if category_acl['can_hide_own_posts'] != 2:
  573. raise PermissionDenied(_("You can't delete posts in this category."))
  574. if user.id != target.poster_id:
  575. raise PermissionDenied(_("You can't delete other users posts in this category."))
  576. if not category_acl['can_close_threads']:
  577. if target.category.is_closed:
  578. raise PermissionDenied(_("This category is closed. You can't delete posts from it."))
  579. if target.thread.is_closed:
  580. raise PermissionDenied(_("This thread is closed. You can't delete posts from it."))
  581. if target.is_protected and not category_acl['can_protect_posts']:
  582. raise PermissionDenied(_("This post is protected. You can't delete it."))
  583. if not has_time_to_edit_post(user, target):
  584. message = ungettext(
  585. "You can't delete posts that are older than %(minutes)s minute.",
  586. "You can't delete posts that are older than %(minutes)s minutes.",
  587. category_acl['post_edit_time'])
  588. raise PermissionDenied(message % {'minutes': category_acl['post_edit_time']})
  589. if target.is_first_post:
  590. raise PermissionDenied(_("You can't delete thread's first post."))
  591. can_delete_post = return_boolean(allow_delete_post)
  592. def allow_protect_post(user, target):
  593. if user.is_anonymous():
  594. raise PermissionDenied(_("You have to sign in to protect posts."))
  595. category_acl = user.acl['categories'].get(target.category_id, {
  596. 'can_protect_posts': False
  597. })
  598. if not category_acl['can_protect_posts']:
  599. raise PermissionDenied(_("You can't protect posts in this category."))
  600. if not can_edit_post(user, target):
  601. raise PermissionDenied(_("You can't protect posts you can't edit."))
  602. can_protect_post = return_boolean(allow_protect_post)
  603. def allow_approve_post(user, target):
  604. if user.is_anonymous():
  605. raise PermissionDenied(_("You have to sign in to approve posts."))
  606. category_acl = user.acl['categories'].get(target.category_id, {
  607. 'can_approve_content': False
  608. })
  609. if not category_acl['can_approve_content']:
  610. raise PermissionDenied(_("You can't approve posts in this category."))
  611. if target.is_first_post:
  612. raise PermissionDenied(_("You can't approve thread's first post."))
  613. if not target.is_first_post and not category_acl['can_hide_posts'] and target.is_hidden:
  614. raise PermissionDenied(_("You can't approve posts the content you can't see."))
  615. can_approve_post = return_boolean(allow_approve_post)
  616. def allow_delete_event(user, target):
  617. if user.is_anonymous():
  618. raise PermissionDenied(_("You have to sign in to delete events."))
  619. category_acl = user.acl['categories'].get(target.category_id)
  620. if not category_acl or category_acl['can_hide_events'] != 2:
  621. raise PermissionDenied(_("You can't delete events in this category."))
  622. can_delete_event = return_boolean(allow_delete_event)
  623. """
  624. Permission check helpers
  625. """
  626. def can_change_owned_thread(user, target):
  627. if user.is_anonymous() or user.pk != target.starter_id:
  628. return False
  629. if target.category.is_closed or target.is_closed:
  630. return False
  631. return has_time_to_edit_thread(user, target)
  632. def has_time_to_edit_thread(user, target):
  633. edit_time = user.acl['categories'].get(target.category_id, {}).get('thread_edit_time', 0)
  634. if edit_time:
  635. diff = timezone.now() - target.started_on
  636. diff_minutes = int(diff.total_seconds() / 60)
  637. return diff_minutes < edit_time
  638. else:
  639. return True
  640. def has_time_to_edit_post(user, target):
  641. edit_time = user.acl['categories'].get(target.category_id, {}).get('post_edit_time', 0)
  642. if edit_time:
  643. diff = timezone.now() - target.posted_on
  644. diff_minutes = int(diff.total_seconds() / 60)
  645. return diff_minutes < edit_time
  646. else:
  647. return True
  648. """
  649. Queryset helpers
  650. """
  651. def exclude_invisible_category_threads(queryset, user, category):
  652. if user.is_authenticated():
  653. condition_author = Q(starter_id=user.id)
  654. can_mod = category.acl['can_approve_content']
  655. can_hide = category.acl['can_hide_threads']
  656. if not can_mod and not can_hide:
  657. condition = Q(is_unapproved=False) & Q(is_hidden=False)
  658. queryset = queryset.filter(condition_author | condition)
  659. elif not can_mod:
  660. condition = Q(is_unapproved=False)
  661. queryset = queryset.filter(condition_author | condition)
  662. elif not can_hide:
  663. condition = Q(is_hidden=False)
  664. queryset = queryset.filter(condition_author | condition)
  665. else:
  666. if not category.acl['can_approve_content']:
  667. queryset = queryset.filter(is_unapproved=False)
  668. if not category.acl['can_hide_threads']:
  669. queryset = queryset.filter(is_hidden=False)
  670. return queryset
  671. def exclude_invisible_threads(user, categories, queryset):
  672. show_all = []
  673. show_accepted_visible = []
  674. show_accepted = []
  675. show_visible = []
  676. show_owned = []
  677. show_owned_visible = []
  678. for category in categories:
  679. add_acl(user, category)
  680. if not (category.acl['can_see'] and category.acl['can_browse']):
  681. continue
  682. can_hide = category.acl['can_hide_threads']
  683. if category.acl['can_see_all_threads']:
  684. can_mod = category.acl['can_approve_content']
  685. if can_mod and can_hide:
  686. show_all.append(category)
  687. elif user.is_authenticated():
  688. if not can_mod and not can_hide:
  689. show_accepted_visible.append(category)
  690. elif not can_mod:
  691. show_accepted.append(category)
  692. elif not can_hide:
  693. show_visible.append(category)
  694. else:
  695. show_accepted_visible.append(category)
  696. elif user.is_authenticated():
  697. if can_hide:
  698. show_owned.append(category)
  699. else:
  700. show_owned_visible.append(category)
  701. conditions = None
  702. if show_all:
  703. conditions = Q(category__in=show_all)
  704. if show_accepted_visible:
  705. if user.is_authenticated():
  706. condition = Q(
  707. Q(starter=user) | Q(is_unapproved=False),
  708. category__in=show_accepted_visible,
  709. is_hidden=False,
  710. )
  711. else:
  712. condition = Q(
  713. category__in=show_accepted_visible,
  714. is_hidden=False,
  715. is_unapproved=False,
  716. )
  717. if conditions:
  718. conditions = conditions | condition
  719. else:
  720. conditions = condition
  721. if show_accepted:
  722. condition = Q(
  723. Q(starter=user) | Q(is_unapproved=False),
  724. category__in=show_accepted,
  725. )
  726. if conditions:
  727. conditions = conditions | condition
  728. else:
  729. conditions = condition
  730. if show_visible:
  731. condition = Q(category__in=show_visible, is_hidden=False)
  732. if conditions:
  733. conditions = conditions | condition
  734. else:
  735. conditions = condition
  736. if show_owned:
  737. condition = Q(category__in=show_owned, starter=user)
  738. if conditions:
  739. conditions = conditions | condition
  740. else:
  741. conditions = condition
  742. if show_owned_visible:
  743. condition = Q(
  744. category__in=show_owned_visible,
  745. starter=user,
  746. is_hidden=False,
  747. )
  748. if conditions:
  749. conditions = conditions | condition
  750. else:
  751. conditions = condition
  752. if conditions:
  753. return Thread.objects.filter(conditions)
  754. else:
  755. return Thread.objects.none()
  756. def exclude_invisible_posts(user, category, queryset):
  757. if not category.acl['can_approve_content']:
  758. if user.is_authenticated():
  759. queryset = queryset.filter(Q(is_unapproved=False) | Q(poster=user))
  760. else:
  761. queryset = queryset.exclude(is_unapproved=True)
  762. if not category.acl['can_hide_events']:
  763. queryset = queryset.exclude(is_event=True, is_hidden=True)
  764. return queryset