threads.py 33 KB

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