threads.py 33 KB

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