threads.py 33 KB

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