threads.py 31 KB

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