threads.py 33 KB

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